响应消息的WCF charset=utf-8不匹配绑定的内容类型(应用程序/soap+xml;charset = utf - 8)

时间:2022-11-13 14:37:55

I'm hosting a WCF service in IIS 7.5 using .NET 4.0. I also have a WPF application that I am using as my client that was built with Visual Studio 2010 and .NET 4.0. I added my service reference and when I attempt to call a function, I get the following exception

我使用。net 4.0在IIS 7.5中托管WCF服务。我还有一个WPF应用程序,我正在使用它作为我的客户端,它是用Visual Studio 2010和。net 4.0构建的。我添加了我的服务引用,当我尝试调用一个函数时,我得到了下面的异常。

The content type application/xml; charset=utf-8 of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8)

内容类型application / xml;响应消息的charset=utf-8与绑定的内容类型(应用程序/soap+xml)不匹配;charset = utf - 8)

I am able to navigate to the service in a web browser and my bindings appear to be the same between the client and service (WsHttp bindings).

我能够在web浏览器中导航到服务,而我的绑定在客户机和服务(WsHttp绑定)之间似乎是相同的。

I know there are alot of google results about this error but none of them seemed to be relevant/help my specific problem. I tried installing Non-HTTP Activation features as well as a wide variety of other small tricks. Anybody be able to help? Thanks

我知道关于这个错误有很多谷歌的结果,但是没有一个看起来是相关的/帮助我的具体问题。我尝试安装非http激活特性以及各种各样的其他小技巧。有人能帮忙吗?谢谢

edit, here are my configs (they are quite lengthy)

编辑,这是我的配置文件(它们相当长)

Client

客户端

<?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>

and Server

和服务器

<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>

7 个解决方案

#1


20  

As I suspected - your client-side config looks like this:

正如我所怀疑的——您的客户端配置是这样的:

  <endpoint name="WSHttpBinding_ICommAccountingBinding" 
       address="https://secure.inmatecanteen.com/CommAccountingService/CommAccountingWeb.svc"
       binding="wsHttpBinding" 
       bindingConfiguration="WSHttpBinding_IInmateCanteenServiceWeb"
       contract="CommAccountingWeb.ICommAccountingWeb" />

It expects wsHttpBinding - but the server-side address it's connecting to is:

它期望wsHttpBinding—但是它连接到的服务器端地址是:

 <service name="CommAccountingWeb.CommAccountingWeb"  
          behaviorConfiguration="HttpMexBehavior">
     <endpoint 
         address="" 
         behaviorConfiguration="httpBehavior" 
         binding="webHttpBinding" bindingConfiguration="myWebHttpBinding" 
         contract="CommAccountingWeb.ICommAccountingWeb" />
     <host>
        <baseAddresses>
           <add baseAddress="https://secure.inmatecanteen.com/CommAccountingService/CommAccountingWeb.svc"></add>
        </baseAddresses>
     </host>
 </service>

and this server endpoint uses webHttpBinding.

这个服务器端点使用webHttpBinding。

So while the client expects a SOAP XML message (content type: application/soap+xml; charset=utf-8), the server-side endpoint is a REST endpoint which returns plain XML (content type: application/xml; charset=utf-8)

