当前位置: 代码迷 >> C# >> WCF,传输集合出有关问题
  详细解决方案

WCF,传输集合出有关问题

热度:394   发布时间:2016-04-28 08:42:21.0
WCF,传输集合出问题?
本帖最后由 df43rt4t 于 2015-10-27 22:15:13 编辑
WCF,传输List<stirng>出现异常,这个集合的元素不超过5个字符,传输1000个,则没有问题,传输超过1000个出现以下异常:
“System.ServiceModel.CommunicationException”类型的未经处理的异常在 mscorlib.dll 中发生 

其他信息: 套接字连接已中止。这可能是由于处理消息时出错或远程主机超过接收超时或者潜在的网络资源问题导致的。本地套接字超时是“00:01:00”。
配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <customErrors mode="Off" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataBehavior">
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netTcpBinding>
        <binding name="netTcpBindingConfiguration">
          <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647"  maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
          <security mode="None">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service name="Services.MyService" behaviorConfiguration="metadataBehavior">
        <endpoint address="MyService"  binding="netTcpBinding" contract="Contracts.IService" bindingConfiguration="netTcpBindingConfiguration"  />
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:9999/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>


请问,怎么解决?
------解决思路----------------------
NetTcpBinding.MaxReceivedMessageSize默认为64K,可能你要增加配额。
详细见https://msdn.microsoft.com/zh-cn/library/system.servicemodel.nettcpbinding.maxreceivedmessagesize等


<binding name="netTcpBindingConfiguration" maxReceivedMessageSize="...." maxBufferSize...>
------解决思路----------------------
看错误是超时
------解决思路----------------------
应该是数据库循环插入过程的时间过长了,这种情况最好采用wcf的服务端异步方式进行数据库操作,这里给你一个介绍,你可以先看看,目的就是不要让客户端一直等待到超时。
http://www.cnblogs.com/TianFang/archive/2012/12/28/2837771.html
同时,为加快数据库存入时间,可以考虑数据到达服务端的时候不要立即操作数据库,先接收数据组织成datatable,然后利用拷贝的方式存入。
  相关解决方案