为了预防DOS攻击,WCF的一些配置都有默认的最大值,例如maxBufferSize,默认值是64K。 如果是ASP.NET应用中(使用IIS作为WCF宿主),还要注意HttpRuntime中也有一些类似的配置。传输大数据时,若数据量超过这些默认值就会遇到异常。
哪些配置需要注意呢?
1. 超时设置:
WCF binding 中的sendTimout, receiveTimeout等 (例如basicHttpBinding )
HttpRuntime的executionTimeout
2. 最大值设置:
WCF binding中的maxBufferSize, maxReceivedMessageSize. (例如basicHttpBinding )
WCF binding ReaderQuotas中的maxArrayLength, maxBytesPerRead, maxDepth等
WCF behavior中DataContactSerializer的maxItemsInObjectGraph
HttpRuntime的maxRequestLength
3. 传输大数据,还应考虑使用"流" 的方式传输以及对编码方式的选择http://msdn.microsoft.com/en-us/library/ms733742.aspx
此外, WCF binding中的sendTimeout是在客户端的配置 而receiveTimeout是在服务端的配置:
http://blogs.msdn.com/wenlong/archive/2008/03/10/why-changing-sendtimeout-does-not-help-for-hosted-wcf-services.aspx