Exception: Maximum number of items that can be serialized or deserialized in an object graph is '65536' [duplicate]
Asked Answered
H

3

7

I looked at others posts, but I didn't find solution to my problem.

My service config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>
  <appSettings>
    ...
  </appSettings>
  <log4net>
    ...
  </log4net>
  <system.serviceModel>
    <protocolMapping>
      <add scheme="http" binding="wsDualHttpBinding"/>
    </protocolMapping>
    <services>
      <service name="Service1" behaviorConfiguration="Service1Behavior">
        <endpoint address="" binding="netTcpBinding" contract="IService1" bindingConfiguration="NetTcpBinding_IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="/mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
      </service>
      <service name="Service2" behaviorConfiguration="Service2Behavior">
        <endpoint address="" binding="wsHttpBinding" contract="IService2" bindingConfiguration="WSHttpBinding_IService2">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IService2" receiveTimeout="00:00:10"
          sendTimeout="00:00:10" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
          useDefaultWebProxy="false">
          <reliableSession ordered="true" inactivityTimeout="00:00:11" />
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None" />
        </binding>
      </wsHttpBinding>
      <netTcpBinding>
        <binding name="NetTcpBinding_IService1" receiveTimeout="00:00:20"
          sendTimeout="00:00:20" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <reliableSession ordered="true" inactivityTimeout="00:00:11" />
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Service1Behavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceMetadata httpGetEnabled="False"/>

          <serviceDebug includeExceptionDetailInFaults="True"/>
          <serviceThrottling maxConcurrentCalls="100"
                    maxConcurrentInstances="100"
                    maxConcurrentSessions="100" />
        </behavior>
        <behavior name="Service2Behavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="True"/>
          <serviceThrottling maxConcurrentCalls="100"
                    maxConcurrentInstances="100"
                    maxConcurrentSessions="100"  />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <connectionStrings>
    ...
  </connectionStrings>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Error,ActivityTracing"
        propagateActivity="true">
        <listeners>
          ...
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

My client config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        ...
    </configSections>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IService1" closeTimeout="00:00:15"
                    openTimeout="00:00:15" receiveTimeout="00:00:40" sendTimeout="00:00:40"
                    transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
                    maxConnections="10" maxReceivedMessageSize="2147483647">
                    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="None">
                        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </netTcpBinding>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IService2" closeTimeout="00:00:15"
                    openTimeout="00:00:15" receiveTimeout="00:00:25" sendTimeout="00:00:15"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="false">
                    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                    <reliableSession ordered="true" inactivityTimeout="00:00:11" />
                    <security mode="None">
                        <transport realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://.../Design_Time_Addresses/Service2/"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService2"
                contract="Service2Reference.IService2"
                name="WSHttpBinding_IService2"  behaviorConfiguration="serviceBehaviorConfiguration">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="net.tcp://.../Design_Time_Addresses/Service1/"
                binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService1"
                contract="Service1Reference.IService1" name="NetTcpBinding_IService1"
                behaviorConfiguration="serviceBehaviorConfiguration">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
      <behaviors>
        <endpointBehaviors>
          <behavior name="serviceBehaviorConfiguration">
            <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          </behavior>
        </endpointBehaviors>
      </behaviors>
    </system.serviceModel>
    <userSettings>
        ...
    </userSettings>

</configuration>

Why I still get exception:

The formatter threw an exception while trying to deserialize the message: 
There was an error while trying to deserialize parameter http://tempuri.org/:GetData.
The InnerException message was 'Maximum number of items that can be serialized or deserialized in an object graph is '65536'. 
Change the object graph or increase the MaxItemsInObjectGraph quota. '.

Is seems to ignore <dataContractSerializer maxItemsInObjectGraph="2147483647"/> in both App.config files. Why?

Heathenize answered 25/6, 2013 at 8:42 Comment(1)
@Cybermaxs-Betclic No, it's not duplicate. I'm using "Add Service Reference" in Visual Studio so my config file is correctHeathenize
H
1

