3

我有一个通过 wsHTTPBinding 和 basicHTTPBinding 公开的 wcf webservice。后者将其端点地址指定为“/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 调用其中一种网络方法。因为这是通过 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 服务时,很不幸地收到了来自 wcf 服务的“错误请求”,这并没有让我有太多的后续工作。

我一直在努力尝试完成这项工作至少 3 个小时,所以如果有人有想法,请随时提出一些建议。

谢谢。

4

1 回答 1

5

对于 BasicHttpBinding 请求,您需要在 HTTP 请求中设置 SOAPAction 标头(这将定义应接收消息的操作)。

$.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" }
    });

请注意,该headers选项是 jQuery 1.5 中的新选项,因此如果您使用的是较旧的选项,则需要为此使用该beforeSend函数。

此外,您应该能够在未来通过启用 WCF 跟踪来获取有关“错误请求”或“内部服务器错误”等错误的更多信息 - 跟踪将包含有关问题的更多详细信息。

于 2011-07-15T20:44:43.273 回答