Paul Kiddie

You get a HTTP 400 Bad Request when POSTing to a WCF service

February 01, 2012

I was recently debugging an issue with a WCF service I had written, where it appeared that POSTing data to the service returned a HTTP 400 Bad Request error. There are several reasons you may experience such an error but mine was peculiar in that it wasn’t always repeatable and depended on what the client application sent to the service.

wcf service 400 bad request

On closer investigation, it turned out I was hitting the default maxReceivedMessageSize (set to 65536 bytes) when POSTing the data on the basicHttpBinding binding I was using, as shown on the Fiddler trace above (highlighted in red) — but only just.

How to get around it.

The WCF service was defined by an endpoint using the basicHttpBinding binding, so it was a case of adding the following:

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="" maxReceivedMessageSize="262144"></binding>
</basicHttpBinding>
</bindings>
...
</system.serviceModel>

Here I am setting the default basicHttpBinding binding to accept messages up to 256KB in size. This immediately fixed the problem. This comes with a caveat though as setting this value too high on production code could increase your exposure to possible DoS attacks from large payloads.


👋 I'm Paul Kiddie, a software engineer working in London. I'm currently working as a Principal Engineer at trainline.