標籤:
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)解決方案