【问题标题】:How do I configure the buffer size and max message size in the ASP.NET Web API如何在 ASP.NET Web API 中配置缓冲区大小和最大消息大小
【发布时间】:2012-03-16 06:14:32
【问题描述】:

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

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

【问题讨论】:

  • 我在 ASP.NET Web API 项目中收到 413 Request Entity Too Large 错误。使用config.MaxReceivedMessageSize = int.MaxValue; 帮助了我

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


【解决方案1】:

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

它会这样使用:

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

【讨论】:

  • 谢谢,但我想在 Asp.Net 中托管。曾经有一个WebApiConfiguration类
  • 这似乎是一个可能的解决方案,我将不得不对其进行测试。是的,REST 服务中的 2gb 资源太疯狂了。我们有粗粒度的服务,我讨厌每条消息大约 5 mb。呸!!!
【解决方案2】:

您可能想查看 ASP.NET 应用程序的 web.config 中的 httpRuntime 部分。它们的名称并不完全相同,但可能有一个类似的东西来代表您正在尝试做的事情。例如:

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

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

【讨论】:

  • 这是请求而不是响应。
【解决方案3】:

首先我会在 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 端的引用,检查端点是否已更改为相同。

【讨论】:

    【解决方案4】:

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

    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;
    

    【讨论】:

      猜你喜欢
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      相关资源
      最近更新 更多