2012年1月12日

[WCF]Maximum number of items that can be serialized or deserialized in an object graph is '65536'

I got this error while invoking a web service server with a WCF client.

This error message indicates that the data is too large and exceed max. serialize/deserialize object limit.

To resolve this, we need to modify maxItemsInObjectGraph attribute in <dataContractSerializer> on both client side and server side.

To accomplish this via xml configuration file, add the following lines on server web.config:

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


And on client



<endpointBehaviors>
<behavior name="somename">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/> 
...


To accomplish this via code on the server:
foreach (IServiceBehavior behavior in selfHost.Description.Behaviors)
{
try
{
if(behavior is DataContractSerializerOperationBehavior){
((DataContractSerializerOperationBehavior)behavior).MaxItemsInObjectGraph = int.MaxValue;
}
}
catch { }
}


on the client:


foreach (OperationDescription operation in pipeFactory.Endpoint.Contract.Operations)
{
foreach (IOperationBehavior behavior in operation.Behaviors)
{
try
{
if(behavior is DataContractSerializerOperationBehavior){
((DataContractSerializerOperationBehavior)behavior).MaxItemsInObjectGraph = int.MaxValue;
}
}
catch { }
}
}



And Since my server is a traditional web service, I don’t need to modify server codes (at least for my case). 

沒有留言:

About Me