11

我已经创建了一个这样的 EndpointAddress

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

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

行为如下:

<behaviors>
  <endpointBehaviors>
    <behavior name="NewBehavior">
      <dataContractSerializer maxItemsInObjectGraph="6553600" />
    </behavior>
  </endpointBehaviors>
</behaviors>
4

3 回答 3

29

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

 [ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)]

在客户端上,您必须将其应用于端点。在此示例中,您可以看到如何将其添加到 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;
        }
    }
于 2011-01-27T08:49:29.950 回答
2

在服务器端,您还可以:

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);
}
于 2013-12-03T03:28:24.820 回答
0

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

于 2018-06-13T06:59:26.270 回答