Thursday, November 6, 2008

WCF: file transfer error due to large size

Many people have bloged about a size issue during uploading file via WCF services. I want to detail the exact four scenarios that can occur:

By default, ASP.NET 2.0 has a default maximum request length of 4MB configured in machine.config. This value can be changed directly in machine.config or overridden in the service application's web.config:

WCF service binding definition also comtains a size def in bytes. So 4000000 is 4 MB.

There are four scenarios:
  1. if the size of the message is greater than BOTH values, this exception is thrown by the service:
    System.ServiceModel.CommunicationException: Maximum request length exceeded. ---> System.Web.HttpException: Maximum request length exceeded.

  2. if the size of the message is greater than httpRuntime.maxRequestLength but less than WCF's binding.maxReceivedMessageSize, the same exception above is thrown.

  3. if the size of the message is less than httpRuntime.maxRequestLength but greater than WCF's binding.maxReceivedMessageSize, the client receives a "The remote server returned an unexpected response: (400) Bad Request." excception. Nothing is logged on the server.

  4. if the size of the message is less than BOTH values, it works!
I used the term "size of the message" instead of size of the file above, as there is overhead when a file is packaged in service calls. For example, if the upper limit of file upload is 10 MB, both configurations should be like 12 MB. The actual wiggle room needed depends on the data.