简体   繁体   中英

Start Stop IIS Application Pool

I found this code:

using System.DirectoryServices;

...

void Recycle(string appPool)
{
    string appPoolPath = "IIS://servername/W3SVC/AppPools/" + appPool;

    using (DirectoryEntry appPoolEntry = new DirectoryEntry(appPoolPath))
    {
        appPoolEntry.Invoke("Recycle", null);
        appPoolEntry.Close();
    }
}

But when I try to use this code I have this error:

Exception has been thrown by the target of an invocation., StackTrace: at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)

What I'm doing wrong?
or how can get info about status of my application pool, and how can I start and stop my app pool without any special permissions?

I'am using built-in account: NetworkService

Try to use Microsoft.Web.Administration.ServerManager and Microsoft.Web.Administration.ApplicationPool classes.

Example:

        var serverManager = new ServerManager();
        var appPool = serverManager.ApplicationPools.FirstOrDefault(ap => ap.Name.Equals("AppPoolName"));
        appPool.Start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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