简体   繁体   English

以编程方式设置MaxItemsInObjectGraph

[英]Programmatically set the MaxItemsInObjectGraph

I've an application using WCF on client and server side. 我在客户端和服务器端使用WCF的应用程序。 I get errors when I return a large amount of data: 当我返回大量数据时出错:

There was an error while trying to serialize parameter http://tempuri.org/:GetCurrentDatabaseObjectsResult . 尝试序列化参数http://tempuri.org/:GetCurrentDatabaseObjectsResult时出错。 The InnerException message was 'Maximum number of items that can be serialized or deserialized in an object graph is '65535'. InnerException消息是'对象图中可以序列化或反序列化的最大项数是'65535'。 Change the object graph or increase the MaxItemsInObjectGraph quota. 更改对象图或增加MaxItemsInObjectGraph配额。 '. ”。 Please see InnerException for more details. 有关更多详细信息,请参阅InnerException。

(the main important thing is that I've to increase the MaxItemsInObjectGraph). (最重要的是我要增加MaxItemsInObjectGraph)。

I found this article here: How can I set the maxItemsInObjectGraph property programmatically from a Silverlight Application? 我在这里找到了这篇文章: 如何从Silverlight应用程序以编程方式设置maxItemsInObjectGraph属性? but it seems this is only for the client side and I need to do this on the server. 但似乎这只适用于客户端,我需要在服务器上执行此操作。

In code: 在代码中:

foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
{
    DataContractSerializerOperationBehavior dataContractBehavior =
                op.Behaviors.Find<DataContractSerializerOperationBehavior>()
                as DataContractSerializerOperationBehavior;
    if (dataContractBehavior != null)
    {
        dataContractBehavior.MaxItemsInObjectGraph = 100000;
    }
}

In configuration: 在配置中:

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaivor">
      <serviceAuthorization impersonateCallerForAllOperations="True" />
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceThrottling maxConcurrentCalls="2147483647" />
      <dataContractSerializer maxItemsInObjectGraph="65775" />
    </behavior>
  </serviceBehaviors>
</behaviors>

You want to specify the property in the ServiceBehavior attribute. 您想要在ServiceBehavior属性中指定属性。

 [ServiceContract]
 [ServiceBehavior(MaxItemsInObjectGraph=100000)] 
public interface IDataService 
{
   [OperationContract] 
   DataPoint[] GetData(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM