从 C# 调用 WCF 时如何增加 MaxReceivedMessageSize [重复]
Posted
技术标签:
【中文标题】从 C# 调用 WCF 时如何增加 MaxReceivedMessageSize [重复]【英文标题】:how to increase MaxReceivedMessageSize when calling a WCF from C# [duplicate] 【发布时间】:2012-12-17 12:45:54 【问题描述】:可能重复:The maximum message size quota for incoming messages (65536) has been exceeded
我正在使用 WCF 进行文件上传和下载。上传成功,但是当我下载一个大文件时,我发现了这个错误
错误:传入邮件的最大邮件大小配额 (65536) 已超过。要增加配额,请使用 相应绑定元素上的 MaxReceivedMessageSize 属性。
我的 Service.config 文件有以下代码。
<system.web>
<compilation debug="true" />
<httpRuntime executionTimeout="4800" maxRequestLength="2147483647" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferPoolSize="524288">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
<!--<webHttpBinding>
<binding maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" />
</webHttpBinding>-->
</bindings>
<services>
<service name="WITSService.WITSService">
<clear />
<endpoint binding="basicHttpBinding" contract="WITSService.WITSService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="myEndPointBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
谁能帮我增加 MaxReceivedMessageSize
【问题讨论】:
【参考方案1】:更改 web.config 中的 customBinding 以使用更大的默认值。我选择了 2MB,因为它是一个合理的大小。当然,将其设置为 2GB(如您的代码所示)会起作用,但它确实会让您更容易受到攻击。选择一个大于您的最大请求但又不过大的尺寸。
检查这个:Using Large Message Requests in Silverlight with WCF
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="TestLargeWCF.Web.MyServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="customBinding0">
<binaryMessageEncoding />
<!-- Start change -->
<httpTransport maxReceivedMessageSize="2097152"
maxBufferSize="2097152"
maxBufferPoolSize="2097152"/>
<!-- Stop change -->
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service behaviorConfiguration="Web.MyServiceBehavior" name="TestLargeWCF.Web.MyService">
<endpoint address=""
binding="customBinding"
bindingConfiguration="customBinding0"
contract="TestLargeWCF.Web.MyService"/>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
【讨论】:
【参考方案2】:您需要在客户端配置中设置basicHttpBinding -> MaxReceivedMessageSize。
【讨论】:
以上是关于从 C# 调用 WCF 时如何增加 MaxReceivedMessageSize [重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 Jquery 调用 C# WCF 服务来修复“ERR_ABORTED 400 (Bad Request)”错误?