WCF Error “Maximum number of items that can be serialized or deserialized in an object graph is '65536'”

本小妞迷上赌 提交于 2019-11-26 16:45:07

I just realized that your WPF config file is not right. So, I deleted all my comments because they were assuming a valid WCF configuration. Your WPF config file is not right ... it needs to say "Client" instead of service ... Are you using "Add Service Reference" in Visual Studio? if so, it should have created the correct config file for you.

Otherwise, please refer to MSDN for the correct format for your client config file in your WPF project.

Configuring the below values solved the problem for me.

Client Config:

<system.serviceModel>
<bindings>
<basicHttpBinding>
  <binding name="BasicHttpBinding_IManagementService" closeTimeout="00:01:00"
    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
    maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
    useDefaultWebProxy="true">
    <readerQuotas maxDepth="128" maxStringContentLength="2147483647"
      maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
    <security mode="None">
      <transport clientCredentialType="None" proxyCredentialType="None"
        realm="" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>
  </binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://XXXX/ManagementService.svc"
  binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IManagementService"
  contract="ManagementServiceReference.IManagementService"
  name="BasicHttpBinding_IManagementService" behaviorConfiguration="ManagementServiceBehaviour"/>
</client>
<behaviors>
<endpointBehaviors>
  <behavior name="ManagementServiceBehaviour">
    <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
  </behavior>
</endpointBehaviors>
</behaviors>

Server Config:

<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<behaviors>
<serviceBehaviors>
  <behavior name="ManagementServiceBehaviour">
    <serviceDebug includeExceptionDetailInFaults="true" />
    <serviceMetadata httpGetEnabled="true" />
    <dataContractSerializer ignoreExtensionDataObject="false" maxItemsInObjectGraph="2147483646" />
  </behavior>
  <behavior name="">
    <serviceMetadata httpGetEnabled="true" />
    <serviceDebug includeExceptionDetailInFaults="false" />
  </behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
  <binding name="BasicHttpBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" closeTimeout="01:50:00" openTimeout="01:50:00" sendTimeout="01:50:00" receiveTimeout="01:50:00" >
    <readerQuotas maxDepth="128" maxStringContentLength="8388608" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
  </binding>
</basicHttpBinding>
</bindings>

<services>
<service behaviorConfiguration="ManagementServiceBehaviour" name="BusinessLogic.Facade.EntityFacade.Services.ManagementService">
  <endpoint binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" contract="BusinessLogic.Facade.EntityFacade.Contracts.IManagementService">
    <identity>
      <dns value="" />
    </identity>
  </endpoint>
</service>
</services>
</system.serviceModel>

Have you tried upping the buffer and max received message size as well?

maxBufferSize="6553600" maxBufferPoolSize="52428800" maxReceivedMessageSize="6553600"

Beware of the "dataContractSerializer" element. In my case I got the error mentioned until I put this element as the first item of the parent element "behavior". At least on the client side indeed.

You are returning a generic list or an array which has a size of more than 65536. in your queries, using a select top 60000 or not adding more than 60k elements will solve your problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!