简体   繁体   English

如何正确使用Microsoft.Web.Administration从网页启动应用程序池?

[英]how to use Microsoft.Web.Administration properly to start an application pool from a web page?

I was tasked to find a way to restart an application pool from a web page. 我的任务是找到一种从网页重新启动应用程序池的方法。 The IIS is hosted by our own IT department, so any security settings needed will be changed if necessary. IIS由我们自己的IT部门托管,因此如有必要,将更改所需的任何安全设置。

I am using Microsoft.Web.Administration.dll and trying this for my local IIS (7.5): 我正在使用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();

If I put this logic in a winform and run as local admin, my app pool in local IIS will be started. 如果我将此逻辑放在winform中并以本地管理员身份运行,则将启动本地IIS中的应用程序池。 However, if I put the same logic in a webpage, it fails with error at this line with error: 但是,如果我在网页中放置相同的逻辑,则此行将在此行出现错误并出现错误:

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

I think this is due to security, but I already give a local admin user to my app pool. 我认为这是出于安全原因,但我已经将本地管理员用户分配给我的应用程序池。 Is there a way to achieve this in a web page? 有没有办法在网页上实现这一目标?

I assume the web page is not in the same application pool as the one you are trying to restart, which would not be a good idea (because the web page would suddenly lose its process host). 我假设该网页与您要重新启动的网页不在同一个应用程序池中,这不是一个好主意(因为该网页会突然失去其进程宿主)。 So assuming it is a different application pool, then it is the identity of the web page's application pool which matters - this will need to have permissions to restart the target application pool. 因此,假设它是一个不同的应用程序池,那么重要的是网页应用程序池的标识-这将需要具有重新启动目标应用程序池的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM