如何在IIS Express中调整线程池最大值?
从WebAPI控制器运行以下代码:
int workers;
int completions;
System.Threading.ThreadPool.GetMaxThreads(out workers, out completions);
结果如下:
workers = 2
completions = 2
这太低,无法运行任何其他async
任务。当我在控制台应用程序中运行它时,我获得了更高的数字。
workers = 1023
completions = 1000
如何为我的WebAPI和IIS Express应用程序调整这些数字?
答案 0 :(得分:2)
看起来这可能是由IISExpress / Helios设置得太低了。对于将来的版本,这已经改变了。
详细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
}