使用 basichttpbinding 调用 wcf webservice,无需 REST 或 JSON
我有一个通过 wsHTTPBinding 和 basicHTTPBinding 公开的 wcf Web 服务。后者将其端点地址指定为“/basic”,如下所示:
<endpoint address="/basic" binding="basicHttpBinding" bindingConfiguration="theAwesomeBasicHttpServerBinding" contract="LOServices.Proxy.ServiceContracts.ILOService">
<identity>
<servicePrincipalName value="HTTP/MY_MACHINE_NAME" />
</identity>
</endpoint>
绑定定义如下:
<basicHttpBinding>
<binding name="theAwesomeBasicHttpServerBinding" maxReceivedMessageSize="5000000">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
我试图使用 jquery 调用其中一个 webmethods。因为这是基于 basicHTTPBinding 而不是 RESTful,因此,我不会使用 JSON。因此,我通过 wcf 测试客户端对服务进行的先前(成功)调用构建了请求 XML:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/ILOService/GetLoanActions</Action>
</s:Header>
<s:Body>
<GetLoanActions xmlns="http://tempuri.org/">
<request xmlns:d4p1="http://schemas.datacontract.org/2004/07/LOServices.Proxy.Requests" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:AssociationNumber>3</d4p1:AssociationNumber>
<d4p1:IgnoreWarnings>false</d4p1:IgnoreWarnings>
</request>
</GetLoanActions>
</s:Body>
</s:Envelope>
因此,我使用的实际 javascript 的结构如下:
function callWs() {
var theRequest = " \
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"> \
<s:Header> \
<Action s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/ILOService/GetLoanActions</Action> \
</s:Header> \
<s:Body> \
<GetLoanActions xmlns=\"http://tempuri.org/\"> \
<request xmlns:d4p1=\"http://schemas.datacontract.org/2004/07/LOServices.Proxy.Requests\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"> \
<d4p1:AssociationNumber>3</d4p1:AssociationNumber> \
<d4p1:IgnoreWarnings>false</d4p1:IgnoreWarnings> \
</request> \
</GetLoanActions> \
</s:Body> \
</s:Envelope>";
$.ajax({
type: "POST",
url: "http://localhost/LOServices/Services/LOService.svc/basic",
data: theRequest,
timeout: 10000,
contentType: "text/xml",
dataType: "xml",
async: false
});
}
我很不幸收到“错误请求”然而,当我以这种方式运行 wcf 服务时,这并没有给我留下太多可以跟进的信息。
我已经努力尝试让这项工作至少 3 个小时了,所以如果有人有想法,请随时提出一些建议。
谢谢。
I have a wcf webservice that is exposed through wsHTTPBinding and basicHTTPBinding. The latter specifies it's endpoint address as being "/basic" as in the following:
<endpoint address="/basic" binding="basicHttpBinding" bindingConfiguration="theAwesomeBasicHttpServerBinding" contract="LOServices.Proxy.ServiceContracts.ILOService">
<identity>
<servicePrincipalName value="HTTP/MY_MACHINE_NAME" />
</identity>
</endpoint>
the binding is defined as follows:
<basicHttpBinding>
<binding name="theAwesomeBasicHttpServerBinding" maxReceivedMessageSize="5000000">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
I am trying to call one of the webmethods using jquery. Because this is over a basicHTTPBinding and not RESTful, therefore, I will not be using JSON. I consequently am building the request XML from a previous (successful) call that I made to the service through wcf test client:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/ILOService/GetLoanActions</Action>
</s:Header>
<s:Body>
<GetLoanActions xmlns="http://tempuri.org/">
<request xmlns:d4p1="http://schemas.datacontract.org/2004/07/LOServices.Proxy.Requests" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:AssociationNumber>3</d4p1:AssociationNumber>
<d4p1:IgnoreWarnings>false</d4p1:IgnoreWarnings>
</request>
</GetLoanActions>
</s:Body>
</s:Envelope>
The actual javascript that I am using therefore is structured as follows:
function callWs() {
var theRequest = " \
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"> \
<s:Header> \
<Action s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/ILOService/GetLoanActions</Action> \
</s:Header> \
<s:Body> \
<GetLoanActions xmlns=\"http://tempuri.org/\"> \
<request xmlns:d4p1=\"http://schemas.datacontract.org/2004/07/LOServices.Proxy.Requests\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"> \
<d4p1:AssociationNumber>3</d4p1:AssociationNumber> \
<d4p1:IgnoreWarnings>false</d4p1:IgnoreWarnings> \
</request> \
</GetLoanActions> \
</s:Body> \
</s:Envelope>";
$.ajax({
type: "POST",
url: "http://localhost/LOServices/Services/LOService.svc/basic",
data: theRequest,
timeout: 10000,
contentType: "text/xml",
dataType: "xml",
async: false
});
}
I'm unfortunate enough to receive a "Bad Request" from the wcf service when I run it in this fashion, however, and that doesn't leave me with much to follow up on.
I've been struggling to try to make this work for at least 3 hours now, so if anyone has ideas, please feel free to jump in with some suggestions.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 BasicHttpBinding 请求,您需要在 HTTP 请求中设置 SOAPAction 标头(这将定义哪个操作应接收消息)。
请注意,
headers
选项是 jQuery 1.5 中的新选项,因此如果您使用的是较旧的选项,则需要使用beforeSend
函数。此外,您将来应该能够通过 为 WCF 启用跟踪 - 跟踪将包含有关问题的更详细信息。
For BasicHttpBinding requests, you need to set the SOAPAction header in the HTTP request (that will define which operation should receive the message).
Notice that the
headers
option is new in jQuery 1.5, so if you're using an older one, you'll need to use thebeforeSend
function for that.Also, you should be able to get more information in the future for errors such as "Bad Request" or "Internal Server Error" by enabling tracing for WCF - the trace will contain more detailed information about the problem.