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

Posted

技术标签:

【中文标题】WCF 错误“对象图中可以序列化或反序列化的最大项目数为 \'65536\'”【英文标题】:WCF Error "Maximum number of items that can be serialized or deserialized in an object graph is '65536'"WCF 错误“对象图中可以序列化或反序列化的最大项目数为 '65536'” 【发布时间】:2011-11-20 13:39:18 【问题描述】:

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

可以在一个序列中序列化或反序列化的最大项目数 对象图是'65536'

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

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:

<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>

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

我还尝试了以下方法:

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

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

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

提前致谢!

【问题讨论】:

【参考方案1】:

配置以下值解决了我的问题。

客户端配置:

<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>

服务器配置:

<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>

【讨论】:

嗨,我尝试使用您的代码(绑定和行为,但我收到此错误消息:Content Type text/xml; charset=utf-8 was not supported by service . 你知道我为什么会收到这个错误信息吗?【参考方案2】:

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

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

【讨论】:

这会放在服务宿主项目的 web.config 中吗?还是客户端的app.config?还是两者兼而有之? 在应用程序的 app.config 上。 这对我来说是未知的领域... :-) 这是正确的方法吗? 我把你的建议加到了应用的app.config中,好像没什么区别 这一行解决了问题,但 Ram 得到了 +1,因为他告诉我们应该把它放在哪里【参考方案3】:

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

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

【讨论】:

我从两个配置文件的 system.service 模型部分添加了更多内容 啊哈!...我认为您可能正在做某事...我会检查添加“客户端”部分。仅供参考 - 这是我从另一个团队继承的项目,我不知道他们使用什么方法为 WPF 应用程序生成 app.config 文件。 请尝试在 Visual Studio 中添加“服务引用”,并且不要忘记在添加服务引用之前删除 app.config 中的整个“system.servicemodel”部分......你的生活将变得容易多了……然后您可以通过添加“maxItemsInObjectGraph”的东西来自定义您的配置文件……祝您好运。【参考方案4】:

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

【讨论】:

【参考方案5】:

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

【讨论】:

以上是关于WCF 错误“对象图中可以序列化或反序列化的最大项目数为 '65536'”的主要内容,如果未能解决你的问题,请参考以下文章

WCF 请求返回错误响应

WCF 错误:相对端点地址

WCF 错误处理

如何使用故障契约和 jquery ajax 处理 wcf 错误

WCF 显示超时错误

WCF + JSONP:总是收到“方法不允许”错误消息