Darbio
Darbio

Reputation: 11418

ThreadPools in IIS Express

How do I adjust the thread pool max in IIS Express?

Running the following code from a WebAPI controller:

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

Results in the following:

workers = 2
completions = 2

This is too low to run any other async tasks. When I run it in a console application, I get significantly higher numbers.

workers = 1023
completions = 1000

How do I adjust these numbers for my WebAPI and IIS Express application?

Upvotes: 2

Views: 2624

Answers (1)

Darbio
Darbio

Reputation: 11418

It appears that this might be set by IISExpress/Helios as too low. This has been changed for future releases.

The fix, which is detailed here is to do the following in your code when using IISExpress. I wrapped the code in compiler directives to ensure that this is only compiled in DEBUG build config.

    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
    }

Upvotes: 2

Related Questions