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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9453738/
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-09 07:29:17  来源:igfitidea点击:

How do I configure the buffer size and max message size in the ASP.NET Web API

c#restwcf-web-apiasp.net-web-api

提问by suing

We used to have these properties in the WCF Web API configuration.

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

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

回答by Justin Saraceno

If you're self-hosting, it's part of the HttpSelfHostConfiguration class: MSDN Documentation of the HttpSelfHostConfiguration class

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

It would be used like this:

它将像这样使用:

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

回答by Kasey Speakman

You may want to look at httpRuntimesection in web.config of your ASP.NET application. They are not named exactly the same, but there may be an analog for what you're trying to do. For instance:

您可能需要查看ASP.NET 应用程序的 web.config 中的httpRuntime部分。它们的名称并不完全相同,但您尝试执行的操作可能有相似之处。例如:

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

Note: Int32.MaxValueis 2,147,483,647

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

回答by lolporer

first i would have done this configuration in the WCF App.config

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

Then try updaeting the reference in the ASP.NET side which is calling the WCF in the web.config check that the endpoint has changed to the same.

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

回答by David Neira

I solve this problem making a dynamic binding before like this

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

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;