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>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM