The InnerException error(serialized or deserialized in an object graph) in WCF


Introduction : This Blog introduced how can we solve the InnerException error(serialized or deserialized in an object graph)
in wcf like '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.
Explanation:
Two behaviors are available in WCF,
1.DataContractSerializerOperationBehavior
  Represents the run-time behavior of the DataContractSerializer.
  System.ServiceModel.Description.DataContractSerializerOperationBehavior.
2..XmlSerializerOperationBehavior
   Controls run-time behavior of the XmlSerializer associated with an operation.
   System.ServiceModel.Description.XmlSerializerOperationBehavior

The DataContractSerializerOperationBehavior has the MaxItemsInObjectGraph, IgnoreExtensionDataObject, and DataContractSurrogate properties that
we may use to customize the serialization process.
We can use the DataContractSurrogate property to enable data contract surrogates, which are a powerful mechanism for customizing and extending the serialization process.
We can use the DataContractSerializerOperationBehavior to customize both client and server serialization.
3. Example> Following example shows how to increase the MaxItemsInObjectGraph quota on the client.

ChannelFactory<IDataService> factory = new ChannelFactory<IDataService>(binding, address);
foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
{
    DataContractSerializerOperationBehavior dataContractBehavior =
                op.Behaviors.Find<DataContractSerializerOperationBehavior>()
                as DataContractSerializerOperationBehavior;
    if (dataContractBehavior != null)
    {
        dataContractBehavior.MaxItemsInObjectGraph = 100000;
    }
}
IDataService client = factory.CreateChannel();

4.The MaxItemsInObjectGraph and IgnoreExtensionDataObject can be controlled through configuration by using the dataContractSerializer endpoint or service behavior.
5.Example> Following example shows the behaviors in endpoint.

<configuration>
    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="LargeQuotaBehavior">
                    <dataContractSerializer
                      maxItemsInObjectGraph="100000" />
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <client>
            <endpoint address=http://example.com/myservice
                  behaviorConfiguration="LargeQuotaBehavior"
                binding="basicHttpBinding" bindingConfiguration=""
                            contract="IDataService"
                name="" />
        </client>
    </system.serviceModel>
</configuration>