none
MaxItemsInObjectGraph - has the default changed? RRS feed

  • Question

  • I'm getting the dreaded "Maximum number of items that can be serialized or deserialized in an object graph is '65536'" error, and judging by the number of posts I've found it's pretty common.

    However, having read through quite a few of them I'm still a bit confused. The strange thing is that the error seems to be occurring for one particular client/PC. For myself and a colleague it doesn't occur. This is calling the same web service (which I'm positive has the right config) using the the same code.

    The problem appears to be on the deserialization side so I think it's related to the client code rather than the server. The code is .Net 4.0 and we don't have an app.config.

    I added the code I'd seen dotted around the internet to set the value explicitly:

    // Set MaxItemsInObjectGraph to max
    foreach (var operation in client.Endpoint.Contract.Operations)
    {
        var dataContractBehavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
        if (dataContractBehavior != null)
        {
            dataContractBehavior.MaxItemsInObjectGraph = 2147483647;
        }
    }



    However when debugging on my PC the MaxItemsInObjectGraph value is already set to 2147483647 so I can't see how this would have any affect. Which explains why it works fine for me.

    So I thought I'd try and figure out what the default value is for that. I disassembled the .Net framework code and can see that this value is defaulting to int.MaxValue in DataContractSerializerOperationBehavior. Additionally Microsoft show the default in their explanation here: https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.maxitemsinobjectgraph%28v=vs.110%29.aspx

    On the PC where it didn't work, the above fix did the trick, as did using the original code but adjusting  MaxItemsInObjectGraph via the machine.config like this:

    <system.serviceModel>

    .....

    <commonBehaviors>
      <behaviors>
        <endpointBehaviors>
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </endpointBehaviors>
      </behaviors>
    </commonBehaviors>

    ....

    <system.serviceModel>

    The only difference I can see is that the PC it's failing on has .Net 4.0 whereas the two it works on have .Net 4.5.

    Does anyone know if the default changed between .Net 4.0 and 4.5?





    Wednesday, December 9, 2015 6:38 PM

All replies

  • Hi Laughing John,

    From my experience, I suggest you can add the following note to your web.config file.

     <commonBehaviors>
          <endpointBehaviors>
            <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          </endpointBehaviors>
          <serviceBehaviors>
             <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          </serviceBehaviors>
        </commonBehaviors>
    

    Here is the related information, please refer:

    1.set maxItemsInObjectGraph in client config

    Best Regards,

    Vince

    Tuesday, December 15, 2015 1:59 AM
  • Thanks Vince, but I already had that in the Web configuration, so the server side is fine, this is on the client side where you also have to set it. My first post is screwy for some reason so I appreciate that may not have been obvious! 


    • Marked as answer by Laughing John Tuesday, December 15, 2015 3:13 AM
    • Unmarked as answer by Laughing John Tuesday, December 15, 2015 3:14 AM
    • Edited by Laughing John Tuesday, December 15, 2015 3:15 AM
    Tuesday, December 15, 2015 3:12 AM
  • Sorry was on my mobile and trying to edit the reply was tortuous. As I said the problem is with deserialization on the client side, we already have maxItemsInObjectGraph in the web.config on the server.

    Initially from what I read on various forums it seemed that the client side defaulted to 65536 and you HAD to change it, either in the app.config or in code.

    What surprised me was that it worked on 2 PCs without a change and not on the 3rd. The 2 it worked on had .Net 4.6 and the 3rd had .Net 4.0 only. I disassembled on my PC which had 4.6 and could see that the max was defaulting to 2147483647.

    So I can only assume it has changed at some point. I just wondered when so that I know when we need to deploy the fix..

    Thanks for your help anyway..



    Tuesday, December 15, 2015 4:20 PM
  • Hi Laughing John,

    Please add the following note to app.config file which is your client.

     <commonBehaviors>
          <endpointBehaviors>
            <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          </endpointBehaviors>
          <serviceBehaviors>
             <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          </serviceBehaviors>
        </commonBehaviors>

    Best Wishes,

    Vince LI

    Thursday, December 17, 2015 12:46 PM
  • Hi Vince,


    Thanks again, that's basically what I've done although I added it to machine.config. My question really is when did the default size of MaxItemsInObjectGraph change? I have .Net 4.6 installed and I don't need the the endpoint behaviours because the default in code is already 2147483647, but I do for a PC with .Net 4.0. I was just trying to establish when it changed so I know when we need to apply the config file change and when we don't!

    Doesn't really matter, just thought someone might know.

    Thanks.

    Monday, December 21, 2015 3:57 PM