応答メッセージのWCF charset=utf-8がバインディングのコンテンツタイプ(application/soap+xml; charset=utf-8)と一致しません。

私は、IIS 7.5で.NET 4.0を使用してWCFサービスをホストしています。 また、クライアントとして使用しているWPFアプリケーションは、Visual Studio 2010と.NET 4.0で構築されています。 サービス参照を追加し、関数を呼び出そうとすると、次の例外が発生します。

応答メッセージのコンテンツタイプ application/xml; charset=utf-8 は、バインディングのコンテンツタイプ (application/soap+xml; charset=utf-8) に一致しません。

Web ブラウザでサービスに移動でき、クライアントとサービスの間でバインディングは同じように見えます (WsHttp バインディング)。

このエラーについて多くのグーグル検索結果があることは知っていますが、どれも私の特定の問題に関連または役立つとは思えません。 非HTTPアクティベーション機能のインストールや、その他様々な小細工も試してみました。 どなたか助けていただけませんか? ありがとうございます。

edit, 以下は私の設定です (かなり長いです)

クライアント

<?xml version="1.0"?>
<configuration>
   <system.serviceModel>
      <bindings>
         <basicHttpBinding>
             <binding name="ContentSoap" 
                 closeTimeout="00:01:00" openTimeout="00:01:00"
                 receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
                 maxBufferSize="2147483647" maxBufferPoolSize="2147483647" 
                 maxReceivedMessageSize="2147483647"
                 messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                 useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" 
                              maxArrayLength="2147483647"
                              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                               realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
             </binding>
             <binding name="OrderSoap" 
                 closeTimeout="00:01:00" openTimeout="00:01:00"
                 receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                 maxBufferSize="65536" maxBufferPoolSize="524288" 
                 maxReceivedMessageSize="65536"
                 messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                 useDefaultWebProxy="true">
               <readerQuotas maxDepth="32" maxStringContentLength="8192" 
                             maxArrayLength="16384" maxBytesPerRead="4096" 
                             maxNameTableCharCount="16384" />
               <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                               realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
               </security>
           </binding>
        </basicHttpBinding>
        <netTcpBinding>
           <binding name="NetTcpBindingEndpoint" closeTimeout="00:01:00"
               openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
               transactionFlow="false" transferMode="Buffered"  
               transactionProtocol="OleTransactions"
               hostNameComparisonMode="StrongWildcard" listenBacklog="10"  
               maxBufferPoolSize="2147000000" maxBufferSize="65536" maxConnections="10" 
               maxReceivedMessageSize="2147000000">
              <readerQuotas maxDepth="32" maxStringContentLength="8192"  
                   maxArrayLength="2147000000" maxBytesPerRead="4096" 
                   maxNameTableCharCount="16384" />
              <reliableSession ordered="true" inactivityTimeout="00:01:00"
                               enabled="true" />
              <security mode="None">
                 <transport clientCredentialType="Windows" 
                            protectionLevel="EncryptAndSign" />
                 <message clientCredentialType="Windows" />
              </security>
           </binding>
       </netTcpBinding>
       <wsHttpBinding>
            <binding name="WSHttpBinding_IInmateCanteenServiceWeb" 
                 closeTimeout="00:01:00" openTimeout="00:01:00" 
                 receiveTimeout="00:10:00" sendTimeout="00:01:00"
                 bypassProxyOnLocal="false" transactionFlow="false"  
                 hostNameComparisonMode="StrongWildcard"
                 maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" 
                 messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
                 allowCookies="false">
              <readerQuotas maxDepth="32" maxStringContentLength="8192" 
                   maxArrayLength="16384" maxBytesPerRead="4096" 
                   maxNameTableCharCount="16384" />
              <reliableSession ordered="true" inactivityTimeout="00:10:00"
                   enabled="false" />
              <security mode="Transport">
                  <transport clientCredentialType="None" proxyCredentialType="None"
                             realm="" />
              </security>
          </binding>
      </wsHttpBinding>
   </bindings>
   <client>
       <endpoint name="ContentSoap"
           address="http://media.team.twvending.net/storeservices/content.asmx"
           binding="basicHttpBinding" bindingConfiguration="ContentSoap"
           contract="MediaPortContent.ContentSoap"  />
       <endpoint name="OrderSoap"
           address="http://media.team.twvending.net/storeservices/order.asmx"
           binding="basicHttpBinding" bindingConfiguration="OrderSoap"
           contract="MediaPortOrder.OrderSoap"  />
       <endpoint name="NetTcpBindingEndpoint" 
           address="..."
           binding="netTcpBinding" bindingConfiguration="NetTcpBindingEndpoint"
           contract="WebCallBack.ICallbackService" />
       <endpoint name="WSHttpBinding_IInmateCanteenServiceWeb" 
           address="..."
           binding="wsHttpBinding"  
           bindingConfiguration="WSHttpBinding_IInmateCanteenServiceWeb"
           contract="InmateCanteenWeb.IInmateCanteenServiceWeb" />
       <endpoint name="WSHttpBinding_ICommAccountingBinding" 
           address="..."
           binding="wsHttpBinding" 
           bindingConfiguration="WSHttpBinding_IInmateCanteenServiceWeb"
           contract="CommAccountingWeb.ICommAccountingWeb" />
   </client>
