Tuesday, January 11, 2011

WCF – MaxItemsInObjectGraph Error

If you get the following error:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameterhttp://tempuri.org/:xxxxx. The InnerException message was 'Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota. '.  Please see InnerException for more details.

There are 2 parts to fixing this error:
On the server:
Add the following to the server's config file
<behaviors>
     <serviceBehaviors>
       <behavior name="ServiceBehavior0">
         <dataContractSerializer maxItemsInObjectGraph="2147483647" />
       </behavior>
     </serviceBehaviors>
   </behaviors>

And reference it in your service using the following code:

<services>
      <service behaviorConfiguration="ServiceBehavior0" name="serviceName">……

On the client:
Add the following:
       <behaviors>
        <endpointBehaviors>
          <behavior name="behavior0">
            <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          </behavior>
        </endpointBehaviors>
      </behaviors>

And reference it in your end-point using the following code:

<endpoint address="serviceAddress"
               behaviorConfiguration="behavior0"

The 2147483647 is just a sample value. You should adjust it to something that makes sense for your application.

Note: The above works only if you are not using a custom serializer/deserializer.

No comments: