How to Configure MaxItemsInObjectGraph

I've been told that I'm exceeding the maximum number of items that can be serialized or deserialized. Where is this MaxItemsInObjectGraph setting? I can't find it anywhere.

MaxItemsInObjectGraph looks like a message encoder setting but you won't find it on any of the bindings or message encoder binding elements. You also won't find it on any of the transports or channels. That is because the MaxItemsInObjectGraph quota is actually applied to the serializer that the service uses for a particular operation. You can access this setting through a data contract serializer behavior that modifies how the service or operation works.

In configuration, you can change this setting by adding a custom behavior to the behaviors section that includes the following setting.

<dataContractSerializer maxItemsInObjectGraph="3" />

You can also change this programmatically by modifying the operation description of your service. You'll need to change this code snippet with the appropriate endpoint and operation name.

OperationDescription operation = host.Description.Endpoints[0].Contract.Operations.Find("MyOperationName");
operation.Behaviors.Find<DataContractSerializerOperationBehavior>().MaxItemsInObjectGraph = 3;

Note that you cannot just create a new instance of the behavior and add it to the description. There is a limit of having only one DataContractSerializerOperationBehavior per operation and the operation comes with one by default.

One unusual aspect of this quota is visible in the original question. The quota setting applies to both serializing and deserializing a message. This means that you have to modify the setting on both the client and server side for this to work.

Next time: Splitting Up XML Text Nodes

Comments (1)

  1. This time it’s two questions that have the same answer. What contract does the message encoder have for

Skip to main content