Web Service 或 WCF調用時讀取 XML 資料時,超出最大字串內容長度配額(8192)解決方案

來源:互聯網
上載者:User

標籤:

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>

  

 

Web Service 或 WCF調用時讀取 XML 資料時,超出最大字串內容長度配額(8192)解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.