在从客户端向WCF服务端传送较大数据(>65535B)的时候,发现程序直接从Reference的BeginInvoke跳到EndInvoke,
没有进入服务端的Service实际逻辑中(报错),怀疑是由于数据过大超出限定导致的。
问题是我实际发送的数据是刚刚从WCF服务端接收过来的,一来一去,数据量差别并不大。
然后发现,在客户端和服务端实际使用的是不同的配置,对于客户端,在添加ServiceReference时自动生成的ServiceReferences.ClientConfig文件中
system.serviceModel节下有这样的设置:
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<bindingname="BasicHttpBinding_IBIZFormService"maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<securitymode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpointaddress="http://localhost:1446/CBIZFormService.svc"
binding="basicHttpBinding"bindingConfiguration="BasicHttpBinding_IBIZFormService"
contract="ServiceReference1.IBIZFormService"name="BasicHttpBinding_IBIZFormService" />
</client>
</system.serviceModel>
</configuration>
在Binding里指定了最大缓存字节数和最大接受字节数,相当于2G的大小!除非传一整套连续剧,一般是够用了。
而在服务端,Web.config文件里,Bindings节是空的,而Service也没有指定bindingConfiguration属性,那么它们采用的就是默认的65535的大小。
问题找到,解决就比较容易了:
在Bindings节添加新的Binding设置,指定最大接受数据:
<bindings>
<basicHttpBinding>
<bindingname="BasicHttpBinding_IBIZFormService2222"maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<securitymode="None" />
</binding>
</basicHttpBinding>
</bindings>
之后给相应的Service指定bindingConfiguration属性:
<services>
<servicename="Be.BPM.Service.Form.CBIZFormService">
<host>
<baseAddresses>
<addbaseAddress="http://localhost:8733/Design_Time_Addresses/Be.BPM.Service.Form/CBIZFormService/"/>
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!--除非完全限定,否则地址将与上面提供的基址相关-->
<endpointaddress=""binding="basicHttpBinding"contract="Be.BPM.Interface.Form.IBIZFormService"bindingConfiguration="BasicHttpBinding_IBIZFormService2222" >
<!--
部署时,应删除或替换下列标识元素,以反映
用来运行所部署服务的标识。删除之后,WCF 将
自动推断相应标识。
-->
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!--元数据交换终结点供相应的服务用于向客户端做自我介绍。-->
<!--此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
<endpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>
</service>
</services>
这样就可以从客户端发送足够大的数据了。
P.S.:
.net默认只能传4M的文件,所以尽管设定了Wcf两端的配置,还是超不出.net的限定,所以如果要传输大文件,还需要在System.Web节下加上
<httpRuntimemaxRequestLength="102400" />
这里的单位是KB,这样就可以传100M的文件了。当然,这么大的文件,最好还是分段传输比较好。
http://www.cnblogs.com/smjack/archive/2009/02/27/1399353.html
原文:http://www.cnblogs.com/lyf681888/p/3584906.html