码迷,mamicode.com
首页 > 其他好文 > 详细

远程服务器返回了意外相应:(413) Request Entity Too Large。

时间:2016-04-03 15:55:53      阅读:701      评论:0      收藏:0      [点我收藏+]

标签:

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

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


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

  1. <bindings>??
  2. ????<basicHttpBinding>??
  3. ????????<binding?name="BasicHttpBinding_WcfService"?maxBufferSize="2147483647"??
  4. ????????????maxReceivedMessageSize="2147483647">??
  5. ????????????<security?mode="None"?/>??
  6. ????????</binding>??
  7. ????</basicHttpBinding>??
  8. </bindings>??

<bindings>

<basicHttpBinding>

<binding name="BasicHttpBinding_WcfService" maxBufferSize="2147483647"

maxReceivedMessageSize="2147483647">

<security mode="None" />

</binding>

</basicHttpBinding>

</bindings>

?

然后在Client节里应用Binding Configuration

  1. <client>??
  2. ????????????<endpoint?address="http://localhost:22000/Service/WcfService.svc"??
  3. ????????????????binding="basicHttpBinding"?bindingConfiguration="BasicHttpBinding_WcfService"??
  4. ????????????????contract="WcfServiceReference.WcfService"?name="BasicHttpBinding_WcfService"?/>??
  5. </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设置,指定最大接受数据:

  1. <system.serviceModel>??
  2. .......??
  3. <bindings>??
  4. ????<basicHttpBinding>??
  5. ????????<binding?name="LargeDataTransferServicesBinding"?maxReceivedMessageSize="2147483647"??
  6. ??messageEncoding="Text"?transferMode="Streamed"?sendTimeout="00:10:00"?/>??
  7. ????</basicHttpBinding>??
  8. </bindings>??
  9. ............??
  10. </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属性:

  1. <system.serviceModel>??
  2. ......??
  3. ????<services>??
  4. ??????<service?name="FitFindFW.WCFService.SalaryManage.WageManageService">??
  5. ????????<endpoint?address=""?binding="basicHttpBinding"??
  6. ??????????bindingConfiguration="LargeDataTransferServicesBinding"?name="XXX"?contract="FitFindFW.WCFService.SalaryManage.IWageManageService"?/>??
  7. ??????</service>??
  8. ????</services>??
  9. ............??
  10. </system.serviceModel>??
  11. 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节下加上

  1. <httpRuntimemaxRequestLengthhttpRuntimemaxRequestLength="102400"?/>??

<httpRuntimemaxRequestLength="102400" />


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

远程服务器返回了意外相应:(413) Request Entity Too Large。

标签:

原文地址:http://www.cnblogs.com/qq260250932/p/5349861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!