![](/img/trans.png)
[英]how to make wcf service listen for soap 1.1 and soap 1.2 (in code)
[英]WCF SOAP 1.2 service expecting SOAP 1.1 content type
我正在構建一個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的理解我的配置(或至少其背后的意圖)是正確的。
有沒有人對我的配置有什么問題?
我唯一能想到的就是因為你沒有在很多細節中指定一個綁定,並且它使用HTTP(根據這個:“收聽' http://mycomputer/MyWs.svc '”)然后它是否使用默認值(即basicHttpBinding
)來創建不匹配?
當我的服務有多個綁定時,我遇到了同樣的問題。 當我刪除所有綁定並且只留下一個未命名的綁定時,錯誤消息消失了。
請檢查此鏈接如何:配置WCF服務以與ASP.NET Web服務客戶端進行互操作 。
若要將Windows Communication Foundation(WCF)服務端點配置為可與ASP.NET Web服務客戶端進行互操作,請使用System.ServiceModel。 BasicHttpBinding類型作為服務端點的綁定類型。
此外,定義兩個端點可以使用同一服務的HTTP和HTTPS版本
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.