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

14

我在进行一个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行为,并添加自定义属性以将其应用于服务实现。出于杂乱的考虑,我没有发布代码,但如果有人认为有益,我可以添加它。 想法?建议?我接下来该做什么?
提前感谢!
5个回答

16

下面这些配置值解决了我的问题。

客户端配置:

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

嗨,我尝试使用您的代码(绑定和行为),但是我收到了以下错误信息: 内容类型text/xml; charset=utf-8不受服务支持。 您有任何线索为什么我会收到这个错误信息吗? - NoOne

6

你尝试过增加缓冲区和最大接收消息大小吗?

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

这个要放在服务主机项目的 web.config 中吗?还是客户端的 app.config 中?还是两者都要? - Scot
在应用程序的app.config文件中。 - Josh
这对我来说是未知领域... :-) 这是正确的方法吗? <bindings> <basicHttpBinding> <binding maxBufferPoolSize="2147483646" maxBufferSize="2147483646" maxReceivedMessageSize="2147483646" /> </basicHttpBinding> </bindings> - Scot
我已经将你的建议添加到了应用程序的app.config文件中,但似乎并没有产生任何影响。 - Scot
这行代码解决了问题,但是Ram得到了+1,因为他告诉我们在哪里放置它。 - gillonba
奇怪的是,以下代码不起作用,但将其放在app.config中可以(其中hcs是我的服务...我正在使用asmx...是的,我知道...)((System.ServiceModel.HttpBindingBase)hcs.Endpoint.Binding).MaxReceivedMessageSize = Int32.MaxValue;((System.ServiceModel.HttpBindingBase)hcs.Endpoint.Binding).MaxBufferSize = Int32.MaxValue; ((System.ServiceModel.HttpBindingBase)hcs.Endpoint.Binding).MaxBufferPoolSize = Int32.MaxValue; - Tim

4
我刚刚意识到你的WPF配置文件不正确。因此,我删除了所有我的评论,因为它们假定一个有效的WCF配置。你的WPF配置文件不正确…它需要说“客户端”而不是“服务”…你正在使用Visual Studio中的“添加服务引用”吗?如果是这样,它应该已经为你创建了正确的配置文件。
否则,请参考MSDN了解在WPF项目中客户端配置文件的正确格式。

我从两个配置文件中添加了更多的system.service model部分。 - Scot
啊哈!我觉得你可能有点头绪了……我会尝试添加“客户端”部分。顺便提一下,这是我从另一个团队接手的项目,我不知道他们用什么方法生成WPF应用程序的app.config文件。 - Scot
请尝试在Visual Studio中添加“服务引用”,在添加服务引用之前,请不要忘记删除您的app.config中的整个“system.servicemodel”部分...这样您的生活将变得更加轻松...然后,您可以通过添加“maxItemsInObjectGraph”等内容来自定义配置文件...祝你好运。 - Nabheet

1

注意“dataContractSerializer”元素。 在我的情况下,在父元素“behavior”中将此元素放置为第一项之前,我遇到了提到的错误。 至少在客户端方面确实如此。


0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接