WCF SOAP 1.2服务期望SOAP 1.1内容类型。

22

我正在构建一个WCF Web服务,需要与非WCF客户端进行交互(事实上,没有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的了解来看,我的配置(或至少其背后的意图)是正确的。是否有人对我的配置有什么错误的想法?
3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
9
我能想到的唯一原因是,由于您没有详细指定绑定方式,并且它正在使用HTTP(如此处所示:“在 'http://mycomputer/MyWs.svc' 上侦听”),那么它是否正在使用默认的(即basicHttpBinding),从而导致不匹配?

3
是的!问题最终在于我的.svc文件(markup)中指定的名称属性和web.config中的服务名称不匹配。我看到运行时异常是因为我的配置有时无效 - 但这并不意味着它实际上被映射到了配置! - Stefan Mohr
@StefanMohr,你能告诉我你具体修复了什么使它工作吗? - I.P.
已经过去10年了,我仍然记得为解决这个问题而奋斗!在我的情况下,这实际上是SVC声明(其中我命名了服务)和web.config的system.serviceModel部分中的服务名称属性之间的字符串不匹配。我曾经认为web.config中的名称只是我自己使用的友好名称,但它必须与SVC中的名称完全匹配,才能精确地获取该服务。 - Stefan Mohr

1

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


0
请查看此链接 如何配置WCF服务以与ASP.NET Web服务客户端互操作

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,