J4N
J4N

Reputation: 20733

Programmatically set the MaxItemsInObjectGraph

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.

Upvotes: 4

Views: 9053

Answers (3)

Brad Christie
Brad Christie

Reputation: 101614

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)

Upvotes: 4

John C
John C

Reputation: 920

You want to specify the property in the ServiceBehavior attribute.

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

Upvotes: 2

Felix
Felix

Reputation: 3476

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>

Upvotes: 5

Related Questions