8
votes

WCF 4.0 REST projects will return 400 Bad Request errors if the POST body is greater that 8192 characters in length. This is the default value of the XmlDictionaryReaderQuotas.MaxStringContentLength property. The XmlDictionaryReader class is used in the deserialization process, even for JSON messages.

I've seen many examples of how to solve this for WCF with custom bindings and endpoints, but no solution for WCF 4.0 REST projects, which uses a simplified configuration.

The normal web.config file contains a section that looks like this:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
        <webHttpEndpoint>
            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
        </webHttpEndpoint>
    </standardEndpoints>
</system.serviceModel>

First, the message size needs to be increased. To do that, add maxReceivedMessageSize to the standardEndpoint.

            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="65535" />

To set MaxStringContentLength, add the following to the system.serviceModel section:

<bindings>
  <webHttpBinding>
    <binding>
      <readerQuotas maxStringContentLength="65535"/>
    </binding>
  </webHttpBinding>
</bindings>

You'll need to set the length to a value suitable for your environment.

No. I wanted to make a note of it for others.Todd
Thanks for this Todd. With the seemingly thousands of other non-REST posts on this, this worked where others didn't.Captain Skyhawk