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

本文关键字:缓冲区 消息 配置 ASP NET API Web | 更新日期: 2023-09-27 18:25:46

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

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

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

如果您是自托管,它是HttpSelfHostConfiguration类的一部分:HttpSelfHostConfiguration类的MSDN文档

它会这样使用:

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

您可能需要查看ASP.NET应用程序的web.config中的httpRuntime部分。它们的名称并不完全相同,但可能有一个类似于你想要做的事情。例如:

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

注:Int32.MaxValue为2147483647

首先我会在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>

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

我解决了这个问题,之前像这个一样进行动态绑定

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;