陋室铭
永远也不要停下学习的脚步(大道至简至易)

posts - 2169,comments - 570,views - 413万

在从客户端向WCF服务端传送较大数据(>65535B)的时候,发现程序直接从Reference的BeginInvoke跳到EndInvoke,没有进入服务端的Service实际逻辑中,怀疑是由于数据过大超出限定导致的。

报错信息:远程服务器返回了意外响应: (400) Bad Request。

问题是我实际发送的数据是刚刚从WCF服务端接收过来的,一来一去,数据量差别并不大。

然后发现,在客户端和服务端实际使用的是不同的配置,对于客户端,在添加ServiceReference时自动生成的ServiceReferences.ClientConfig文件中system.serviceModel节下有这样的设置:

<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>

在Binding里指定了最大缓存字节数和最大接受字节数,相当于2G的大小!除非传一整套连续剧,一般是够用了。

而在服务端,Web.config文件里,Bindings节是空的,而Service也没有指定bindingConfiguration属性,那么它们采用的就是默认的65535的大小。

问题找到,解决就比较容易了:

在Bindings节添加新的Binding设置,指定最大接受数据:

<bindings>
    <basicHttpBinding>
        <binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647"
  messageEncoding="Text" transferMode="Streamed" sendTimeout="00:10:00" />
    </basicHttpBinding>
</bindings>
之后给相应的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>

这样就可以从客户端发送足够大的数据了。

P.S.:

.net默认只能传4M的文件,所以尽管设定了Wcf两端的配置,还是超不出.net的限定,所以如果要传输大文件,还需要在System.Web节下加上

    <httpRuntimemaxRequestLength="102400" />

这里的单位是KB,这样就可以传100M的文件了。当然,这么大的文件,最好还是分段传输比较好。

posted on   宏宇  阅读(1830)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2009-09-01 导出excel乱码问题(小技巧)
< 2010年9月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示