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