Using Microsoft.web.administration to print out applications in appPool and Appdomain in worker process

Quickly helping my collegue-Isha in getting the count of applications in the given apppool [Crude way but it works] using Microsoft.Web.Administration API

 

image

Now the Default Website has 7 application spanned across different AppPools. I would like to get the 4 as the count for DefaultAppPool as seen above

Following code would enumerate ALL* the website and check if application Pool assigned is the one I’m looking for .If yes then increase the counter

class Program
{
    static void Main(string[] args)
    {
        ServerManager serverManager = new ServerManager();
        int i=0;
        foreach (Site mysite in serverManager.Sites)
        {
            foreach (Application app in mysite.Applications)
            {
                Console.WriteLine(
                    "Found application with the following path: {0}", app.Path + " -- " + app.ApplicationPoolName);
                if(app.ApplicationPoolName=="DefaultAppPool")
                {
                    i++;
                }
            }
        }
        Console.WriteLine("\n Total for DefaultAppPool::" + i.ToString());
    }

 

Output:

image

Scenario2

Incase you would like to know the number of application domains loaded in a worker process for particular AppPool[MyTestAppPool]

This MyTestAppPool has 5 application in it but not* necessarily all will be loaded. AppDomain is created once we get the request for that application

class Program
    {
        static void Main(string[] args)
        {

                    ServerManager serverManager = new ServerManager();
                    ApplicationPool apppool = serverManager.ApplicationPools["MyTestAppPool"];

                    foreach (WorkerProcess workerProcess in apppool.WorkerProcesses)
                    {
                        Console.WriteLine( workerProcess.AppPoolName.ToString() + "\n");
                      foreach (ApplicationDomain appDom in workerProcess.ApplicationDomains)
                      {
                        Console.WriteLine("\t+--ApplicationDomain Found: {0}", appDom.Id);
                        Console.WriteLine(     "\t\t|--AppDomPhysPath: {0}", appDom.PhysicalPath);
                        Console.WriteLine( "\t\t+--AppDomVirtPath: {0}", appDom.VirtualPath);
                       }

                    }
        } 
  }

Output:

image

Enjoy Smile