1

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?

1

2 Answers 2

1

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);
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

This code gives me the following exception : System.Runtime.InteropServices.COMException: 'Unknown error (0x80005000)' Any toughts on that?
Please check the IIS Metabase and IIS 6 configuration compatibility to install it in windows features.
We have IIS version 10.xxx. Do I need to install any compatibility software to run with IIS6? Maybe i'm misunderstandig this subject. Can you provide me with some documentation or am I missing something obvious?
IIS 7 does not install an ADSI provider by default. You can enable it however as a Role Service for the IIS Web Server. In your control panel, choose programs, turn windows features on or off, then Internet Information Services,Web management compatibility, check IIS metabase and IIS6 configuration
0

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.

1 Comment

Can you provide some backing documentation for that claim that it is the same as the IIS apppool name?

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.