How to add maxItemsInObjectGraph programmatically without using configuration file?

前端 未结 3 2060
陌清茗
陌清茗 2021-02-20 06:39

I have create a EndpointAddress like that

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

But

相关标签:
3条回答
  • 2021-02-20 07:00

    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);
    }
    
    0 讨论(0)
  • 2021-02-20 07:03

    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;
            }
        }
    
    0 讨论(0)
  • 2021-02-20 07:08

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

    0 讨论(0)
提交回复
热议问题