在从客户端向WCF服务端传送较大数据(>65535B)的时候,发现程序直接从Reference的BeginInvoke跳到EndInvoke,没有进入服务端的Service实际逻辑中,怀疑是由于数据过大超出限定导致的。
问题是我实际发送的数据是刚刚从WCF服务端接收过来的,一来一去,数据量差别并不大。
然后发现,在客户端和服务端实际使用的是不同的配置,对于客户端,在添加ServiceReference时自动生成的ServiceReferences.ClientConfig文件中system.serviceModel节下有这样的设置:
-
<bindings>
-
<basicHttpBinding>
-
<binding name="BasicHttpBinding_WcfService" maxBufferSize="2147483647"
-
maxReceivedMessageSize="2147483647">
-
<security mode="None" />
-
</binding>
-
</basicHttpBinding>
-
</bindings>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_WcfService" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
然后在Client节里应用Binding Configuration:
-
<client>
-
<endpoint address="http://localhost:22000/Service/WcfService.svc"
-
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WcfService"
-
contract="WcfServiceReference.WcfService" name="BasicHttpBinding_WcfService" />
-
</client>
<client>
<endpoint address="http://localhost:22000/Service/WcfService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WcfService"
contract="WcfServiceReference.WcfService" name="BasicHttpBinding_WcfService" />
</client>
在Binding里指定了最大缓存字节数和最大接受字节数,相当于2G的大小!除非传一整套连续剧,一般是够用了。
而在服务端,Web.config文件里,Bindings节是空的,而Service也没有指定bindingConfiguration属性,那么它们采用的就是默认的65535的大小。
问题找到,解决就比较容易了:
在Bindings节添加新的Binding设置,指定最大接受数据:
-
<system.serviceModel>
-
.......
-
<bindings>
-
<basicHttpBinding>
-
<binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647"
-
messageEncoding="Text" transferMode="Streamed" sendTimeout="00:10:00" />
-
</basicHttpBinding>
-
</bindings>
-
............
-
</system.serviceModel>
<system.serviceModel>
.......
<bindings>
<basicHttpBinding>
<binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647"
messageEncoding="Text" transferMode="Streamed" sendTimeout="00:10:00" />
</basicHttpBinding>
</bindings>
............
</system.serviceModel>
之后给相应的Service指定bindingConfiguration属性:
-
<system.serviceModel>
-
......
-
<services>
-
<service name="FitFindFW.WCFService.SalaryManage.WageManageService">
-
<endpoint address="" binding="basicHttpBinding"
-
bindingConfiguration="LargeDataTransferServicesBinding" name="XXX" contract="FitFindFW.WCFService.SalaryManage.IWageManageService" />
-
</service>
-
</services>
-
............
-
</system.serviceModel>
-
contract : 命名空间 + 类名
<system.serviceModel>
......
<services>
<service name="FitFindFW.WCFService.SalaryManage.WageManageService">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="LargeDataTransferServicesBinding" name="XXX" contract="FitFindFW.WCFService.SalaryManage.IWageManageService" />
</service>
</services>
............
</system.serviceModel>
contract : 命名空间 + 类名
这样就可以从客户端发送足够大的数据了。
P.S.:
.net默认只能传4M的文件,所以尽管设定了Wcf两端的配置,还是超不出.net的限定,所以如果要传输大文件,还需要在System.Web节下加上
-
<httpRuntimemaxRequestLengthhttpRuntimemaxRequestLength="102400" />
<httpRuntimemaxRequestLength="102400" />
这里的单位是KB,这样就可以传100M的文件了。当然,这么大的文件,最好还是分段传输比较好。