The maximum message size quota for incoming messages exceeded exception in WCF

This is a common error you get when developing you are consuming WCF service operation that deals with large data.

To overcome this error you need to customize the bindings, service behavior and endpoint behavior of the service.

  1. Customize the binding:

First of all you need to set the maxbufferpoolsize and maxReceivedMessageSize in the binding.

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.

Once you define these the binding element should look like the following

 <bindings>

<basicHttpBinding>

<binding name=”Test_Service1″ closeTimeout=”00:10:00″

openTimeout=”00:10:00″ receiveTimeout=”00:10:00″ sendTimeout=”00:10:00″

allowCookies=”false” bypassProxyOnLocal=”false” hostNameComparisonMode=”StrongWildcard”

maxBufferSize=”2147483647″

maxBufferPoolSize=”2147483647″

maxReceivedMessageSize=”2147483647″

messageEncoding=”Text” textEncoding=”utf-8″ transferMode=”Buffered”

useDefaultWebProxy=”true”>

<readerQuotas maxDepth=”2147483647″ maxStringContentLength=”2147483647″ maxArrayLength=”2147483647″

maxBytesPerRead=”2147483647″ maxNameTableCharCount=”2147483647″ />

</binding>

</basicHttpBinding>

</bindings>

  1. Customize Service behavior

You need to add a service behavior and  set the maxitemobjectgraph property in datacontractserlizer attribute.

MaxItemObjectGraph gets the maximum number of items in an object graph to serialize or deserialize.

 <serviceBehaviors>       

        <behavior >

          <dataContractSerializer maxItemsInObjectGraph=”2147483647″ />        

        </behavior>

</serviceBehaviors>

 

  1. Customize an Endpoint behavior

 You also need to add an endpoint behavior and set the maxItemInObjectGraph for data serialization

<behaviors>

<endpointbehaviors>

<behavior name=”Behaviors.EndpointBehavior”>

<datacontractserializer maxItemsInObjectGraph=”2147483647″></datacontractserializer>

</behavior>

/endpointbehaviors>

</behaviors>

Once you have configure the service make sure you have the same settings in your client configuration as well. Because when you add a service reference using visual studio it generates the configuration with default settings.

 

Advertisements

2 Responses to The maximum message size quota for incoming messages exceeded exception in WCF

  1. sandeep says:

    Is there any assumptions you are making on server capacity, ex. 4 GB Memory, 4 Core Processor, 64 bit OS etc.

Leave a Reply

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: