Friday 23 May 2014

WCF: The maximum message size quota for incoming messages (65536) has been exceeded


Hi Friends,


Problem: 

Recently working on one WCF project we found one issue when we are reading PDF file from SERVER using WCF. There is error like

System.ServiceModel.CommunicationException : The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.


Solution:

To resolve this problem we increased MaxReceivedMessageSize in app.config.
We need to increase the value of two limiting parameters: maxBufferSize and maxReceivedMessageSize. In my example these values are set to 2147483647.
For max file size in WCF, we need to set following parameter in binding files.

MaxBufferPoolSize  gets or sets the maximum size of any buffer pools used by the transport.  The default is 524,288 bytes.

MaxReceivedMessageSize gets and sets the maximum message size that can be receivied. The default is 65536 bytes.

ReaderQuotas: Defines the contraints on the complexity of SOAP messages that can be processed by endpoint configured with a binding. You need to set the maxBytesPerRead, maxDepth, maxNameTableCharCount,maxStringContentLength.

<binding name="MyService"
       maxReceivedMessageSize="2147483647"
       maxBufferSize="2147483647" transferMode="Streamed" >
       <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
     maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647"/>
<!-- other binding info here, such as security -->
</binding>


Max possible size: MaxReceivedMessageSize to 2147483647 (i.e. Int32.MaxValue), which is 2GB

Please Note:

1.       But it’s recommended that the file size should be less, as the data is transferred through network there is possibility of slow network performance.
 
 2.       We can set it to the maximum value, which is 2GB (2147483647) - or if we’re using streamed transfers we can go up to 263 bytes (almost 10,000,000 terabytes). But remember that by setting it to a value larger than necessary we may be opening our service (or client) to attacks.
3.       Only if the service is expected to receive messages larger than 65536 bytes. And in this case, we’d be increasing MaxReceivedMessageSize on the endpoints where we expect to receive such big messages.
4.     If the messages that our client/servers expect to receive are larger than the default value for MaxReceivedMessageSize, then we need to increase this quota.

Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     

Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------