【问题标题】:calling wcf webservice using basichttpbinding without REST or JSON使用没有 REST 或 JSON 的 basichttpbinding 调用 wcf webservice
【发布时间】:2011-07-15 20:33:48
【问题描述】:

我有一个通过 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 个小时了,所以如果有人有想法,请随时提出一些建议。

谢谢。

【问题讨论】:

    标签: jquery ajax wcf basichttpbinding


    【解决方案1】:

    对于 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 函数。

    此外,您应该能够在未来获得更多关于错误的信息,例如 enabling tracing 为 WCF 提供的“错误请求”或“内部服务器错误” - 跟踪将包含有关该问题的更多详细信息。

    【讨论】:

    • 这正是我想听到的。谢谢@carlosfigueira。还值得注意的是,我必须删除
      标记(和子标记)才能使其正常工作; basicHTTPBinding 不喜欢它。
    猜你喜欢
    • 2011-09-06
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    相关资源
    最近更新 更多