如何在不使用配置文件的情况下以编程方式添加maxItemsInObjectGraph?

Md.*_*din 10 .net c# wcf wcf-configuration wcf-endpoint

我已经创建了一个类似的EndpointAddress

EndpointAddress address = new EndpointAddress("http://example.com/services/OrderService.svc");
Run Code Online (Sandbox Code Playgroud)

但我无法以编程方式将此行为添加到此端点.

行为如下:

<behaviors>
  <endpointBehaviors>
    <behavior name="NewBehavior">
      <dataContractSerializer maxItemsInObjectGraph="6553600" />
    </behavior>
  </endpointBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)

fla*_*ayn 28

在服务器上,您必须将其添加到ServiceBehavior属性中:

 [ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)]
Run Code Online (Sandbox Code Playgroud)

在客户端上,您必须将其应用于端点.在此示例中,您可以看到如何将其添加到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;
        }
    }
Run Code Online (Sandbox Code Playgroud)