Thomas
Thomas

Reputation: 2984

Is it possible to get the application pool an applicaiton is running in (without knowing the website beforehand)?

I'm running an ASP.NET application (.NET Framework 4.6) on a Windows Server with IIS 10.

I've read a few examples where you get the application pool for specific sites.

ServerManager manager = new ServerManager();
Site defaultSite = manager.Sites["Default Web Site"];

foreach (Application app in defaultSite.Applications)
{
    Console.WriteLine(
        "{0} is assigned to the '{1}' application pool.", 
        app.Path, app.ApplicationPoolName);
}

But in these examples I have to define the Website (as far as I understand) under which the application is running.

Now when I don't want to fixate that in the source code (as it can change), I'm wondering how can I get under which application pool name is the application running?

Upvotes: 3

Views: 310

Answers (1)

Nkosi
Nkosi

Reputation: 246998

Get the current site name and path via the HostingEnvironment.SiteName and HostingEnvironment.ApplicationVirtualPath respectively.

With them, getting the desired information from the server manager can be done like

using Microsoft.Web.Administration;
using System.Web.Hosting;

//...

var manager = new ServerManager();

var siteName = HostingEnvironment.SiteName;

var site = manager.Sites[siteName];

var applicationPath = HostingEnvironment.ApplicationVirtualPath;

var application = site.Applications[applicationPath];

Console.WriteLine(
    "{0} is assigned to the '{1}' application pool.", 
    application.Path, application.ApplicationPoolName);

var appPoolName = application.ApplicationPoolName;

var applicationPool = manager.ApplicationPools[appPoolName];

Upvotes: 2

Related Questions