在從客戶(hù)端向WCF服務(wù)端傳送較大數(shù)據(jù)(>65535B)的時(shí)候, 客戶(hù)端會(huì)報(bào)錯(cuò)。什么遠(yuǎn)程服務(wù)器沒(méi)反應(yīng)之類(lèi)的。
問(wèn)題是我實(shí)際發(fā)送的數(shù)據(jù)是剛剛從WCF服務(wù)端接收過(guò)來(lái)的,一來(lái)一去,數(shù)據(jù)量差別并不大。
然后發(fā)現(xiàn),在客戶(hù)端和服務(wù)端實(shí)際使用的是不同的配置,對(duì)于客戶(hù)端,在添加ServiceReference時(shí)自動(dòng)生成的ServiceReferences.ClientConfig文件中system.serviceModel節(jié)下有這樣的設(shè)置:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_WcfService" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
然后在Client節(jié)里應(yīng)用Binding Configuration:
<client>
<endpoint address="http://localhost:22000/Service/WcfService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WcfService"
contract="WcfServiceReference.WcfService" name="BasicHttpBinding_WcfService" />
</client>
在Binding里指定了最大緩存字節(jié)數(shù)和最大接受字節(jié)數(shù),相當(dāng)于2G的大小!除非傳一整套連續(xù)劇,一般是夠用了。
而在服務(wù)端,Web.config文件里,Bindings節(jié)是空的,而Service也沒(méi)有指定bindingConfiguration屬性,那么它們采用的就是默認(rèn)的65535的大小。
問(wèn)題找到,解決就比較容易了:
在Bindings節(jié)添加新的Binding設(shè)置,TB指定最大接受數(shù)據(jù):
<bindings>
<basicHttpBinding>
<binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647"
messageEncoding="Text" transferMode="Streamed" sendTimeout="00:10:00" />
</basicHttpBinding>
</bindings>
之后給相應(yīng)的Service指定bindingConfiguration屬性:
<service behaviorConfiguration="Server.Service.WcfServiceBehavior"
name="Server.Service.WcfService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="LargeDataTransferServicesBinding" contract="Server.Service.WcfService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
這樣就可以從客戶(hù)端發(fā)送足夠大的數(shù)據(jù)了。
P.S.:
asp.net默認(rèn)只能傳4M的文件,所以盡管設(shè)定了Wcf兩端的配置,還是超不出.net的限定,所以如果要傳輸大文件,還需要在System.Web節(jié)下加上
<httpRuntime maxRequestLength="102400" />
這里的單位是KB,這樣就可以傳100M的文件了。當(dāng)然,這么大的文件,最好還是分段傳輸比較好。