呼叫使用basicHttpBinding的不休息或JSON WCF web服务不休息、basicHttpBinding、JSON、web

2023-09-11 01:08:31 作者:撩妹一把手

我有一个WCF Web服务,它通过WsHttpBinding的和basicHttpBinding的暴露。后者指定它的端点地址为/基本,如以下内容:

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>

我试图调用使用jquery的webMethods的之一。因为这是在一个basicHttpBinding的,而不是基于REST的,因此,我不会使用JSON。我啥都正在建设从previous请求XML(成功)的电话,我通过WCF测试客户端对服务进行:

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>

这是我用的,因此被构造为实际的JavaScript如下:

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
            });
        }

我不幸接收来自WCF服务的一个错误的请求当我跑不过它以这种方式,而且不离开我,多跟进。

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.

我一直在努力尝试,使这项工作至少3小时,现在,因此,如果任何人有想法,请随时跳了一些建议。

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.

感谢。

推荐答案

有关basicHttpBinding的请求,你需要设置在HTTP请求中的SOAPAction头(这将定义哪些操作应该收到消息)。

For BasicHttpBinding requests, you need to set the SOAPAction header in the HTTP request (that will define which operation should receive the message).

$.ajax({
      type: "POST",
      url: "http://localhost/LOServices/Services/LOService.svc/basic",
      data: theRequest,
      timeout: 10000,
      contentType: "text/xml",
      dataType: "xml",
      async: false,
      headers: { "SOAPAction": "http://tempuri.org/Contract/Operation" }
    });

请注意,标题选项中是新增的jQuery 1.5,所以,如果你使用的是旧的,你需要使用 beforeSend 函数,该函数。

Notice that the headers option is new in jQuery 1.5, so if you're using an older one, you'll need to use the beforeSend function for that.

此外,你应该能够得到更多的信息,在未来的错误,例如错误的请求或内部服务器错误通过的启用跟踪为WCF - 跟踪将包含有关问题的更多详细信息。

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.