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

发布于 2024-10-14 05:00:23 字数 452 浏览 4 评论 0原文

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

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

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

该行为如下:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人骨 2024-10-21 05:00:23

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

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;
        }
    }
冷了相思 2024-10-21 05:00:23

在服务器端,您还可以:

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

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);
}
若能看破又如何 2024-10-21 05:00:23

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文