How to add maxItemsInObjectGraph programmatically without using configuration file?

好久不见. 提交于 2019-12-05 17:13:52

问题


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>

回答1:


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;
        }
    }



回答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);
}



回答3:


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



来源:https://stackoverflow.com/questions/4812668/how-to-add-maxitemsinobjectgraph-programmatically-without-using-configuration-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!