Search code examples
c#iisiis-7requestworker

How to get the current list of Worker Request from IIS using C#?


I am writing the code for a RESTful web service status page. I was wondering if there is away to get the current request from IIS into C#.

I am using IIS 7.0 and the info I want is under

IIS > Worker procecces > ASP.NET v4.0 > Requests

enter image description here


Solution

  • You can use the GetRequests method of the WorkerProcess type. This type is located in the Microsoft.Web.Administration assembly which can be installed using this unofficial nuget package or by adding a reference to this dll %WinDir%\System32\InetSrv\Microsoft.Web.Administration.dll

    Example:

    using (ServerManager manager = new ServerManager())
    {
        while (true)
        {
            var requests = manager.ApplicationPools
                                    .Where(pool => pool.Name == "FooPool")
                                    .SelectMany(pool => pool.WorkerProcesses)
                                    .SelectMany(wp => wp.GetRequests(10));
    
            Console.WriteLine(requests.Count());
            Thread.Sleep(100);
        }
    }
    

    Note: You need to have the IIS Request Manager feature enabled and also run with a user with sufficient permissions. See How do I see currently executing web request on IIS 8