首页 技术 正文
技术 2022年11月15日
0 收藏 402 点赞 4,368 浏览 2934 个字

开发中所用的数据需要通过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。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,906
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,739
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,492
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,130
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,293