【转】解决WCF大数据量传输 ,System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接...
開發中所用的數據需要通過WCF進行數據傳輸,結果就遇到了WCF大量傳輸問題 也就是提示System.Net.Sockets.SocketException: 遠程主機強迫關閉了一個現有的連接
網上解決方案都是千篇一律互相轉發的,并且沒有明確的解決方案或者按照,各個博客中的解決方案都沒能解決這個問題。
為此我整整浪費了一天時間用來解決這個問題,而且用了最笨的辦法一點點的嘗試網上所查到的方案。對于精研WCF來說的這可能是一個小問題,但是對于僅僅了解wcf,一知半解的會很困惑。將解決方案貼出來希望能幫助到一樣遇到此問題和沒有深入研究過wcf的程序猿們快速解決這個問題.
?
首先網上所說的解決大數據量傳輸需要給修改客戶端與服務端的配置屬性
maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" 單位是字節數,最大為2G
?
服務端配置
??? <bindings>
????? <basicHttpBinding>
??????? <binding name="GEDemoServiceBinding" closeTimeout="00:05:00" openTimeout="00:05:00" sendTimeout="00:05:00"maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
????????? <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" maxDepth="32" />
??????? </binding>
????? </basicHttpBinding>
??? </bindings>
客戶端配置
?? <bindings>
????? <basicHttpBinding>
??????? <binding name="BasicHttpBinding_IGEDemoService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
????????? <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
????????? <security mode="None">
??????????? <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
??????????? <message clientCredentialType="UserName" algorithmSuite="Default" />
????????? </security>
??????? </binding>
????? </basicHttpBinding>
??? </bindings>
?
但是這遠遠不夠這只是修改了wcf傳輸通道的空間容量。
但是由于WCF傳輸時肯定會涉及到自定義對象的傳輸,因此服務端與客戶端通過wcf傳輸時需要進行序列化因此需要修改dataContractSerializer配置屬性
?
修改dataContractSerializer節點以后的配置
服務端配置
?<behavior name="GE_DemoService.GEDemoServiceBehavior">
????????? <serviceMetadata httpGetEnabled="true" />
????????? <serviceDebug includeExceptionDetailInFaults="true" />
?? <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
??????? </behavior>
?
?
客戶端配置
?
? <!--Service Configure--> ??
??? <endpoint address="http://localhost:10081/GEDemoService/" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IGEDemoService" contract="DemoService.IGEDemoService" name="BasicHttpBinding_IGEDemoService"?behaviorConfiguration="endpoinmaxItemsInObjectGraph"?/> ?
?? ??? </client>
?<behaviors> ?????
<endpointBehaviors> ?????
?? <behavior name="endpoinmaxItemsInObjectGraph"> ???????
?? <dataContractSerializer maxItemsInObjectGraph="2147483647"/> ?????
?? </behavior> ???
?? </endpointBehaviors> ???
</behaviors>
?
至此修改配置完成,沒怎么大批量測試但是解決我當時遇到的問題是可以了,傳輸數據2000以上。沒有問題了原來只要超過1000條數據則就會掛掉因此此方案是可行的
。
最后需要注意的是在客戶端配置文件中增加httpRuntime配置節點
<httpRuntime maxRequestLength="512000" executionTimeout="120" />
?
這是因為:
如果WCF以IIS作為宿主,WCF傳輸數據量的能力還受到HttpRunttime設置的制約,可能需要修改HttpRunttime(在system.Web節中)的MaxRequestLength屬性。MaxRequestLength屬性表示請求的最大大小(以千字節為單位)。默認大小為 4096 KB (4 MB),允許的最大值是2097151。
總結
以上是生活随笔為你收集整理的【转】解决WCF大数据量传输 ,System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 18000M速率!新华三首款Wi-Fi
- 下一篇: 狼叔休·杰克曼再次确诊新冠 暂停出演百老