I want to send data in jason format to a wcf service for processing. Wcf service is developed and when jason input is sent to the service using fiddler, it throws the error - The server encountered an error processing the request. The exception message is 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details.
Service contract
================
public interface IRegisterEmployee
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Json, UriTemplate = "AddEmployee")]
bool ProcessEmployee(Employee emp);
}
[DataContract]
public class Employee
{
[DataMember]
public string emp { get; set; } //this is actually a complex type, but simplified here
}
Service class
============
public class RegisterEmployee : IRegisterEmployee
{
public bool ProcessEmployee(Employee emp)
{
//do some processing
return true;
}
Web.config
=========
<services>
<service name="Project.RegisterEmployee">
<endpoint address="Rest" behaviorConfiguration="RestfulBehavior" binding="webHttpBinding" name="Rest" contract="Project.IRegisterEmployee" />
<endpoint address="Soap" behaviorConfiguration="" binding="basicHttpBinding" name="Soap" contract="Project.IRegisterEmployee" />
<endpoint address="Mex" behaviorConfiguration="" binding="mexHttpBinding" name="Mex" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/Project" />
</baseAddresses>
</host>
</service>
</services>
<endpointBehaviors>
<behavior name="RestfulBehavior">
<webHttp automaticFormatSelectionEnabled="true" />
</behavior>
</endpointBehaviors>
*Fiddler
======
POST; http://localhost/Project/RegisterEmployee.svc/Rest/AddEmployee
Content-Type: application/jason
Request Body = {"emp" : "test"}*
Error - HTTP/1.1 400 Bad Request
If I use wcftestclient (debug mode), it works fine - guess it uses soap/xml.
This issue will still occur after the above correction IF there is a mismatch between what the content type mapper is returning and the RequestFormat.
On your binding, if you have
verify that what the method
returns matches say
on your operationcontract
The content type of the request should be application/json, not application/jason. Try changing that and it should work.