解决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。

 

posted @ 2013-12-12 09:41  不起名字了  阅读(9584)  评论(0编辑  收藏  举报