从 C# 调用 WCF 时如何增加 MaxReceivedMessageSize

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14134107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:46:31  来源:igfitidea点击:

how to increase MaxReceivedMessageSize when calling a WCF from C#

c#asp.net.netweb-serviceswcf

提问by MindFresher

Possible Duplicate:
The maximum message size quota for incoming messages (65536) has been exceeded

可能重复:
已超出传入邮件的最大邮件大小配额 (65536)

I am using WCF for file uploading and downloading. uploading is successful but when i downloading a large file i found this error

我正在使用 WCF 进行文件上传和下载。上传成功但是当我下载一个大文件时我发现这个错误

Error : The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

错误:已超出传入邮件的最大邮件大小配额 (65536)。要增加配额,请在适当的绑定元素上使用 MaxReceivedMessageSize 属性。

My Service.config file has the following code.

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

can any one help me how to increase MaxReceivedMessageSize

任何人都可以帮助我如何增加 MaxReceivedMessageSize

采纳答案by Pranay Rana

Change the customBinding in the web.config to use larger defaults. I picked 2MB as it is a reasonable size. Of course setting it to 2GB (as your code suggests) will work but it does leave you more vulnerable to attacks. Pick a size that is larger than your largest request but isn't overly large.

更改 web.config 中的 customBinding 以使用更大的默认值。我选择了 2MB,因为它是一个合理的大小。当然,将其设置为 2GB(如您的代码所示)会起作用,但它确实会让您更容易受到攻击。选择比您最大的要求大但又不过大的尺寸。

Check this : Using Large Message Requests in Silverlight with WCF

检查这个:在 Silverlight 和 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> 

回答by Tilak

You need to set basicHttpBinding -> MaxReceivedMessageSizein the client configuration.

您需要在客户端配置中设置basicHttpBinding -> MaxReceivedMessageSize