How to add maxItemsInObjectGraph programmatically without using configuration file?

Md. Rashim Uddin picture Md. Rashim Uddin · Jan 27, 2011 · Viewed 7.5k times · Source

I have create a EndpointAddress like that

EndpointAddress address = new EndpointAddress("http://example.com/services/OrderService.svc");

But I could not add the Behavior to this Endpoint programmatically.

The behavior is given below.:

<behaviors>
  <endpointBehaviors>
    <behavior name="NewBehavior">
      <dataContractSerializer maxItemsInObjectGraph="6553600" />
    </behavior>
  </endpointBehaviors>
</behaviors>

Answer

flayn picture flayn · Jan 27, 2011

On the server you have to add it in the ServiceBehavior Attribute:

 [ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)]

On the client you have to apply it to the endpoint. In this example you can see how to add it to all the endpoints in your ChannelFactory:

var factory = new ChannelFactory<IInterface>(...);
foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
    {
        var dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>();
        if (dataContractBehavior != null)
        {
            dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
        }
    }