21

我正在构建一个需要与非 WCF 客户端互操作的 WCF Web 服务(事实上,不会有 WCF 客户端)。

我已经使用 SOAP 1.2 编写了一个 WSDL(根据这个例子)。我已经验证了 WSDL 并使用了这个文件(不是 WCF 生成的 WSDL,表面上有所不同)来创建一个soapUI测试项目。

我的要求是 Web 服务将支持 SOAP 1.2,所以我不能只是退回到 SOAP 1.1(它在早期原型中工作得很好)。

我使用WSCF.blue来生成我的 WCF 服务、接口和数据协定类。如果我在浏览器中点击 WCF 服务,一切都编译得很好,并且端点会暴露出来。世界上一切似乎都很好。

当我尝试从 soapUi 调用方法时,我从服务器得到以下响应(从 soapUI 可见):

HTTP/1.1 415 Cannot process the message because the content type 
'application/soap+xml;charset=UTF-8;action="http://tempuri.org/FetchMyThing"' 
was not the expected type 'text/xml; charset=utf-8'.
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Mon, 30 Apr 2012 08:15:29 GMT
Content-Length: 0

(为了这个问题,实际的方法名称和命名空间已手动更改。命名空间中的任何拼写错误都不是我的代码中的错误——只是输入这个问题时的疏忽)

我知道 SOAP 1.1 指定内容类型必须是text/xml。SOAP 1.2 需要application/soap+xml

我的原始请求(根据soapUI):

POST http://localhost/MyWs.svc HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/soap+xml;charset=UTF-8;action="http://tempuri.org/FetchMyThing"

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
               xmlns:ns="http://tempuri.org">
   <soap:Header/>
   <soap:Body>
      <ns:fetchMyThingRequest attribute1="1" attribute2="10">
      </ns:fetchMyThingRequest>
   </soap:Body>
</soap:Envelope>

从这个响应中,它告诉我我的请求格式正确——它是一个内容类型正确的 SOAP 1.2 请求。然而,我的 WCF 服务并不期望这种内容类型,我认为这意味着我没有正确配置它,它仍然认为它是一个 SOAP 1.1 Web 服务。

最小的 Web.config,根据这篇博文

<system.serviceModel>
  <services>
    <service name="MyNamespace.MyPort">
      <endpoint address="" binding="customBinding" bindingConfiguration="httpSoap12" contract="IWsPort12" />
    </service>
  </services>

  <bindings>
    <customBinding>
      <binding name="httpSoap12">
        <textMessageEncoding messageVersion="Soap12" />
        <httpTransport />
      </binding>
    </customBinding>
  </bindings>
</system.serviceModel>

服务合同的片段:

[ServiceContract(Namespace = "http://tempuri.org")]
public interface IWsPort
{
  [OperationContract(Action = "http://tempuri.org/FetchMyThing")]
  [FaultContract(typeof(WsFault), Action = "http://tempuri.org/FetchMyThing", Name = "fetchMyThingFault")]
  [XmlSerializerFormat(SupportFaults = true)]
  FetchMyThingResponse FetchMyThing(FetchMyThingRequest request);
}

我为我的 WCF 服务启用了服务跟踪,并看到以下异常似乎证实了我的假设:

Activity: Listen at 'http://mycomputer/MyWs.svc
<Exception>
  <ExceptionType>System.ServiceModel.ProtocolException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
  <Message>Content Type application/soap+xml;charset=UTF-8;action="http://tempuri.org/FetchMyThing" was sent to a service expecting text/xml; charset=utf-8.  The client and service bindings may be mismatched.   
  </Message>
(erroneous detail snipped)
</Exception>

因此,如果相信此消息,我的合同和服务绑定可能不匹配,但根据我对 WCF 的理解,我的配置(或至少其背后的意图)是正确的。

有人对我的配置有什么问题有任何想法吗?

4

3 回答 3

9

我唯一能想到的是,因为您没有详细指定绑定,并且它使用 HTTP(根据此:“Listen at ' http://mycomputer/MyWs.svc '”)然后它是否为此使用默认值(即basicHttpBinding),这会造成不匹配?

于 2012-05-02T06:55:54.260 回答
1

当我的服务有多个绑定时,我遇到了同样的问题。当我删除所有绑定并且只留下一个未命名的绑定时,错误消息消失了。

于 2014-10-18T19:23:10.393 回答
0

请查看此链接How to: Configure WCF Service to Interoperate with ASP.NET Web Service Clients

若要将 Windows Communication Foundation (WCF) 服务终结点配置为与 ASP.NET Web 服务客户端可互操作,请使用 System.ServiceModel。BasicHttpBinding类型作为服务端点的绑定类型。

此外,定义两个端点,您可以使用同一服务的 HTTP 和 HTTPS 版本

于 2015-09-07T21:41:32.953 回答