如何在ASP.NET Web API中配置缓冲区大小和最大消息大小

时间:2012-02-26 14:06:52

标签: c# rest wcf-web-api asp.net-web-api

我们曾经在WCF Web API配置中拥有这些属性。

            MaxBufferSize = int.MaxValue,
            MaxReceivedMessageSize = int.MaxValue,

4 个答案:

答案 0 :(得分:17)

如果您是自托管,那么它是HttpSelfHostConfiguration类的一部分: MSDN Documentation of the HttpSelfHostConfiguration class

它会像这样使用:

var config = new HttpSelfHostConfiguration(baseAddress);
config.MaxReceivedMessageSize = int.MaxValue;
config.MaxBufferSize = int.MaxValue;

答案 1 :(得分:15)

您可能希望查看ASP.NET应用程序的web.config中的httpRuntime部分。它们的名称并不完全相同,但可能会有类似的内容。例如:

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="16384" requestLengthDiskThreshold="16384"/>
  </system.web>
</configuration>

注意:Int32.MaxValue为2,147,483,647

答案 2 :(得分:4)

首先我会在WCF App.config

中完成此配置
<endpoint address ="" binding="basicHttpBinding" bindingConfiguration="basicHttp" contract="AddSubService.IService1">
      <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>

    <!-- Metadata Endpoints -->
    <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
    <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>

</services>

<bindings>
  <basicHttpBinding>
    <binding name="basicHttp" allowCookies="true"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
      <readerQuotas maxDepth="32"
           maxArrayLength="200000000"
           maxStringContentLength="200000000"/>
    </binding>
  </basicHttpBinding>
</bindings>

然后尝试更新ASP.NET端的引用,该引用在web.config中调用WCF,检查端点是否已更改为相同。

答案 3 :(得分:1)

我解决了这个问题,在此之前进行动态绑定

BasicHttpBinding bind = new BasicHttpBinding();
bind.OpenTimeout = bind.CloseTimeout = bind.SendTimeout = bind.ReceiveTimeout = new TimeSpan(0, 30, 0);
bind.MaxBufferSize = int.MaxValue;
bind.MaxReceivedMessageSize = int.MaxValue;