如何正确使用Microsoft.web.administration以从网页启动应用程序池



我的任务是找到从网页重新启动应用程序池的方法。IIS由我们自己的IT部门托管,因此必要时需要更改所需的任何安全设置。

我正在使用microsoft.web.administration.dll,并为我的本地IIS(7.5)尝试此操作:

            ServerManager serverManager = new ServerManager();
            ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools;
            foreach (ApplicationPool applicationPool in applicationPoolCollection)
            {
                appPoolNames.Add(applicationPool.Name);
                // If the applicationPool is stopped, restart it.
                if (applicationPool.State == ObjectState.Stopped)
                {
                    applicationPool.Start();
                }
            }
            // CommitChanges to persist the changes to the ApplicationHost.config.
            serverManager.CommitChanges();

如果我将此逻辑放在Winform中并以本地管理员的身份运行,我将在本地IIS中启动我的应用程序池。但是,如果我将相同的逻辑放在网页中,则在此行中出现错误而失败,而错误:

if (applicationPool.State == ObjectState.Stopped)
Exception: System.ComponentModel.Win32Exception: Access is denied

我认为这是由于安全性造成的,但是我已经将本地管理用户提供给了我的应用程序池。有没有办法在网页中实现这一目标?

我假设网页与您要重新启动的网页不在同一应用程序池中)。因此,假设它是一个不同的应用程序池,那么重要的是网页应用程序池的身份 - 这将需要具有重新启动目标应用程序池的权限。

最新更新