在不使用REST或JSON的情况下使用basichttpbinding调用wcf webservice

Jas*_*son 3 ajax wcf jquery basichttpbinding

我有一个通过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>
Run Code Online (Sandbox Code Playgroud)

绑定定义如下:

  <basicHttpBinding>
    <binding name="theAwesomeBasicHttpServerBinding" maxReceivedMessageSize="5000000">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </basicHttpBinding>
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用jquery调用其中一种web方法。由于这是通过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>
Run Code Online (Sandbox Code Playgroud)

因此,我正在使用的实际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
            });
        }
Run Code Online (Sandbox Code Playgroud)

但是,当我以这种方式运行它时,我很不幸从wcf服务接收到一个“错误请求”,但这不足以使我继续进行后续工作。

我一直在努力使这项工作至少持续3个小时,因此,如果有人有想法,请随时提出一些建议。

谢谢。

car*_*ira 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" }
    });
Run Code Online (Sandbox Code Playgroud)

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

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