如何调整 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/