How to set the default RequestFormat for a WCF ServiceContract?

Ryan J. Thompson picture Ryan J. Thompson · Aug 2, 2012 · Viewed 8.7k times · Source

I'm writing a web service that has a lot of methods. They are all set up similar to the following:

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

What I want to do is just set the default BodyStyle / RequestFormat / ResponseFormat all in the web.config file. Now, I know I can do this:

  <endpointBehaviors>
    <behavior name="webHttpBehavior">
      <webHttp defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" />
    </behavior>
  </endpointBehaviors>

But there doesn't seem to be an attribute for the RequestFormat. How can I set the default RequestFormat to JSON?

Answer

SliverNinja - MSFT picture SliverNinja - MSFT · Aug 3, 2012

Request types are automatically interpreted by WCF, you don't need to specify a default RequestFormat for your service operation.

If you are trying to enforce the supported request format, see this related SO post on enforcing request content types.

Note: it doesn't make sense to assign a RequestFormat for a WebGet operation. By definition, a WebGet cannot contain a Body which is where the JSON format would exist. A better example here would be WebInvoke.