因此,客户端期望SOAP XML消息(内容类型:应用程序/ SOAP + XML;服务器端端点是一个REST端点,它返回纯XML(内容类型:应用程序/ XML;charset = utf - 8)

Solution: you need to make sure both the client and the server endpoint used are in sync with regards to bindings and configuration!

解决方案:您需要确保使用的客户机和服务器端点在绑定和配置方面是同步的!

#2


3  

I came across a similar error while creating a client service to one of the existing server side WebService. I could rectify it using SOAP 1.1 transport protocol on the client. Somehow soap 1.2 is giving/expecting a different format. This trace back to the difference between BasicHttpBinding vs WebHttpBinding vs WsHttpBinding.

在为现有的服务器端WebService创建客户机服务时,遇到了类似的错误。我可以在客户机上使用SOAP 1.1传输协议来纠正它。在某种程度上,soap 1.2提供/期待一种不同的格式。这可以追溯到BasicHttpBinding与WebHttpBinding与WsHttpBinding之间的区别。

#3


3  

As one of the comments says in this question, Add ?wsdl to your client's endpoint address, and you should have more luck with the service. ?wsdl is important - it means the browser is just getting "Web Services Description Language" for the service, and not calling the service.

正如在这个问题中的一个评论所说,将wsdl添加到您的客户端的端点地址,您应该在服务中获得更多的运气。?wsdl很重要——它意味着浏览器只是获得了服务的“Web服务描述语言”,而不是调用服务。

I had the same issue and adding ?wsdl solved my headache.

我也有同样的问题,并添加了wsdl解决了我的头疼问题。

#4


0  

I got this problem after I added a method that returned a collection of instances of a base class that didn't have a [KnownType] attribute that would resolve to a concrete instance. With the [KnownType] attribute in place the problem disappeared.

我在添加了一个方法后得到了这个问题,该方法返回了一个基类的实例集合,它没有一个将解析为具体实例的[KnownType]属性。有了[KnownType]属性,问题就消失了。

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

[DataContract]
// [KnownType(typeof(RealItemA))] <--- without these attributes you will get a problem
// [KnownType(typeof(RealItemB))]
public class ItemBase {
}

[DataContract]
public class RealItemA : ItemBase {
}

[DataContract]
public class RealItemB : ITemBase {
}

#5


0  

in my case same error was caused by missing

在我的案例中,同样的错误是由于缺少。

[datacontract] 
[datamember]

attributes in returned data type.

返回数据类型的属性。

Error message was really misleading.

错误信息是很误导人的。

#6


0  

Make sure , you have not made changes in bindings, contract and endpoints generated in app.config on addition of service reference.

请确保,您没有在添加服务引用时在app.config中生成的绑定、契约和端点进行更改。

Add MessageEncoding as Mtom in .NET code and then check if you still get the message.

在。net代码中添加MessageEncoding作为Mtom,然后检查是否仍然得到消息。

I have tried explaining creating a consumption of wcf service using soap protocol for both wshttpbinding and basichttpbinding and issues faced here : http://www.writeulearn.com/consume-wcf-soap-service-csharp/ Please go through it.

我已经尝试过使用soap协议来解释创建wcf服务的使用,它既适用于wshttpbinding和basichttpbinding,也适用于这里的问题:http://www.writeulearn.com/consume-wcf-soap-service-csharp/请浏览一下。

#7


0  

I have this error due to Different version of Syste.Web.MVC DLL. You can refer answer Here in another stack overflow question.

由于Syste.Web的不同版本,我有这个错误。MVC DLL。您可以在另一个堆栈溢出问题中引用答案。

#1


20  

As I suspected - your client-side config looks like this:

正如我所怀疑的——您的客户端配置是这样的:

  <endpoint name="WSHttpBinding_ICommAccountingBinding" 
       address="https://secure.inmatecanteen.com/CommAccountingService/CommAccountingWeb.svc"
       binding="wsHttpBinding" 
       bindingConfiguration="WSHttpBinding_IInmateCanteenServiceWeb"
       contract="CommAccountingWeb.ICommAccountingWeb" />

It expects wsHttpBinding - but the server-side address it's connecting to is:

它期望wsHttpBinding—但是它连接到的服务器端地址是:

 <service name="CommAccountingWeb.CommAccountingWeb"  
          behaviorConfiguration="HttpMexBehavior">
     <endpoint 
         address="" 
         behaviorConfiguration="httpBehavior" 
         binding="webHttpBinding" bindingConfiguration="myWebHttpBinding" 
         contract="CommAccountingWeb.ICommAccountingWeb" />
     <host>
        <baseAddresses>
           <add baseAddress="https://secure.inmatecanteen.com/CommAccountingService/CommAccountingWeb.svc"></add>
        </baseAddresses>
     </host>
 </service>

and this server endpoint uses webHttpBinding.

这个服务器端点使用webHttpBinding。

So while the client expects a SOAP XML message (content type: application/soap+xml; charset=utf-8), the server-side endpoint is a REST endpoint which returns plain XML (content type: application/xml; charset=utf-8)

因此,客户端期望SOAP XML消息(内容类型:应用程序/ SOAP + XML;服务器端端点是一个REST端点,它返回纯XML(内容类型:应用程序/ XML;charset = utf - 8)

Solution: you need to make sure both the client and the server endpoint used are in sync with regards to bindings and configuration!

解决方案:您需要确保使用的客户机和服务器端点在绑定和配置方面是同步的!

#2


3  

I came across a similar error while creating a client service to one of the existing server side WebService. I could rectify it using SOAP 1.1 transport protocol on the client. Somehow soap 1.2 is giving/expecting a different format. This trace back to the difference between BasicHttpBinding vs WebHttpBinding vs WsHttpBinding.

在为现有的服务器端WebService创建客户机服务时,遇到了类似的错误。我可以在客户机上使用SOAP 1.1传输协议来纠正它。在某种程度上,soap 1.2提供/期待一种不同的格式。这可以追溯到BasicHttpBinding与WebHttpBinding与WsHttpBinding之间的区别。

#3


3  

As one of the comments says in this question, Add ?wsdl to your client's endpoint address, and you should have more luck with the service. ?wsdl is important - it means the browser is just getting "Web Services Description Language" for the service, and not calling the service.

正如在这个问题中的一个评论所说,将wsdl添加到您的客户端的端点地址,您应该在服务中获得更多的运气。?wsdl很重要——它意味着浏览器只是获得了服务的“Web服务描述语言”,而不是调用服务。

I had the same issue and adding ?wsdl solved my headache.

我也有同样的问题,并添加了wsdl解决了我的头疼问题。

#4


0  

I got this problem after I added a method that returned a collection of instances of a base class that didn't have a [KnownType] attribute that would resolve to a concrete instance. With the [KnownType] attribute in place the problem disappeared.

我在添加了一个方法后得到了这个问题,该方法返回了一个基类的实例集合,它没有一个将解析为具体实例的[KnownType]属性。有了[KnownType]属性,问题就消失了。

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

[DataContract]
// [KnownType(typeof(RealItemA))] <--- without these attributes you will get a problem
// [KnownType(typeof(RealItemB))]
public class ItemBase {
}

[DataContract]
public class RealItemA : ItemBase {
}

[DataContract]
public class RealItemB : ITemBase {
}

#5


0  

in my case same error was caused by missing

在我的案例中,同样的错误是由于缺少。

[datacontract] 
[datamember]

attributes in returned data type.

返回数据类型的属性。

Error message was really misleading.

错误信息是很误导人的。

#6


0  

Make sure , you have not made changes in bindings, contract and endpoints generated in app.config on addition of service reference.

请确保,您没有在添加服务引用时在app.config中生成的绑定、契约和端点进行更改。

Add MessageEncoding as Mtom in .NET code and then check if you still get the message.

在。net代码中添加MessageEncoding作为Mtom,然后检查是否仍然得到消息。

I have tried explaining creating a consumption of wcf service using soap protocol for both wshttpbinding and basichttpbinding and issues faced here : http://www.writeulearn.com/consume-wcf-soap-service-csharp/ Please go through it.

我已经尝试过使用soap协议来解释创建wcf服务的使用,它既适用于wshttpbinding和basichttpbinding,也适用于这里的问题:http://www.writeulearn.com/consume-wcf-soap-service-csharp/请浏览一下。

#7


0  

I have this error due to Different version of Syste.Web.MVC DLL. You can refer answer Here in another stack overflow question.

由于Syste.Web的不同版本,我有这个错误。MVC DLL。您可以在另一个堆栈溢出问题中引用答案。