预期的应用程序池未显示。而不是显示通用应用程序池

问题描述 投票:0回答:1

我用 C# 编写了代码来启动和停止 IIS 池。我使用了 ServerManager 方法并尝试使用 ServerManager().ApplicationPools 但它只显示以下五个应用程序池,而不是我在 IIS 中的池。我的 IIS 版本是 10.0.19041.1 。下面是提到的代码。

我得到的 IIS 池

“Clr4集成应用程序池” “Clr4经典应用程序池” “Clr2集成应用程序池” “Clr2经典应用程序池” “非托管经典应用程序池”

 private void PerformAction(string action)
    {
       
        // Get selected server and app pools
        string selectedServer = ddlServers.SelectedValue;
        List<string> selectedAppPools = new List<string>();

        foreach (ListItem item in cblAppPools.Items)
        {
            if (item.Selected)
            {
                selectedAppPools.Add(item.Value);
            }
        }

        // Perform start or stop action on selected app pools
        foreach (string appPool in selectedAppPools)
        {
            try
            {
                using (ServerManager serverManager = new ServerManager())
                {
                    Console.WriteLine($"Trying to access App Pool: {appPool}");

                    ApplicationPool selectedAppPool = serverManager.ApplicationPools[appPool];
                    
                    if (action.Equals("start", StringComparison.OrdinalIgnoreCase))
                    {
                        selectedAppPool.Start();
                    }
                    else if (action.Equals("stop", StringComparison.OrdinalIgnoreCase))
                    {
                        selectedAppPool.Stop();
                    }
                }
            }
            catch (Exception ex)
            {
                // Handle exception (log it, display it to the user, etc.)
            }
        }

        // Refresh the UI or display a message to the user
        LoadAppPools(selectedServer);
    }
}
c# asp.net iis iis-10 application-pool
1个回答
0
投票

您可以使用此代码作为参考:

// Writes out the applications and the application pool names 
// associated with the applications under the default Web site.
public void GetApplicationPoolNames()
{
   ServerManager manager = new ServerManager();
   Site defaultSite = manager.Sites["Default Web Site"];

   foreach (Application app in defaultSite.Applications)
   {
       Console.WriteLine(
        "{0} is assigned to the '{1}' application pool.", 
        app.Path, app.ApplicationPoolName);
   }
}   
© www.soinside.com 2019 - 2024. All rights reserved.