2016-01-14 64 views
1

如何調整IIS Express中的線程池max?IIS Express中的ThreadPools

運行從的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應用程序調整這些數字?

+1

嗨,是不是IIS Express限制在這種方式?我的意思是它可以通過設計... – ipavlu

回答

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 
    }