Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Application Pool name through code (C#, ASP.net)

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.

like image 592
dIvYaNsH sInGh Avatar asked Sep 04 '14 11:09

dIvYaNsH sInGh


People also ask

How do I get to application pools?

In the Connections pane, expand the server name, and then click Application Pools. In the Actions pane, click Add Application Pool.... In the Add Application Pool dialog box, enter the name of the application pool in the Name: box, in the . NET Framework version: drop-down list select the .

How do I get to application pool in IIS?

Expand the IIS server. Choose Application Pool. On the right pane, click Add Application Pool or right-click the middle pane and choose Add Application Pool. When the Add Application Pool window appears, type the name of the application pool on the Namefield (e.g. OSCE).


2 Answers

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;
    }
like image 123
dIvYaNsH sInGh Avatar answered Oct 11 '22 20:10

dIvYaNsH sInGh


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);
like image 34
Knaģis Avatar answered Oct 11 '22 19:10

Knaģis