.net WCF 错误“对象图中可以序列化或反序列化的最大项目数为‘65536’”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7476853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:55:19  来源:igfitidea点击:

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

.netwcfdatacontractserializerxmlserializerservicebehavior

提问by Scot

I am receiving the following error on a WCF call:

我在 WCF 调用中收到以下错误:

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

对象图中可以序列化或反序列化的最大项目数为“65536”

I've read a ton of forum posts and many of them mention modifying the app.config and web.config to specify new behavior to allow larger object graphs. I've done that and this is what I have in those files:

我已经阅读了大量论坛帖子,其中很多都提到修改 app.config 和 web.config 以指定新行为以允许更大的对象图。我已经做到了,这就是我在这些文件中的内容:

App.Config on the WPF project:

WPF 项目上的 App.Config:

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="">
      <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
    </behavior>

  </endpointBehaviors>

</behaviors>

<services>
  <service name="digiPM.Shell.LogOutPMSEMRService.PMSEMRLogOutService">
    <!--<endpoint address="" binding="basicHttpBinding" contract="digiPM.Shell.LogOutPMSEMRService.IPMSEMRLogOutService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8732/Design_Time_Addresses/digiPM.Shell.LogOutPMSEMRService/PMSEMRLogOutService/" />
      </baseAddresses>
    </host>-->
    <endpoint address="" binding="netTcpBinding" name="NetTcpBindingEndpoint" contract="digiPM.Shell.LogOutPMSEMRService.IPMSEMRLogOutService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" name="MexTcpBidingEndpoint" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8732/Design_Time_Addresses/digiPM.Shell.LogOutPMSEMRService/PMSEMRLogOutService/" />
      </baseAddresses>
    </host>
  </service>
</services>

<!--binding info - removed this for the sake of readability for this post -->

web.config on the service project:

服务项目上的 web.config:

<system.serviceModel>

<bindings>
  <wsHttpBinding>
      <binding name="WSHttpBinding_Services" closeTimeout="01:10:00" openTimeout="01:10:00" receiveTimeout="01:10:00" sendTimeout="01:10:00" bypassProxyOnLocal="false" 
               transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647" 
               messageEncoding="Mtom" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="4096" maxStringContentLength="2147483647" maxArrayLength="524288" maxBytesPerRead="524288" maxNameTableCharCount="524288" />
          <reliableSession ordered="true" inactivityTimeout="01:10:00" enabled="false" />

          <security mode="None">

          </security>
      </binding>
  </wsHttpBinding>
</bindings>

<behaviors>
  <serviceBehaviors>
    <behavior name="digiPM.Service.Behavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>

  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="customObjectQuota">
      <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
    </behavior>
  </endpointBehaviors>
</behaviors>

<services>
  <service behaviorConfiguration="digiPM.Service.Behavior"
    name="digiPM.Service.AddressCrudService">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_Services"
                name="AddressCrudServiceEndPoint" bindingNamespace="urn:Dawliasoft.Sculpture"  contract="digiPM.Services.Contracts.IAddressCrudService" behaviorConfiguration="customObjectQuota" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"  behaviorConfiguration="customObjectQuota" />
  </service>

 <!--<more services defined with same configuration as above..>-->

  </services>


</system.serviceModel>

This, however, hasn't helped. Note that the services referenced in the APP.CONFIG are not the services I am having issues with.

然而,这并没有帮助。请注意,APP.CONFIG 中引用的服务不是我遇到问题的服务。

I've also tried the following:

我还尝试了以下方法:

  • added these attributes to the service implementation: [DataContract(IsReference=true)], [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, MaxItemsInObjectGraph = 2147483646)]

  • wrote a custom DataContractSerializerOperationBehavior class that set MaximumObjectsInGraph and IsReference behavior. Also added custom Attribute to apply this the service implementations. For the sake of clutter, I didn't post the code, but can add it if anyone thinks it would be beneficial.

  • 将这些属性添加到服务实现中:[DataContract(IsReference=true)]、[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, MaxItemsInObjectGraph = 2147483646)]

  • 编写了一个自定义 DataContractSerializerOperationBehavior 类,用于设置 MaximumObjectsInGraph 和 IsReference 行为。还添加了自定义属性以应用此服务实现。为了混乱,我没有发布代码,但如果有人认为它会有所帮助,可以添加它。

Thoughts? Ideas? Where do I go from here?

想法?想法?我从这里去哪里?

Thanks in advance!

提前致谢!

采纳答案by Nabheet

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.

我刚刚意识到您的 WPF 配置文件不正确。因此,我删除了所有评论,因为它们假设了有效的 WCF 配置。您的 WPF 配置文件不正确……它需要说“客户端”而不是服务……您是否在 Visual Studio 中使用“添加服务引用”?如果是这样,它应该为您创建了正确的配置文件。

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

否则,请参阅MSDN以获取 WPF 项目中客户端配置文件的正确格式。

回答by Ram

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>

回答by Josh

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

您是否也尝试过增加缓冲区和最大接收消息大小?

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

回答by Roland STAUB

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.

当心“dataContractSerializer”元素。在我的情况下,我收到了提到的错误,直到我将此元素作为父元素“行为”的第一项。至少在客户端确实如此。

回答by U?ur Gümü?han

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.

您将返回一个大小超过 65536 的通用列表或数组。在您的查询中,使用 select top 60000 或不添加超过 60k 的元素将解决您的问题。