Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add maxItemsInObjectGraph programmatically without using configuration file?

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>
like image 787
Md. Rashim Uddin Avatar asked Jan 27 '11 04:01

Md. Rashim Uddin


2 Answers

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;
        }
    }
like image 99
flayn Avatar answered Nov 04 '22 18:11

flayn


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);
}
like image 24
gfan Avatar answered Nov 04 '22 16:11

gfan