我在本地计算机上使用以下代码为我提供临时WCF服务:
using System;
using System.Windows.Forms;
using System.ServiceModel;
using TestService;
namespace TestClient
{
public partial class Form1 : Form
{
IService1 patientSvc = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:2202/PatientService"));
NetTcpBinding binding = new NetTcpBinding();
ChannelFactory<IService1> factory = new ChannelFactory<IService1>(binding, address);
patientSvc = factory.CreateChannel();
}
}
}
和class1:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace TestService
{
[ServiceContract()]
public interface IService1
{
[OperationContract]
Patient GetPatient(Int32 index);
[OperationContract]
void SetPatient(Int32 index, Patient patient);
}
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class PatientService : IService1
{
Patient[] pat = null;
public PatientService()
{
pat = new Patient[3];
pat[0] = new Patient();
pat[0].FirstName = "Bob";
pat[0].LastName = "Chandler";
pat[1] = new Patient();
pat[1].FirstName = "Joe";
pat[1].LastName = "Klink";
pat[2] = new Patient();
pat[2].FirstName = "Sally";
pat[2].LastName = "Wilson";
}
public Patient GetPatient(Int32 index)
{
if (index <= pat.GetUpperBound(0) && index > -1)
return pat[index];
else
return new Patient();
}
public void SetPatient(Int32 index, Patient patient)
{
if (index <= pat.GetUpperBound(0) && index > -1)
pat[index] = patient;
}
}
[DataContract]
public class Patient
{
string firstName;
string lastName;
[DataMember]
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
[DataMember]
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
}
}
如果我使用&#34; 1&#34;来调用 PatientService ,这样就可以了。它显示了值 Joe 和 Klink 。
但是,在我的ASP.net网站页面上执行相同的操作:
.cs页面:
@{
ViewBag.Title = ViewBag.Message;
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="col-sm-offset-2 col-sm-8" style="margin-bottom: 10px; margin-top: 10px; text-align: center;">
<Button class="tips btn btn-success btn-outline" style="margin-right: 10px;" id="WCF" data-tooltip="@Html.Raw(tips["Continue"])">WCF TEST</Button>
</div>
JavaScript:
$(document).ready(function () {
$("#WCF").on("click", function () {
$.ajax({
type: "POST",
url: ajaxDirPath + "WCF",
data: '{"patNum": "1"}',
contentType: "application/json", // content type sent to server
success: ServiceSucceeded,
error: ServiceFailed
});
});
});
function ServiceFailed(result) {
console.log('Service call failed: ' + result.status + ' ' + result.statusText);
}
function ServiceSucceeded(result) {
resultObject = result.MyFunctionResult;
console.log("Success: " + resultObject);
}
背后的代码:
[HttpPost]
public string WCF(string patNum)
{
Dictionary<string, string> resultsBack = new Dictionary<string, string>();
EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:2202/PatientService"));
NetTcpBinding binding = new NetTcpBinding();
ChannelFactory<IService1> factory = new ChannelFactory<IService1>(binding, address);
IService1 patientSvc = factory.CreateChannel();
Patient patient = patientSvc.GetPatient(Convert.ToInt32(patNum));
if (patient != null)
{
resultsBack.Add("dback", "GOOD");
resultsBack.Add("return1", patient.FirstName);
resultsBack.Add("return2", patient.LastName);
}
return JsonConvert.SerializeObject(resultsBack, Formatting.Indented);
}
对于它返回的所有内容,患者变量为 NULL 。
我会做错什么?
更新 这是从javascript和WCF服务调试的步骤:
下一步 - &gt;
下一步 - &gt;
下一步 - &gt;
下一步 - &gt;
下一步 - &gt;