我们有一个 ASP.NET 网站项目。过去,我们一直使用 asmx webservice。现在,我们有了 WCF 服务,我正在尝试使用带有 jQuery 的客户端代理对象调用 WCF 服务。使用 asmx,使用以下代码行调用 Web 服务相当容易
function GetBooks() {
$.ajax({
type: "POST",
data: "{}",
dataType: "json",
url: "http: /WebService.asmx/GetBooks",
contentType: "application/json; charset=utf-8",
success: onSuccess
});
}
WebService 类中的方法是
[WebMethod(EnableSession = true)]
public Books[] GetBooks()
{
List<BooksTO> dtos = BooksDTOUtils.GetBooks(entityOwnerID);
return dtos.ToArray();
}
现在,必须从 jQuery 调用 GetBooks_Wcf() 方法。我在新类(WcfCall.cs)中使用客户端代理来调用 wcf 方法 GetBooks
public Books[] GetBooksWcf()
{
var service = WcfProxy.GetServiceProxy();
var request = new GetBooksRequest();
request.entityOwnerID= entityOwnerID;
var response = service.GetBooks(request);
returnresponse.Results.ToArray();
}
我的代理(Wcfproxy.cs)是
public static Service.ServiceClient GetServiceProxy()
{
var Service = Session["Service"] as Service.ServiceClient;
if (Service == null)
{
// create the proxy
Service = CreateServiceInstance();
// store it in Session for next usage
Session["Service"] = Service;
}
return Service;
}
public static Service.ServiceClient CreateServiceInstance()
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(IgnoreCertificateErrorHandler);
string configValue = Environment.GetConfigSettingStr("WcfService");
Service.ServiceClient webService = new Service.ServiceClient();
//Here is my WCF endpoint
webService.Endpoint.Address = new System.ServiceModel.EndpointAddress(configValue);
return webService;
}
所以,我的问题是如何从 jQuery 调用 GetBooksWcf?我创建了一个reference.cs,上面方法的Service.ServiceClient在下面的reference.cs中。此外,参数“entityOwnerID”是敏感的,我不能从 JQuery 传递它,要么我必须坚持它,要么从 web.config 作为键调用。
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class ServiceClient : System.ServiceModel.ClientBase<Service.IService>, Service.IService
{
.........
}
提前致谢!