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

后端 未结 5 629
误落风尘
误落风尘 2020-12-06 00:09

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 add

相关标签:
5条回答
  • 2020-12-06 00:42

    in my case same error was caused by missing

    [datacontract] 
    [datamember]
    

    attributes in returned data type.

    Error message was really misleading.

    0 讨论(0)
  • 2020-12-06 00:45

    As Steven Westbrook says in a comment on this answer:

    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.

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

    0 讨论(0)
  • 2020-12-06 00:55

    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:

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

    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)

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

    0 讨论(0)
  • 2020-12-06 01:01

    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.

    [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 {
    }
    
    0 讨论(0)
  • 2020-12-06 01:04

    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.

    0 讨论(0)
提交回复
热议问题