使用WCF傳輸大數據時,我們都會碰到如題中出現的錯誤信息,出現這個問題是因為WCF本身的安全機制導致的,限制了客戶端與服務器資源傳輸大小,那我們如何還解決這個問題呢?
針對這個問題,我們要分發送、接受兩個方面來解決。
發送大數據:在WCF服務端解決
NetTcpBinding binding = new NetTcpBinding();
binding.MaxReceivedMessageSize= 2147483647(更改這個數字) ;
接受大數據:在WCF客戶端解決
NetTcpBinding binding = new NetTcpBinding();
binding.ReaderQuotas = new XmlDictionaryReaderQuotas() { MaxStringContentLength = 2147483647(更改這個數字) };
我們即可以使用如上述通過代碼配置,我們同樣也可以使用配置文件進行配置(在binding節中)。
public static System.ServiceModel.BasicHttpBinding Binding() { //讀取 XML 數據時,超出最大字符串內容長度配額 (8192)。 System.ServiceModel.BasicHttpBinding bing = new System.ServiceModel.BasicHttpBinding(); bing.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas() { MaxStringContentLength = 2147483647 }; //(更改這個數字) return bing; }
public static API.iClient api = new API.iClient(Binding(), new System.ServiceModel.EndpointAddress("http://192.168.1.11:4417/WebService.svc"));