Geeks With Blogs

Randy Michak
| Home |

Recently I ran into the following error message in Web Application I was developing:

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. '.

After researching this issue, I was able to determine why the error was occuring.  The problem has to do with the default serializatoin sizes that were being utilized by my WCF connection.   Now typically with other such settings, you would customize the bindings in the web.config or in code and all is well.   Unfortunately, this setting is not as easy to get to from within a client app.  You need to actually add a behavior on the client side to change the setting. 

There are a number of links that talk about how to do this via the app.config or web.config, but the problem I was having was that I needed to change this in code.  My application was actually building the url in code and therefore, I had to navigate through to the channel itself and make the change.

On the server I was able to change the config itself as follows:

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

On the client, I was able to iterate through each of the Operations and set the property to a much larger value. 

protected ISecurityAdministrationService GetSecAdminClient()
{
     ChannelFactory<ISecurityAdministrationService> factory = new    ChannelFactory<ISecurityAdministrationService>(wsSecAdminBinding, SecAdminEndpointAddress);
     foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
     {
       DataContractSerializerOperationBehavior dataContractBehavior =op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior;
       if (dataContractBehavior != null)
       {
             dataContractBehavior.MaxItemsInObjectGraph = 2147483647;
       }
     }
    ISecurityAdministrationService client = factory.CreateChannel();
    return client;
}

 

 

The following MSDN link talks about changing some of the Data Transfer settings in service contracts: http://msdn.microsoft.com/en-us/library/ms732038.aspx


Posted on Wednesday, March 4, 2009 1:43 PM | Back to top


Comments on this post: Programattically setting the MaxItemsInObjectGraph property in client

# re: Programattically setting the MaxItemsInObjectGraph property in client
Requesting Gravatar...
Finally got the solution. thanks.
Left by Ram Sharma on Mar 31, 2009 11:54 AM

# re: Programattically setting the MaxItemsInObjectGraph property in client
Requesting Gravatar...
This is really helpful
Left by Lakshmi Patil on Dec 03, 2009 12:04 AM

# re: Programattically setting the MaxItemsInObjectGraph property in client
Requesting Gravatar...
To solve this error, use following function :
Following function calling syntax :


ConfigureClientEndPoint(<ClientObjectOfService>.EndPoint);
ClientObjectOfService is object of service at client, like object of IAirfareQuoteService.

private void ConfigureClientEndPoint(ServiceEndpoint endPoint)
{
foreach (OperationDescription operation in endPoint.Contract.Operations)
{
DataContractSerializerOperationBehavior dataContractBehavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (dataContractBehavior != null)
{
dataContractBehavior.MaxItemsInObjectGraph = 2147483647;
}
}
Left by Uday Kadam on Dec 22, 2009 3:58 AM

# re: Programattically setting the MaxItemsInObjectGraph property in client
Requesting Gravatar...
this would be really helpful to me, but I can't find which namespace ISecurityAdministrationService is in - and google doesnt seem to want to help either.

Could you add the using statements possibly?
Left by Jas on Sep 20, 2011 9:32 AM

# re: Programattically setting the MaxItemsInObjectGraph property in client
Requesting Gravatar...
Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you
Left by Shaul B on Oct 07, 2011 3:43 AM

# re: Programattically setting the MaxItemsInObjectGraph property in client
Requesting Gravatar...
If you are getting same issue then dont search any where just follow up this solution ... great work :)
Left by Punit Pandya on Mar 31, 2012 4:28 AM

Your comment:
 (will show your gravatar)
 


Copyright © Randy Michak | Powered by: GeeksWithBlogs.net | Join free