c# - 异常 : Maximum number of items that can be serialized or deserialized in an object graph is '65536'

标签 c# visual-studio-2010 wcf serialization datacontractserializer

<分区>

我看了其他帖子,但没有找到解决问题的方法。

我的服务配置文件:

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

我的客户端配置文件:

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

为什么我仍然得到异常:

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

似乎忽略了<dataContractSerializer maxItemsInObjectGraph="2147483647"/>在两个App.config文件。为什么?

最佳答案

在客户端,查看标记为 **** 的 3 行:

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

关于c# - 异常 : Maximum number of items that can be serialized or deserialized in an object graph is '65536' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17292811/

相关文章:

c# - 在现有 WCF SOAP 服务中添加 Http header 不起作用

wcf - 从 SSIS 使用 WCF 服务

c# - 在 WCF REST 服务中禁用 XML 示例?

visual-studio-2010 - fatal error ,无法继续 : The method 'ToStringJS' is not generic

c++ - visual studio 2010 中的 graphics.h 问题

javascript - Hide/Show Div below select 取决于是否选择了一个选项

wcf - WCF 客户端发送的数据已损坏

c# - 如何实际使用 TypeConverter 以及它与 System.Convert.ToXXX() 有何不同

c# - 在变化的位置插入琴弦

c# - 你如何使用 String.IsNullOrEmpty 单元测试代码?