How to add maxItemsInObjectGraph programmatically without using configuration file?
Asked Answered
A

3

11

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>
Alga answered 27/1, 2011 at 4:13 Comment(0)
G
29

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;
        }
    }
Gunplay answered 27/1, 2011 at 8:49 Comment(1)
But note that if you are implementing your own DataOntrolSerializerOperationBehavior, for example, to preserve cycle references, you must specify the MaxItemsInObjectGraph in the constructor to DataContractSerializer.Wehrmacht
P
2

On Server Side, you can also:

ServiceHost host = new ServiceHost();
ServiceBehaviorAttribute sba = host .Description.Behaviors.Find<ServiceBehaviorAttribute>();
            if (sba == null)
            {
                sba = new ServiceBehaviorAttribute();
                sba.MaxItemsInObjectGraph = int.MaxValue;
                host.Description.Behaviors.Add(sba);
}
Pierro answered 3/12, 2013 at 3:28 Comment(0)
O
0

Alternative: ((ServiceBehaviorAttribute) host.Description.Behaviors[typeof(ServiceBehaviorAttribute)]).MaxItemsInObjectGraph = int.MaxValue;

Omission answered 13/6, 2018 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.