none
MaxItemsInObjectGraph

    Question

  • I'm getting the following error.

      Error while trying to serialize parameter http://tempuri.org/:GetByUpdatedDateResult. Maximum numberError while trying to serialize parameter http://tempuri.org/:GetByUpdatedDateResult. 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.

    The method returns List<T> where there are about 1,900 items.

    Any ideas?

     

    Thursday, June 08, 2006 11:35 AM

Answers

  • I think we can change this parameter from custom behaviour

    <behaviors>

    <behavior name="CalculatorServiceBehavior">

    <dataContractSerializer maxItemsInObjectGraph="6553600" />

    </behavior>

    </behaviors>

    Thursday, June 08, 2006 5:28 PM
  • Hey a step forward!  Madhu looks as though you are right about the behaviour.

    The error now looks is now on the client side, which would indicate it has sucessfully serialized the object graph and the exception is in the proxy:

    The formatter threw an exception while trying to deserialize the message: Error while trying to deserialize parameter http://tempuri.org/:GetByUpdatedDateResult. 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.

    However putting in this behavior also on the client solves the problem.  Thank you for your help.

    BenW

     

    Thursday, June 08, 2006 5:41 PM

All replies

  • May this help?

    <bindings>
       <
    netTcpBinding>
          <
    binding name="SecureBinding" 
                  
    maxBufferSize="99000000" 
                   maxReceivedMessageSize
    ="99000000">
             <
    readerQuotas maxDepth="900000"
                           
    maxStringContentLength="900000"
                           
    maxArrayLength="900000" 
                           
    maxBytesPerRead="900000"
                         
     maxNameTableCharCount="900000" />

          </binding>
       </
    netTcpBinding>
    </
    bindings>

     

    Guy

    http://blogs.microsoft.co.il/blogs/bursteg/

     

     

     

    Thursday, June 08, 2006 11:41 AM
  • Thanks for the quick reply.  Just tried this and no.

    The exception is being thrown by the server when it tries to serialize.

     

     

     

    Thursday, June 08, 2006 11:49 AM
  • Try adding this section to your app.config:

    <system.runtime.serialization>
    <datacontractserializer maxItemsInObjectGraph="100000" />
    </system.runtime.serialization>

    Thursday, June 08, 2006 12:58 PM
  • Thanks, however when this line is in,  it throws the following exception on the line below:

    The type initializer for 'System.ServiceModel.DiagnosticUtility' threw an exception.

    "Unrecognized attribute 'maxItemsInObjectGraph'. Note that attribute names are case-sensitive."

    ServiceHost host1 = new ServiceHost(typeof(MyService), new Uri(http://localhost:9100/myservice/service.svc));

     app.config below:

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

    <system.runtime.serialization>

    <dataContractSerializer maxItemsInObjectGraph="100000" />

    </system.runtime.serialization>

    BenW

    Thursday, June 08, 2006 3:39 PM
  • Hi Ben,

     

    If you have repro for this problem,can you please send it to me,my email id is madhup@microsoft.com

    -Thank you

    Madhu

    Thursday, June 08, 2006 4:46 PM
  • Thanks Madhu,

    The code that causes the original serialization problem or the exception caused by putting in the maxItemsInObjectGraph element in the app.config?

    Intellisence does not pick up maxItemsInObjectGraph as being an attribute of dataContractSerializer in the app.config and it not mentioned in the windowssdk online

    http://windowssdk.msdn.microsoft.com/library/default.asp?url=/library/en-us/wcf_genref/html/f41fb4d5-24e7-4059-8010-286a30bfea93.asp

     

     

    Thursday, June 08, 2006 5:08 PM
  • I think we can change this parameter from custom behaviour

    <behaviors>

    <behavior name="CalculatorServiceBehavior">

    <dataContractSerializer maxItemsInObjectGraph="6553600" />

    </behavior>

    </behaviors>

    Thursday, June 08, 2006 5:28 PM
  • Hey a step forward!  Madhu looks as though you are right about the behaviour.

    The error now looks is now on the client side, which would indicate it has sucessfully serialized the object graph and the exception is in the proxy:

    The formatter threw an exception while trying to deserialize the message: Error while trying to deserialize parameter http://tempuri.org/:GetByUpdatedDateResult. 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.

    However putting in this behavior also on the client solves the problem.  Thank you for your help.

    BenW

     

    Thursday, June 08, 2006 5:41 PM
  • Just out interest, as the configuration of MaxItemsInObjectGraph was the issue, what consitutes an object for the purpose of this count? Are DateTime and String types included in the count?  Thanks
    Thursday, June 08, 2006 7:29 PM
  • After all this valuable info, one thing i would llike to add in here. the changed propery "behaviour" needs to be added in teh "endpoint" element to make this all work. this is key otherwise madhus information is clinical in here.

     

    I faced a same one such problem and I missed putting in the new behavior in end point's binding and i got the error until i did so.

    Tuesday, June 12, 2007 11:33 AM
  •  


    How to set the MaxItemsInObjectGraph configuration.

     

    Symptoms/Exceptions:

    ("System.Runtime.Serialization.SerializationException : 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.").

    "System.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly"

    Solution

    You need to modify both the configuration file for the service as well as the client.

    For the services configuration should look like this:

    <system.serviceModel>    <services>
          <service behaviorConfiguration="Umea.se.EventReactor.ServiceTier.ServiceViewEventBehavior"
           name="Umea.se.EventReactor.ServiceTier.ServiceViewEvent">
            <endpoint address="" binding="wsHttpBinding" contract="Umea.se.EventReactor.ServiceTier.IServiceViewEvent">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="Umea.se.EventReactor.ServiceTier.ServiceViewEventBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
              <dataContractSerializer maxItemsInObjectGraph="2147483647" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>


    The client configuration should look like this:


    <system.serviceModel>
        <client>
    <endpoint address="http://localhost:3379/ServiceViewEvent.svc" behaviorConfiguration="ServiceViewEventBehavior"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IServiceViewEvent"
            contract="ServiceReferenceViewEvent.IServiceViewEvent" name="WSHttpBinding_IServiceViewEvent">
            <identity>
              <dns value="localhost" />
            </identity>
          </endpoint>
    </client>
    <behaviors>
          <endpointBehaviors>
            <behavior name="ServiceViewEventBehavior">
              <dataContractSerializer maxItemsInObjectGraph="2147483647"/>   
            </behavior>
          </endpointBehaviors>
    </behaviors>
      </system.serviceModel>

     


    My Source info:
    <a href="http://blog.hill-it.be/post/2007/08/22/MaxItemsInObjectGraph-and-keeping-references-when-serializing-in-WCF">http://blog.hill-it.be/post/2007/08/22/MaxItemsInObjectGraph-and-keeping-references-when-serializing-in-WCF</a>
    <a href="http://processmentor.com/community/blogs/scott_middleton/archive/2007/06/08/169.aspx"></a><blockquote></blockquote>

    Monday, April 21, 2008 8:15 AM
  • Hi,

    How can I achieve the same thing in code only, with no configuration file?

    Thanks.
    Monday, July 07, 2008 7:21 PM
  • See for details of the ServiceBehaviorAttribute class

     

    http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.aspx

     

    and the MaxItemsInObjectGraph property

     

    http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.maxitemsinobjectgraph.aspx

     

    There is also a general article on extending WCF with custom Behaviors here by Aaron Skonnard, that you might find interesting.

     

    http://msdn.microsoft.com/en-us/magazine/cc163302.aspx

     

    Sunday, August 10, 2008 8:23 AM
  • Daniel - your advice on exactly how to set maxItemsInObjectGraph, client-side + server-side, is bang on the mark. Well done!
    T Sadikali
    Wednesday, October 07, 2009 2:28 PM
  • How to set behaviourConfiguration programatically?

     

    Suppose this

     

     var binding = new CustomBinding()
                {
                    Name = "BinaryHttp",
                    Namespace = Namespaces.BusinessNext,
                    CloseTimeout = new TimeSpan(01, 20, 10),
                    OpenTimeout = new TimeSpan(01, 20, 10),
                    ReceiveTimeout = new TimeSpan(01, 20, 10),
                    SendTimeout = new TimeSpan(01, 20, 10),
                };
     
                var bindingElement = new BinaryMessageEncodingBindingElement
                                         {
                                             MessageVersion = MessageVersion.Default,
                                             
                                         };
     binding.Elements.Add(bindingElement);
                binding.Elements.Add(new HttpTransportBindingElement()
                {
                                   
                    MaxReceivedMessageSize = 64 * 1024 * 1024,
                  
                    TransferMode = TransferMode.Buffered,
                    
     
                });
     var endpointAddress = new EndpointAddress(Location);
     
                channelFactory = new ChannelFactory<T>(binding, endpointAddress);


     


    kunal
    Wednesday, March 23, 2011 7:36 AM
  • Daniel,

     

    Thank you for the explanation. It helped me solve my problem. 

    Slavica

    Tuesday, May 24, 2011 10:08 AM
  • Hi all

    As far as I understand, there is two main solutions, one is to change maxItemInObjectGraph to equal 655300 in the service and client sides, and the other is to change maxItemInObjectGraph ="2147483647" in both the client and service configurations.
    I still get the same error.

    any ideas?

     

    bests.


    Tuesday, November 15, 2011 8:19 PM
  • In case anyone runs across this... here is the simple way to do it without the XML files:

    On the client:

                 foreach (OperationDescription operation in pipeFactory.Endpoint.Contract.Operations)
                {
                    foreach (IOperationBehavior behavior in operation.Behaviors)
                    {
                        try
                        {
                            ((dynamic)behavior).MaxItemsInObjectGraph = int.MaxValue;
                            foundItems++;
                        }
                        catch { } 
                    }
                }

     

    On the server:

                foreach (IServiceBehavior behavior in selfHost.Description.Behaviors)
                {
                      try
                      {
                           ((dynamic)behavior).MaxItemsInObjectGraph = int.MaxValue;
                       }
                       catch { }                       
                }

     

    ...yes, there is a more delicate way to do this (without the dynamics) but this will always work, so I am not interested in the most syntactically correct thing in the universe for this problem.

    I really don't know why it's so freaking hard for people to just answer questions like this. We don't want a link to the 1,200 page reference manual on what a behavior is and how to implement one. We just want the practical solution... </grrr>

     


    • Edited by MrManMan Thursday, December 29, 2011 5:23 PM
    • Proposed as answer by MrManMan Thursday, December 29, 2011 5:26 PM
    Thursday, December 29, 2011 5:20 PM