Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set the MaxItemsInObjectGraph

Tags:

c#

.net

wcf

bigdata

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

There was an error while trying to serialize parameter http://tempuri.org/:GetCurrentDatabaseObjectsResult. The InnerException message was 'Maximum number of items that can be serialized or deserialized in an object graph is '65535'. Change the object graph or increase the MaxItemsInObjectGraph quota. '. Please see InnerException for more details.

(the main important thing is that I've to increase the MaxItemsInObjectGraph).

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

like image 332
J4N Avatar asked Dec 22 '22 19:12

J4N


2 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>
like image 185
Felix Avatar answered Jan 04 '23 21:01

Felix


https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/specifying-data-transfer-in-service-contracts#controlling-the-serialization-process

Go down to "Controlling the serialization process" heading (or do a search for the maxItemsInObjectGraph)

like image 34
Brad Christie Avatar answered Jan 04 '23 21:01

Brad Christie