如何为WCF ServiceContract设置默认的RequestFormat?

Rya*_*son 8 c# wcf json webhttpbinding

我正在编写一个有很多方法的Web服务.它们都设置类似于以下内容:

[OperationContract]
    [WebInvoke(
        BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "x/y/z")]
    void someMethod(int x, int y, int z);
Run Code Online (Sandbox Code Playgroud)

我想要做的只是在web.config文件中设置默认的BodyStyle/ RequestFormat/ ResponseFormatall.现在,我知道我可以这样做:

  <endpointBehaviors>
    <behavior name="webHttpBehavior">
      <webHttp defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" />
    </behavior>
  </endpointBehaviors>
Run Code Online (Sandbox Code Playgroud)

但似乎没有RequestFormat的属性.如何将默认设置RequestFormat为JSON?

Sli*_*SFT 5

请求类型由WCF自动解释,您无需RequestFormat为服务操作指定默认值.

如果您尝试强制执行支持的请求格式,请参阅有关强制执行请求内容类型的相关SO帖子.

注意:RequestFormatWebGet操作分配一个没有意义.根据定义,a WebGet不能包含BodyJSON格式存在的位置.这里有一个更好的例子WebInvoke.

  • 我从你的链接找到一个有趣的小问题:"如果在操作中没有指定默认格式,则使用DefaultOutgoingResponseFormat属性的值." 所以基本上,如果海报没有指定内容类型,并且操作上没有RequestFormat,它实际上会从defaultOutgoingResponseFormat中获取格式.有趣. (3认同)