【问题标题】:The InnerException message was 'Maximum number of items that can be serialized or deserialized in an object graph is '65536'.InnerException 消息是“对象图中可以序列化或反序列化的最大项目数为“65536”。
【发布时间】:2018-09-26 22:55:50
【问题描述】:

我可以看到这个问题已经被问过好几次了,但是它们都是关于 WCF 的。找了一阵子,一直没找到解决这个错误的办法。

我有一个从 Web 服务中提取数据的 WinForms 应用程序。 Web 服务是用 WebForms/asp.net 编写的。

我连接到网络服务的代码是:

Dim _DownloadStock As srShopDownload.shop_downloadsSoapClient
Dim binding As New STKBinding("STKBinder")
NewEndPoint = New EndpointAddress("https://www.example.com/web_services/downloads.asmx")
            _DownloadStock = New srShopDownload.shop_downloadsSoapClient(binding._Binder, NewEndPoint)
_StockcodesList = _DownloadStock.GetStockcodes

我的 _Binder 代码是

Public Sub New(ByVal BinderName As String)

    _Binder = New BasicHttpBinding()
    _Binder.Name = BinderName

    _Binder.CloseTimeout = TimeSpan.FromMinutes(1)
    _Binder.OpenTimeout = TimeSpan.FromMinutes(1)
    _Binder.ReceiveTimeout = TimeSpan.FromMinutes(10)
    _Binder.SendTimeout = TimeSpan.FromMinutes(1)
    _Binder.AllowCookies = False
    _Binder.BypassProxyOnLocal = False
    _Binder.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard

    _Binder.MaxBufferSize = 100000000
    _Binder.MaxBufferPoolSize = 12000000
    _Binder.MaxReceivedMessageSize = 100000000

    _Binder.MessageEncoding = WSMessageEncoding.Text
    _Binder.TextEncoding = System.Text.Encoding.UTF8
    _Binder.TransferMode = TransferMode.Streamed
    _Binder.UseDefaultWebProxy = True

    _Binder.ReaderQuotas.MaxDepth = 2147483647
    _Binder.ReaderQuotas.MaxStringContentLength = 2147483647
    _Binder.ReaderQuotas.MaxArrayLength = 2147483647
    _Binder.ReaderQuotas.MaxBytesPerRead = 2147483647
    _Binder.ReaderQuotas.MaxNameTableCharCount = 2147483647

    _Binder.Security.Transport.ClientCredentialType = HttpClientCredentialType.None
    _Binder.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None
    _Binder.Security.Transport.Realm = ""

    _Binder.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName
    _Binder.Security.Message.AlgorithmSuite = Security.SecurityAlgorithmSuite.Default
    _Binder.Security.Mode = BasicHttpSecurityMode.Transport

End Sub

在web服务端,它只是一个基本的功能:

<WebMethod()>
Public Function GetStockcodes() As List(Of Stockcodes)

    Return GetStockcodes()

End Function

我已经看到您应该更改客户端和服务器上的 serviceBehaviors 的答案,但我不知道如何在不使用 WCF 的情况下进行。

【问题讨论】:

  • 我试图重现这一点,但我没有达到 WebServices 的限制。甚至不是 1000000 个对象的列表,也不是 1000 个对象的列表,每个对象都有 1000 个子对象。您确定 GetStockcodes 不依赖于另一个 WCF 服务并且您的异常不会从那里重新抛出吗?您可以检查您的消息以查看异常是来自服务器端还是客户端?
  • 我实际上找到了解决方案。它是特定于机器的。因此,您需要调整 .NET machine.config 中的设置。我试图发布答案,但 XML 没有显示,即使它是“代码”。我只是不想因为代码格式而对答案投反对票。

标签: asp.net vb.net web-services


【解决方案1】:

我找到了一个解决方法,只是放在这里以防万一有人遇到同样的问题。

  1. 在 PC 上打开此文件夹。它是 .NET Framework 文件夹:%windir%\Microsoft.NET\

  2. 在此目录中,您应该会看到以下文件夹:Framework 和 Framework64(在 64 位环境中)。这两个都包含一些具有特定 .NET 版本名称的文件夹(例如 v2.0.50727)。您需要浏览每个文件夹并查看它是否包含 CONFIG 目录以及一个名为 machine.config 的文件。 (或者只搜索 machine.config)

  3. 打开编辑每个 machine.config 文件并在 system.serviceModel 节点下添加以下子节点:

<commonBehaviors>
    <endpointBehaviors>
        <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </endpointBehaviors>
    <serviceBehaviors>
        <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </serviceBehaviors>
</commonBehaviors> 
  1. 编辑后应如下所示:

<system.serviceMode>
    (...)
    <extensions>
        (...)
    </extensions>
    <client>
        (...)
    </client>
    <commonBehaviors>
          <endpointBehaviors>
              <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          </endpointBehaviors>
          <serviceBehaviors>
              <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          </serviceBehaviors>
    </commonBehaviors> 
    (...)
</system.serviceModel>
  1. 如果配置文件中缺少system.serviceModel节点,需要手动创建该节点。

  2. 重新启动应用程序。如果仍然无法正常工作,您可能需要重新启动电脑。

我在link 找到了解决方案。只需将答案放在这里,以防将来页面出现故障。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 2011-11-20
    • 2013-06-21
    • 2012-02-29
    • 1970-01-01
    • 2011-09-29
    相关资源
    最近更新 更多