Programmatically set MaxItemsInObjectGraph

I have an application using WCF on the client and server side. I get errors when I return a large amount of data:

An error occurred while trying to serialize the http://tempuri.org/:GetCurrentDatabaseObjectsResult parameter . InnerException message: "The maximum number of elements that can be serialized or deserialized in an object graph is" 65535. "Change the object graph or increase the quota of MaxItemsInObjectGraph.". See InnerException for more information.

(the main thing is that I need to increase MaxItemsInObjectGraph).

I found this article here: How can I set the maxItemsInObjectGraph property programmatically from a Silverlight application? but it looks like this is only for the client side and I need to do it on the server.

+3
source share
3 answers

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>
+4
source

You want to specify a property in the ServiceBehavior attribute.

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

Source: https://habr.com/ru/post/1783804/


All Articles