【问题标题】:How to configure concurrency in .NET Core Web API?如何在 .NET Core Web API 中配置并发?
【发布时间】:2017-11-07 13:04:35
【问题描述】:

在过去的 WCF 时代,您可以通过 MaxConcurrentCalls 设置控制服务并发。 MaxConcurrentCalls 默认为 16 个并发调用,但您可以根据需要提高或降低该值。

如何在 .NET Core Web API 中控制服务器端并发?在我们的例子中,我们可能需要限制它,因为太多的并发请求会阻碍整体服务器性能。

【问题讨论】:

    标签: concurrency asp.net-core asp.net-core-webapi


    【解决方案1】:

    ASP.NET Core 应用程序并发由其web server 处理。例如:

    红隼

    var host = new WebHostBuilder()
        .UseKestrel(options => options.ThreadCount = 8)
    

    由于基于 Kestrel 异步实现,不建议将 Kestrel 线程数设置为像 1K 这样的大值。

    更多信息:Is Kestrel using a single thread for processing requests like Node.js?

    在 ASP.NET Core 2.0 Preview 2 中,新的 Limits 属性为 introduced

    您现在可以为以下内容添加限制:

    1. 最大客户端连接数
    2. 最大请求正文大小
    3. 最大请求正文数据速率

    例如:

    .UseKestrel(options =>
    {
        options.Limits.MaxConcurrentConnections = 100;
    }
    

    IIS

    当 Kestrel 在反向代理后面运行时,您可以调整代理本身。例如,您可以在web.configaspnet.config 中配置IIS 应用程序池:

    <configuration>
      <system.web>
        <applicationPool
            maxConcurrentRequestsPerCPU="5000"
            maxConcurrentThreadsPerCPU="0"
            requestQueueLimit="5000" />
      </system.web>
    </configuration>
    

    当然 NginxApache 有自己的并发设置。

    【讨论】:

    • 如果我将更改添加到 aspnet.config,如何应用它们?重启服务器?重启iis?回收应用池?
    • 有谁知道当请求进来并且服务器已经在运行最大线程数时 Kestrel web 主机的行为是什么?
    猜你喜欢
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    相关资源
    最近更新 更多