WinForms调用ASP.NET WebService遇序列化数量上限65536错误求解决
解决“对象图中可序列化或反序列化的最大项数为65536”错误
我懂你现在的头疼——搜遍相关问题全是WCF的解决方案,但你用的是传统ASP.NET WebService(ASMX)搭配WinForms客户端,完全不对症对吧?别慌,咱们专门针对ASMX的场景来搞定这个问题:
服务器端(ASP.NET WebService)配置修改
你需要在WebService项目的web.config里调整序列化的最大项数,找到<system.web>节点,添加或者修改<webServices>配置:
<system.web> <webServices> <soapExtensionTypes /> <soapServerProtocolFactory type="System.Web.Services.Protocols.SoapServerProtocolFactory, System.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <protocols> <add name="HttpGet"/> <add name="HttpPost"/> </protocols> <!-- **关键配置**:调整序列化最大项数到int最大值 --> <serialization maxItemsInObjectGraph="2147483647" /> </webServices> </system.web>
把maxItemsInObjectGraph设为2147483647(int类型的最大值),足够覆盖绝大多数数据量的场景。
客户端(WinForms)配置修改
你的WinForms项目里的配置文件(app.config)也需要同步调整,找到<system.web>节点(没有的话直接新建),添加:
<system.web> <webServices> <serialization maxItemsInObjectGraph="2147483647" /> </webServices> </system.web>
另外,如果你是通过添加服务引用生成的客户端,还可以在<system.serviceModel>里调整绑定的消息大小和读取配额,避免触发其他数据量限制:
<system.serviceModel> <bindings> <basicHttpBinding> <binding name="STKBinder" maxReceivedMessageSize="2147483647"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> </binding> </basicHttpBinding> </bindings> </system.serviceModel>
代码层面的额外调整
看你代码里手动创建了STKBinding,那也得在代码里设置对应的配额参数:
Dim binding As New STKBinding("STKBinder") ' 调大消息接收上限 binding.MaxReceivedMessageSize = 2147483647 ' 调整所有读取配额到最大值 With binding.ReaderQuotas .MaxDepth = 2147483647 .MaxStringContentLength = 2147483647 .MaxArrayLength = 2147483647 .MaxBytesPerRead = 2147483647 .MaxNameTableCharCount = 2147483647 End With
最后别忘了重启WebService服务器和你的WinForms客户端,确保所有配置都生效。这个方案是专门针对ASMX WebService的,和WCF的配置逻辑不同,应该能解决你遇到的序列化项数超限问题。
内容的提问来源于stack exchange,提问作者Daniel Gee
相关产品推荐
相关产品推荐