</system.serviceModel>

とサーバー

<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="httpBehavior">
                <!--<webHttp />-->
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
           <behavior name="ServiceBehavior">
              <serviceCredentials>
                  <clientCertificate>
                     <authentication revocationMode="NoCheck" />
                  </clientCertificate>
                  <serviceCertificate findValue="CN=secure.inmatecanteen.com" />
              </serviceCredentials>
              <serviceMetadata httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
           </behavior>
           <behavior name="MexBehavior">
               <serviceMetadata httpsGetEnabled="true" />
               <serviceDebug includeExceptionDetailInFaults="true" />
           </behavior>
           <behavior name="HttpMexBehavior">
                <serviceMetadata httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
           </behavior>
           <behavior name="BasicHttpMexBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
           </behavior>
           <behavior name="">
                <serviceMetadata httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
           </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="false" 
                                 multipleSiteBindingsEnabled="true" />
      <bindings>
         <wsHttpBinding>
            <binding name="myWsHttpBinding" maxBufferPoolSize="2147483647" 
                maxReceivedMessageSize="2147483647" messageEncoding="Text">
                <readerQuotas maxDepth="32" maxStringContentLength="2147483647" 
                       maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                       maxNameTableCharCount="2147483647" />
                <security mode="Transport">
                    <transport clientCredentialType="None" />
                </security>
            </binding>
         </wsHttpBinding>
         <webHttpBinding>
             <binding name="myWebHttpBinding">
                <security mode="Transport">
                   <transport clientCredentialType="None" proxyCredentialType="None" 
                             realm=""  />
                </security>
            </binding>
         </webHttpBinding>
         <basicHttpBinding>
             <binding name="myBasicHttpBinding" maxBufferPoolSize="2147483647" 
                 maxReceivedMessageSize="2147483647">
                 <readerQuotas maxDepth="32" maxStringContentLength="2147483647" 
                        maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                        maxNameTableCharCount="2147483647" />
             </binding>
        </basicHttpBinding>
     </bindings>
     <services>
         <service name="CommAccountingWeb.CommAccountingWeb"  
                  behaviorConfiguration="HttpMexBehavior">
             <endpoint 
                 address="" 
                 behaviorConfiguration="httpBehavior" 
                 binding="webHttpBinding" bindingConfiguration="myWebHttpBinding" 
                 contract="CommAccountingWeb.ICommAccountingWeb" />
             <endpoint 
                 address="mex"
                 binding="mexHttpsBinding" 
                 contract="IMetadataExchange"  />
             <host>
                <baseAddresses>
                    <add baseAddress="..."></add>
                </baseAddresses>
             </host>
         </service>
         <service name="CommAccountingWeb.CommAccountingBasic" 
                  behaviorConfiguration="BasicHttpMexBehavior">
            <endpoint 
                address="" 
                binding="basicHttpBinding" 
                bindingConfiguration="myBasicHttpBinding" 
                contract="CommAccountingWeb.ICommAccountingBasic" />
            <endpoint 
                address="mex"
                binding="mexHttpBinding"  
                contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                  <add baseAddress="..." />
              </baseAddresses>
           </host>
        </service>
    </services>
</system.serviceModel>
ソリューション

やはり、クライアント側の設定はこのようになっているようですね。

これは wsHttpBinding を期待するものですが、接続先のサーバー側のアドレスは wsHttpBinding です。








で、このサーバーエンドポイントは webHttpBinding を使用しています。

つまり、クライアントは SOAP XML メッセージ (content type: application/soap+xml; charset=utf-8) を期待しますが、サーバー側のエンドポイントは REST エンドポイントで、プレーン XML (content type: application/xml; charset=utf-8) を返します。

解決策:使用するクライアントエンドポイントとサーバーエンドポイントの両方が、バインディングと設定に関して同期していることを確認する必要があります。

解説 (0)

私は、具象インスタンスに解決する [KnownType] 属性を持たない基底クラスのインスタンスのコレクションを返すメソッドを追加した後に、この問題が発生しました。 KnownType]属性があることで、この問題は解消されました。


[ServiceContract]
public interface IService {
    [OperationContract]
    IEnumerable GetItems();
}

[DataContract]
// [KnownType(typeof(RealItemA))] 
解説 (0)

私の場合、同じエラーは

[datacontract] 
[datamember]

属性が返されます。

エラーメッセージは本当に誤解を招くものでした。

解説 (0)