WCF服务错误 - 传入消息具有意外的消息格式' Raw'。预期的消息格式是' Xml' Json'

时间:2014-09-30 14:42:18

标签: wcf rest post

我想以jason格式将数据发送到wcf服务进行处理。开发了Wcf服务,当使用fiddler将jason输入发送到服务时,它会抛出错误 - 服务器在处理请求时遇到错误。异常消息是'传入消息具有意外的消息格式'Raw'。操作的预期消息格式是'Xml','Json'。这可能是因为尚未在绑定上配置WebContentTypeMapper。有关更多详细信息,请参阅WebContentTypeMapper的文档。有关详细信息,请参阅服务器日志。

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

如果我使用wcftestclient(调试模式),它运行正常 - 猜它使用soap / xml。

2 个答案:

答案 0 :(得分:20)

请求的内容类型应为application / json ,而不是application / jason 。尝试改变它,它应该工作。

答案 1 :(得分:0)

如果内容类型映射器返回的内容与RequestFormat不匹配,则在上述更正后仍会出现此问题。

关于你的装订,如果你有

contentTypeMapper="Abc.Service.NewtonsoftCustom.CustomContentTypeMapper

验证方法

CustomContentTypeMapper(string contentType)

返回匹配说

RequestFormat =WebMessageFormat.Json 

在您的operationcontract