c# - IIS Express 中的线程池

标签 c# .net asp.net-web-api task iis-express

如何调整 IIS Express 中的线程池最大值?

从 WebAPI Controller 运行以下代码:

int workers;
int completions;
System.Threading.ThreadPool.GetMaxThreads(out workers, out completions);

结果如下:

workers = 2
completions = 2

这个值太低,无法运行任何其他异步任务。当我在控制台应用程序中运行它时,我得到的数字明显更高。

workers = 1023
completions = 1000

如何调整我的 WebAPI 和 IIS Express 应用程序的这些数字?

最佳答案

看来这可能被 IISExpress/Helios 设置得太低。这已针对 future 版本进行了更改。

修复,详细here是在使用 IISExpress 时在代码中执行以下操作。我将代码包装在编译器指令中,以确保它仅在 DEBUG 构建配置中编译。

    public Startup()
    {
#if DEBUG
        // HACK
        // Set worker threads as HElios sets them too low for IIS Express
        // https://github.com/aspnet/Home/issues/94
        int newLimits = 100 * Environment.ProcessorCount; // this is actually # cores (including hyperthreaded cores) 
        int existingMaxWorkerThreads;
        int existingMaxIocpThreads;
        System.Threading.ThreadPool.GetMaxThreads(out existingMaxWorkerThreads, out existingMaxIocpThreads);
        System.Threading.ThreadPool.SetMaxThreads(Math.Max(newLimits, existingMaxWorkerThreads), Math.Max(newLimits, existingMaxIocpThreads));
#endif
    }

关于c# - IIS Express 中的线程池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34780226/

相关文章:

c# - 在 C# 中调用标量函数

c# - Entity Framework 6 中的 Nullable DateTime 属性在值为 null 时在保存时抛出异常

c# - ASP.NET 身份电子邮件验证 token 始终无效

c# - 从 WebAPI 调用返回 Sitecore 媒体项目

c# - 如何在 C# 中生成 UTC Unix 时间戳

c# - 在打印作业期间更换打印机托盘

c# - Azure Devops Visual Studio 测试任务无法发现具有 TestCaseSource 属性的 Nunit 测试

c# - 删除 DataGridView 中两个单元格之间的行分隔符

c# - 如何使用 WndProc 捕获 ESC 按键?

jquery - Web API OWIN 从 $.AJAX POST withCredentials :true 接收空数据