I solved this problem with adding behavior in C# code.

client.Endpoint.Behaviors.Remove(typeof(CallbackBehaviorAttribute));
client.Endpoint.Behaviors.Add(new CallbackBehaviorAttribute() { MaxItemsInObjectGraph = 2147483647 });

Problem probably is caused because of using ServiceClient constructor with InstanceContext, Binding and EndpointAddress so behavior from app.config is ignored.

Heathenize answered 25/6, 2013 at 10:27 Comment(1)
Could you please explain where this code should go? Is it on instantiating the app? Or before each connection? Thanks. I don't fully understand the constructor issue you describe.Denitadenitrate
B
15

At the client, see the 3 lines marked ****:

    <client>
        <endpoint address="http://.../Design_Time_Addresses/Service2/"
****        behaviorConfiguration="foo"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService2"
            contract="Service2Reference.IService2"
            name="WSHttpBinding_IService2">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
        <endpoint address="net.tcp://.../Design_Time_Addresses/Service1/"
****        behaviorConfiguration="foo"
            binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService1"
            contract="Service1Reference.IService1" name="NetTcpBinding_IService1">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
    </client>
  <behaviors>
    <endpointBehaviors>
****  <behavior name="foo">
        <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
Bb answered 25/6, 2013 at 8:48 Comment(5)
Yes, I tried this also. Still not workingHeathenize
@Heathenize hmm, ok; right - to be clear: which end is serializing when this breaks? client? or server?Bb
Server part semms to be ok. Client part is brokenHeathenize
@Heathenize hmmm... that's vexing; unless I'm going crazy, the above should have fixed itBb
@Heathenize yes, that is normalBb
H
5

Add to the config

<behaviors> 
    <endpointBehaviors> 
        <behavior name="Behaviors.EndpointBehavior"> 
            <dataContractSerializer maxItemsInObjectGraph="2147483647" /> 
        </behavior> 
    </endpointBehaviors> 
</behaviors

Then define your client with behavior

<client> 
    <endpoint address=http://localhost:9997/Services/MyService 
        behaviorConfiguration="Behaviors.EndpointBehavior" 
        binding="wsHttpBinding" bindingConfiguration="WSHTTPBinding.Configuration.Client" 
        contract="IAppointments" name="Client.EndpointConfiguration" /> 
</client>

Or do it in code behind

foreach ( var operation in channelFactory.Endpoint.Contract.Operations )   
{   
    var behavior = operation.Behaviors.Find() as DataContractSerializerOperationBehavior;   
    if ( behavior != null )   
    {   
        behavior.MaxItemsInObjectGraph = 2147483647;       
    }   
} 

(Source: http://devlicio.us/blogs/derik_whittaker/archive/2010/05/04/setting-maxitemsinobjectgraph-for-wcf-there-has-to-be-a-better-way.aspx)

I think you have to decorate the server side definition and the client endpoint as well.

Hertahertberg answered 25/6, 2013 at 9:1 Comment(4)
Yes, I tried this also. Still not workingHeathenize
:( try regenerate client config after a rebuildHertahertberg
Ahh, and dont forget to apply this value on server side!Hertahertberg
The code behind version worked for me; the accepted answer only works if you are using a duplex endpoint.Volny
H
1

I solved this problem with adding behavior in C# code.

client.Endpoint.Behaviors.Remove(typeof(CallbackBehaviorAttribute));
client.Endpoint.Behaviors.Add(new CallbackBehaviorAttribute() { MaxItemsInObjectGraph = 2147483647 });

Problem probably is caused because of using ServiceClient constructor with InstanceContext, Binding and EndpointAddress so behavior from app.config is ignored.

Heathenize answered 25/6, 2013 at 10:27 Comment(1)
Could you please explain where this code should go? Is it on instantiating the app? Or before each connection? Thanks. I don't fully understand the constructor issue you describe.Denitadenitrate

© 2022 - 2024 — McMap. All rights reserved.