1

How can I know the number of IIS threads consumed at a given time by my ASP.NET Web API service?

I can count .NET CLR threads of the running application by the below code snippet:

int number = Process.GetCurrentProcess().Threads.Count;

but my question is about IIS threads.

IIS v8.5, Windows Server 2012

ITExpert
  • 323
  • 1
  • 2
  • 8

1 Answers1

2

In IIS a service (ASP.NET application) is running using an application pool. So if the name of the application pool is known at runtime, you can determine the number of threads for your application pool:

var applicationPoolName = "YOUR APPLICATION POOL";

using (var serverManager = new Microsoft.Web.Administration.ServerManager())
{
    var applicationPool = serverManager.ApplicationPools[applicationPoolName];

    var threadsCount = applicationPool
                            .WorkerProcesses
                            .Select(p => Process.GetProcessById(p.ProcessId))
                            .SelectMany(p => p.Threads.Cast<ProcessThread>())
                            .Count();
}
Andriy Tolstoy
  • 5,690
  • 2
  • 31
  • 30