Web Service 或 WCF调用时读取 XML 数据时,超出最大字符串内容长度配额(8192)解决方法

1.调用服务时服务

  当我们使用 Web Service 或 WCF 服务时,常把读取的数据转化为string类型(xml格式),当数据量达到一 定数量时,会出现以下异常:

    错误格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ (命名空间)进行反序列化时出错: InnerException 消息是“反序列化对象异常,读取 XML 数据时,超出最大字符串内容长度配额 (8192)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额。

2.原因及解决方案
 

 WCF传输大数据时,因为WCF本身的安全机制导致的,限制了客户端与服务器资源传输大小,当传输的数据超过上限后会产生异常。

     发送大数据:在WCF服务端解决

                  NetTcpBinding binding =  new NetTcpBinding();

            binding.MaxReceivedMessageSize= 2147483647(更改这个数字) ;

     接收大数据:在WCF客户端解决

            NetTcpBinding binding =  new NetTcpBinding();

            binding.ReaderQuotas = new XmlDictionaryReaderQuotas()

                  { MaxStringContentLength = 2147483647(更改这个数字) };

Web Service 调用时,在绑定代理端是,添加如下BasicHttpBinding:

 有两种方法处理:
  第一种:在调用时传入Binding参数。

    

 BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None)
                  { MaxReceivedMessageSize = int.MaxValue,
                    MaxBufferSize = int.MaxValue,
                    ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
                   { MaxStringContentLength = 2147483647 }
                  }
DLTEST.ServiceReference2.CS_WebServiceSoapClient svs = new DLTEST.ServiceReference2.CS_WebServiceSoapClient(binding);  

  第二种方法,改一下调用客户端的配置文件app.config

    增加binding节点下增加 readerQuotas节点控制

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="CS_WebServiceSoap" >
                  <readerQuotas maxDepth="64" maxStringContentLength="8192000" maxArrayLength="16384000"
                            maxBytesPerRead="4096000" maxNameTableCharCount="16384000" />
                </binding>
              <binding name="CS_WebServiceSoap1" >
                <readerQuotas maxDepth="64" maxStringContentLength="8192000" maxArrayLength="16384000"
                            maxBytesPerRead="4096000" maxNameTableCharCount="16384000" />
              </binding>
                <binding name="CS_WebServiceSoap2" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://10.0.0.251:100/WebService/CS_WebService.asmx"
                binding="basicHttpBinding" bindingConfiguration="CS_WebServiceSoap"
                contract="ServiceReference1.CS_WebServiceSoap" name="CS_WebServiceSoap" />
            <endpoint address="http://localhost:90/WebService/CS_WebService.asmx"
                binding="basicHttpBinding" bindingConfiguration="CS_WebServiceSoap1"
                contract="ServiceReference2.CS_WebServiceSoap" name="CS_WebServiceSoap1" />
            <endpoint address="http://localhost:3383/WebService/CS_WebService.asmx"
                binding="basicHttpBinding" bindingConfiguration="CS_WebServiceSoap2"
                contract="ServiceReference3.CS_WebServiceSoap" name="CS_WebServiceSoap2" />
        </client>
    </system.serviceModel>
</configuration>

  

原文地址:https://www.cnblogs.com/spring_wang/p/5888267.html