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 maxReceivedMessageSize and <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 <httpRuntime>‘s 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!

About Ed Jones

Ed is a .NET and Integration architect for Avanade in the Twin Cities. Contact Ed

10 responses to “WCF: (400) Bad Request while Streaming Large Files through IIS”

  1. Jonathan Lucas says :

    Thank you so much! I was hitting my head against the wall with exactly what you were describing (the and settings). I added that one line for httpRuntime to my web.config, and everything runs like clockwork. I literally looked at over 50 different web sites before finding this.

    • Ed Jones says :

      Glad to be able to help!

    • Luke says :

      I ensured maxReceivedMessageSize is set high enough on both server hosting WCF service and the client proxy. I also added the httpRuntime maxRequestLength setting to my Web.config on my WCF service. I still get the bad request 400 error. I have searched everywhere and updated all settings on both server/client to no avail. I am still banging my head against the proverbial wall.

      • Ed Jones says :

        I assume smaller messages are getting through OK?

      • Luke says :

        Yes, smaller messages get through just fine. I was able to solve my problem although I am very unhappy with the solution.

        If I give the binding a name=”whatever” and add that to the endpoint as the bindingConfiguration=”whateve” it completely ignores it. If I remove the name and the bindingConfiguration atrrib/value, it works just fine. This makes no sense to me, but I’m sure there is something else wrong perhaps in my config file on the server.

  2. Venkat Tanguturi says :

    Hello Ed

    I am facing the same problem as Luke’s one. Did you find the solution for that? I am working on IIS 7.5 and done all the settings still cannot transfer large files (400 Bad Request error). Please help. Thanks in Advance


    • Ed Jones says :

      There could be a configuration problem with your endpoint. If you are using WCF 4, a configuration issue may result in the default endpoint settings being used. In that case, larger messages would fail because the default settings for maxReceivedMessageSize or ReaderQuotas aren’t big enough, not because the httpRuntime settings.

    • Luke says :

      I had also asked about this in the MSDN forums. You can check out http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/43e5f6a5-8c69-4a9b-b0eb-05eede3fab43 to see the reply. It actually solved my problem because I apparently kept overlooking it every time I went over my config.

      • Ed Jones says :

        Thanks, Luke! That link was really helpful! I’m very glad that you were able to get your problem solved. That points us at yet another one of many things we need to be mindful of when working with WCF.

Trackbacks / Pingbacks

  1. 2010 in review | Extremely Talented Monkeys - January 8, 2011

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: