Friday, May 2, 2014

WCF messge size tips and tricks

The default size of message in SOAP\WCF communication is 65536 bytes.

Sometimes you may need to send soap message bigger than 65536 bytes.

If you exceed that limit you obtain this exception:
System.ServiceModel.ProtocolException occurred
  Message=The remote server returned an unexpected response: (413) Request Entity Too Large.

To go beyond this limit you have to modify the service's binding configuration settings of web.config and app.config .

Setting are:
maxReceivedMessageSize of binding configuration
maxArrayLength of readerQuotas section
maxArrayLength of readerQuotas section
maxBytesPerRead of readerQuotas section
maxDepth of readerQuotas section
maxNameTableCharCount of readerQuotas section

All those setting require an int value, so the maximum value is the int.MaxValue.
Below there is an example of basicHttpBinding with all size values to the maximum value.


In this way you can have soap messages with size about to 2gb.
But you must pay attention to do this because a single message can put your server in denial of service (DOS).
Size of 2gb is not a good idea.

All above said is formally corrected but... surprise surprise doesn't work completely. Yes of course.
If you are hosting your web service in IIS and your message is greater then 30,000,000 bytes, client side you'll receive this exception:

System.ServiceModel.EndpointNotFoundException occurred
  Message=There was no endpoint listening at http://localhost/WebApplication1/Service1.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

This exception is right because IIS enter in action before WCF "runtime" to filter requests and prevent denial of service (DOS) attacks.

If you need to go beyond 30,000,000 bytes you have to modify webserver's settings.
You can modify webserver's settings modifying web.config like the example below.

Setting to modify is maxAllowedContentLength that require integer value.

            <mimeMap fileExtension=".desk" mimeType="application/x-zip-compressed" />
            <mimeMap fileExtension=".crc" mimeType="text/plain" />
                <requestLimits maxAllowedContentLength="2147483647" 

                   maxQueryString="2147483647" />


There are other ways to alter requestfiltering setting using "IIS Manager" described at this link:
Those ways at the end alter web.config.


  1. If you need to create a WCF service that accept more than 65536 bytes and less than 30,000,000 bytes of data you have to modify app.config and web.config altering maxReceivedMessageSize and readerQuotas.          
  2. If your messages are bigger than 30,000,000 bytes you have to alter all maxReceivedMessageSize, readerQuotas and requestFiltering settings with same values.
Pay attention because big amount of data can cause Denial Of Service problems, so be careful and apply this setting only on binding defined for the specified service that require so much data. 
Remember that for large amount of data scenario WCF support streaming transfer mode (but this is another story).

No comments:

Post a Comment