WCF: (400) Bad Request while Streaming Large Files through IIS
It’s always the little things.
Last week, one of my WCF services hit a snag as attempts were made to submit large files via streaming. For more info on how to set up a WCF service to stream data, see How to: Enable Streaming on MSDN.
When we sent small files through the service, all worked fine. However, larger files (about 4MB and higher) generated the very unhelpful http exception:
(400) Bad Request. This exception occurs because the entire message is not processed.
We looked at the typical culprits of this problem like
<binding> settings such as
<readerQuota> values to no avail.
It turned out the real problem was not in WCF, but in IIS. HTTP communication has a size limit that you have to override if you want to send large messages to your WCF service hosted in IIS. In the config file (web.config), adjust the
maxRequestLength attribute to a large enough size to support your messages. The value will be an integer representing the size in Kilobytes and has about a 2GB limit. The setting looks something like this (I set mine to about 64MB):
<httpRuntime maxRequestLength="65536" />
If IIS is the problem, you should now be able to process messages up to the size indicated in the
maxRequestLength attribute. Good luck!