C# 从ASP.net AJAX调用本地WCF服务
我在本地计算机上使用以下代码为我提供临时WCF服务:C# 从ASP.net AJAX调用本地WCF服务,c#,ajax,asp.net-mvc,wcf,nettcpbinding,C#,Ajax,Asp.net Mvc,Wcf,Nettcpbinding,我在本地计算机上使用以下代码为我提供临时WCF服务: using System; using System.Windows.Forms; using System.ServiceModel; using TestService; namespace TestClient { public partial class Form1 : Form { IService1 patientSvc = null; public Form1()
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();
}
}
}
以及背后的代码:
[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);
}
[HttpPost]
公共字符串WCF(字符串patNum)
{
Dictionary resultsBack=新建字典();
EndpointAddress地址=新的EndpointAddress(新Uri(“网络”)。tcp://localhost:2202/PatientService"));
NetTcpBinding=新的NetTcpBinding();
ChannelFactory工厂=新的ChannelFactory(绑定,地址);
IService1 patientSvc=factory.CreateChannel();
Patient-Patient=patientSvc.GetPatient(转换为32(patNum));
如果(患者!=null)
{
结果返回。添加(“dback”、“GOOD”);
结果返回。添加(“返回1”,患者名);
结果返回。添加(“返回2”,患者。姓氏);
}
返回JsonConvert.SerializedObject(resultsBack,Formatting.Indented);
}
patient变量返回的所有内容均为NULL
我会做错什么
更新
以下是从javascript和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();
}
}
}
下一步-->
下一步-->
下一步-->
下一步-->
下一步-->
方法
公共字符串WCF(stringpatnum)
是在哪里编写的?你能调试这个方法吗?你能调试wcf服务吗?是的,两边都是ChetanRanpariya。请参阅我更新的OP以获取此屏幕截图。
$(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);
}