【问题标题】:How to add maxItemsInObjectGraph programmatically without using configuration file?如何在不使用配置文件的情况下以编程方式添加 maxItemsInObjectGraph?
【发布时间】:2011-06-16 07:23:35
【问题描述】:

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

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

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

行为如下:

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

【问题讨论】:

    标签: c# .net wcf wcf-configuration wcf-endpoint


    【解决方案1】:

    在服务器上,您必须将其添加到 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;
            }
        }
    

    【讨论】:

    • 很好 - 昨天用过。谢谢
    • 但请注意,如果您正在实现自己的DataOntrolSerializerOperationBehavior,例如,要保留循环引用,则必须在DataContractSerializer的构造函数中指定MaxItemsInObjectGraph。
    【解决方案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);
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-30
        • 2022-10-31
        • 2012-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多