Reputation: 361
I want to access the name and status of the application pool with .NET core
We used to either manually check the status or use different tools to check the status of the application pools in IIS. But the update and removal with these tools is still a manual job which people tend to forget.
I tried to do this:
public static void GetApplicationPoolNames()
{
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 this is only for my local IIS installation. I like to access a different IIS server and retrieve this information. Is it nescessary that I host the application within the same IIS pool? Or can it be a standalone project?
Upvotes: 1
Views: 2557
Reputation: 1695
If anyone finds this question and just needs the name of the apppool in .NET Core
Environment.UserName
holds the username, which by default is the same as the application pool name in IIS.
Upvotes: 0
Reputation: 3042
Here are two methods to get information of application pool. The first one is just like in your code, you can easily and quickly get the information of the application pool on the local IIS.
public static void GetPoolName()
{
ServerManager server = new ServerManager();
ApplicationPoolCollection apc = server.ApplicationPools;
foreach (ApplicationPool pool in apc)
{
bool autostart = pool.AutoStart;
string poolname = pool.Name;
Console.WriteLine(poolname);
Console.WriteLine(autostart);
}
}
The second one use DirectoryEntry to get app pool information, including iis in remote server. You need to set server name, username and password so that have permission. More information can refer to this link
public static void GetPoolName()
{
string uname = "your username";
string pwd = "passwod";
string serverName = "remote server";
DirectoryEntries apppools = new DirectoryEntry("IIS://" + serverName + "/W3SVC/AppPools", uname, pwd).Children;
foreach (DirectoryEntry pool in apppools)
{
int code = (int)pool.InvokeGet("AppPoolState");
string status;
switch (code)
{
case 2:
status = "Running";
break;
case 4:
status = "Stopped";
break;
default:
status = "Unknown";
break;
}
Console.WriteLine(pool.Name);
Console.WriteLine(status);
}
}
Upvotes: 1