如何使用 C# 从 IIS 获取当前的 Worker Request 列表?

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

我正在为 RESTful Web 服务状态页面编写代码。我想知道是否有办法将 IIS 的当前请求获取到 C# 中。

我正在使用 IIS 7.0,我想要的信息在

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

您可以使用 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 或添加对此 dll 的引用 %WinDir%\System32\InetSrv\Microsoft.Web.Administration.dll

示例:

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);
    }
}

注意:您需要启用 IIS 请求管理器功能,并且 运行 具有足够权限的用户。参见 How do I see currently executing web request on IIS 8