15

I want to recycle the application pool through my application.

Previously I was storing the application pool name in my database and using that to recycle. But It happened in the past that we moved apps from one app pool to another and sometimes we forget to update the app pool name in the database.

So I am thinking to get the app pool name through the application and use that for recycling.

1

3 Answers 3

28

In many cases it might be enough to just read the name of the application pool from the environment variable:

var apppool = System.Environment.GetEnvironmentVariable(
                  "APP_POOL_ID", EnvironmentVariableTarget.Process);
Sign up to request clarification or add additional context in comments.

2 Comments

Can you provide some documentation around this environment variable? Who defines it, and what other variables are defined by IIS?
@julealgon There is a brief mention of this specific environment variable here: learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/…
21

Modified version of @Razon answer :)

public static string GetCurrentApplicationPoolName()
{
    ServerManager manager = new ServerManager();
    string DefaultSiteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
    Site defaultSite = manager.Sites[DefaultSiteName];
    string appVirtualPath = HttpRuntime.AppDomainAppVirtualPath;

    string appPoolName = string.Empty;
    foreach (Application app in defaultSite.Applications)
    {
        string appPath = app.Path;
        if (appPath == appVirtualPath)
        {
            appPoolName = app.ApplicationPoolName;
        }   
    }
    return appPoolName;
}

4 Comments

IMHO, better answer than Razon answer
Combine the link with the code and that should be the answer.
Caution: This code might throw a COM exception if the "Load User Profile" setting in the AppPool on your IIS is not set to "True"
Please Note: make sure you wrap the ServerManager into a Using statement so that its disposed of once it goes out of scope (its un-managed code)
5

May this can help: ApplicationPoolName Property

Namespace: Microsoft.Web.Administration Assembly: Microsoft.Web.Administration (in Microsoft.Web.Administration.dll)

http://msdn.microsoft.com/en-us/library/microsoft.web.administration.application.applicationpoolname(v=vs.90).aspx

1 Comment

And just in case, Install-Package Microsoft.Web.Administration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.