我在写一个有很多方法的web服务。他们都成立了类似以下内容:如何为WCF ServiceContract设置默认的RequestFormat?
[OperationContract]
[WebInvoke(
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "x/y/z")]
void someMethod(int x, int y, int z);
我想要做的就是设置默认BodyStyle
/RequestFormat
/ResponseFormat
所有在web.config文件中。现在,我知道我可以这样做:
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" />
</behavior>
</endpointBehaviors>
但是似乎并没有RequestFormat的属性。我如何将默认RequestFormat
设置为JSON?
谢谢澄清!所以只要body指定“application/json”格式,WCF就会自动提取它,正确吗? – 2012-08-03 15:56:51
我从链接中找到一个有趣的小技巧:“如果在操作中没有指定默认格式,则使用DefaultOutgoingResponseFormat属性的值。”所以基本上,如果海报没有指定内容类型,并且操作上没有RequestFormat,它实际上会从defaultOutgoingResponseFormat中选取格式。有趣。 – 2012-08-03 16:02:44