WCF service error - Incoming message has an unexpe

2019-06-16 16:05发布

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.

标签: wcf rest post
2条回答
成全新的幸福
2楼-- · 2019-06-16 16:30

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

contentTypeMapper="Abc.Service.NewtonsoftCustom.CustomContentTypeMapper

verify that what the method

CustomContentTypeMapper(string contentType)

returns matches say

RequestFormat =WebMessageFormat.Json 

on your operationcontract

查看更多
SAY GOODBYE
3楼-- · 2019-06-16 16:31

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

查看更多
登录 后发表回答