Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何使我的WCF Rest方法在使用WebInvoke时不需要内容类型_Json_Wcf_Rest - Fatal编程技术网

Json 如何使我的WCF Rest方法在使用WebInvoke时不需要内容类型

Json 如何使我的WCF Rest方法在使用WebInvoke时不需要内容类型,json,wcf,rest,Json,Wcf,Rest,我有一个WCF REST服务,其操作如下: [OperationContract] [WebInvoke(UriTemplate = "/User", BodyStyle = WebMessageBodyStyle.Bare, Method = "POST", RequestFormat = WebMessageFormat.Json)] void User(User user); 当我从Fiddler调用它时,如果我像这样指定Content Type=“application/json”,它

我有一个WCF REST服务,其操作如下:

[OperationContract]
[WebInvoke(UriTemplate = "/User", BodyStyle = WebMessageBodyStyle.Bare, Method = "POST", RequestFormat = WebMessageFormat.Json)]
void User(User user);
当我从Fiddler调用它时,如果我像这样指定Content Type=“application/json”,它就可以正常工作:

但是如果排除内容类型,则会出现错误400,因为它试图将请求正文作为XML处理。这很烦人,您可能真的认为设置RequestFormat=WebMessageFormat.Json可以使我不必指定内容类型,但事实并非如此。事实上,如果我删除'RequestFormat',一切都不会改变。我也尝试过对WebMessageBodyStyle进行“包装”,但随后DTO通过null

为了澄清,如果我在文章的正文中也使用XML(并省略内容类型),就会发生这种情况。。。因此,我真正想要实现的是:

如何使我的WCF Rest方法在使用WebInvoke时不需要内容类型(我希望WCF能够自动解决这个问题)


这让我快疯了,请帮帮我。

您是否尝试将端点行为上的
AutomaticFormatSelectEnabled
设置为true

考虑到这个属性是Net4.0特有的,我记得我使用它来根据http的Accept头格式化响应消息

我没有尝试重现您的场景,但在阅读此官方文档时,它说明了以下内容:

如果请求消息包含Accept标头,则Windows 通信基础(WCF)基础设施搜索一种类型 它支持。如果Accept标头指定了其介质的优先级 类型,他们很荣幸。如果在Accept中找不到合适的格式 标头,则使用请求消息的内容类型如果否 指定了合适的内容类型,即 已使用操作。

下面是msdn中如何设置AutomaticFormatSelectEnabled的示例

<behaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp automaticFormatSelectionEnabled="true" />
    </behavior>
  </endpointBehaviors>
</behaviors>

您需要在WebHttpBinding上添加WebContentTypeMapper。在这种情况下,您可以告诉WCF运行时提供的(或假定的)内容类型mime值是什么WebContentFormat。通常,当接收到没有内容类型标题的帖子时,这将是“application/x-www-form-urlencode”(也可能发生多部分/表单数据或text/plain,请注意)。只需告诉WCF这是Json或Xml(或任何您需要的),它就可以正常工作(您的单位可能会有所不同)

只需将WebHttpBinding上的ContentTypeMapper设置为类的实例(这可以在配置文件中完成,也可以在具有ContentTypeMapper属性的绑定中完成)


更多信息=>

感谢您的回复。是的,我确实有这个设置,但它似乎对请求没有影响-如果我省略ContentType,那么我会得到一个400错误:异常消息是“传入消息具有意外的消息格式”“Raw”。操作的预期消息格式为“Xml”、“Json”。
<behaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp automaticFormatSelectionEnabled="true" />
    </behavior>
  </endpointBehaviors>
</behaviors>
    public class YourContentTypeMapper : WebContentTypeMapper
{
    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        if (contentType == "application/x-www-form-urlencode")
        {
            return WebContentFormat.Json; // assuming this is wanted
        }
        else
        {
            return WebContentFormat.Default;
        }
    }
}
var binding = new WebHttpBinding();
binding.ContentTypeMapper = new YourContentTypeMapper();