Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF service error - Incoming message has an unexpected message format 'Raw'. Expected message formats are 'Xml', 'Json'

Tags:

rest

post

wcf

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.

like image 687
bdotnet Avatar asked Sep 30 '14 14:09

bdotnet


1 Answers

The content type of the request should be application/json, not application/jason. Try changing that and it should work.

like image 135
carlosfigueira Avatar answered Oct 19 '22 08:10

carlosfigueira