Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF REST Service not accepting JSON in .Net 4

Tags:

json

.net

wcf

I've been trying out the StandardEndpoints that were introduced as part of .Net 4 and I'm getting the most peculiar of errors.

My code

[ServiceContract]
public interface IAuthenticator
{
    [OperationContract]
    [WebInvoke(UriTemplate = "AuthenticateUser", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
    AuthPacket AuthenticateUser(string Username, string Password, string DeviceId);
}

My web.config

<system.web>
  <compilation debug="true" targetFramework="4.0" />
</system.web>

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
  </modules>
</system.webServer>

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  <standardEndpoints>
    <webHttpEndpoint>
      <!--
          Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
          via the attributes on the <standardEndpoint> element below
      -->
      <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
    </webHttpEndpoint>
  </standardEndpoints>
</system.serviceModel>

The exception that is driving me crazy!

415 Cannot process the message because the content type 'application/json' was not 
the expected type 'text/xml; charset=utf-8'.

I can make the problem going away by going back to the .Net 3.5 standard of declaring each service, but, unless I am mistaken, one of the major upgrades in WCF with .Net 4 was it's ability to handle stuff like this. Am I doing something wrong?

like image 928
thaBadDawg Avatar asked Feb 15 '11 16:02

thaBadDawg


1 Answers

If I read this operation contract correctly, you have defined JSON to be your response format - but not your request format:

[ServiceContract]
public interface IAuthenticator
{
    [OperationContract]
    [WebInvoke(UriTemplate = "AuthenticateUser", Method = "POST", 
     BodyStyle = WebMessageBodyStyle.WrappedRequest, 
     ResponseFormat = WebMessageFormat.Json)]
    AuthPacket AuthenticateUser(string Username, string Password, string DeviceId);
}

Could that be the problem?? What happens if you add RequestFormat = WebMessageFormat.Json to your operation contract ??

[ServiceContract]
public interface IAuthenticator
{
    [OperationContract]
    [WebInvoke(UriTemplate = "AuthenticateUser", Method = "POST", 
     BodyStyle = WebMessageBodyStyle.WrappedRequest, 
     RequestFormat = WebMessageFormat.Json,   <== ADD THIS HERE TO YOUR CODE !
     ResponseFormat = WebMessageFormat.Json)]
    AuthPacket AuthenticateUser(string Username, string Password, string DeviceId);
}

Update: if you're using WCF 4 "out of the box", its protocol mapping will associate the http:// scheme with basicHttpBinding.

To fix this, you need to override the default protocol mapping like this (in your web.config):

Default Protocol Mapping

The answer to this question is simple. WCF defines a default protocol mapping between transport protocol schemes (e.g., http, net.tcp, net.pipe, etc) and the built-in WCF bindings. The default protocol mapping is found in the .NET 4 machine.config.comments file and it looks like this:

<system.serviceModel>
   <protocolMapping>
       <add scheme="http" binding="webHttpBinding" bindingConfiguration="" />
   </protocolMapping>

Now, by default http://..... would be mapped to webHttpBinding.

(taken from: A Developer's Introduction to Windows Communication Foundation 4 )

like image 83
marc_s Avatar answered Nov 15 '22 19:11

marc_s