Search code examples
jsonbindingwsdlservice-reference

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


I am getting this error message when trying to receive data back from a web service.

Here is my code:

ChannelFactory<IInterface> factory = new ChannelFactory<IInterface>(new BasicHttpBinding(), new EndpointAddress("http://example.net/MyService.svc/Test"));
var client = factory.CreateChannel();

MyObj x = client.Test();

Although I get the error, I can see the response (a JSON string) in the error message. I tried changing the Binding to WebHttpBinding, as well as adding an endpoint behavior of WebHttpBehavior, but this simply returns a null object.


Solution

  • I solved it. Initially I was correct in using WebHttpBinding, however for the endpoint behavior I needed to modify it slightly. Heres the working code:

    ChannelFactory<IInterface> factory = new ChannelFactory<IInterface>(new WebHttpBinding(), new EndpointAddress("http://example.net/MyService.svc/Test"));
    
    WebHttpBehavior behavior = new WebHttpBehavior()
    { 
        DefaultOutgoingResponseFormat = WebMessageFormat.Json,
        DefaultBodyStyle = WebMessageBodyStyle.Wrapped,
        HelpEnabled = true,
        DefaultOutgoingRequestFormat = WebMessageFormat.Json
    };
    
    factory.Endpoint.Behaviors.Add(behavior);
    
    var client = factory.CreateChannel();
    
    MyObj x = client.Test();