midhun b

midhun b

  • NA
  • 20
  • 27.5k

Programatically recycle Remote IIS server application pools

Nov 14 2013 5:08 AM
Hi,

I have a requirement where i have to recycle Application pools preset on remote IIS server.

I have achieved all the requirement and working great but the only error i am getting at the recycle. Here is my code.

 System.DirectoryServices.DirectoryEntry Root = new DirectoryEntry("IIS://ServerName/W3SVC/AppPools", "Username", "password");
              
               if (Root == null)
                {
                    Console.Write("No Child in AppPool");
                }
                else
                {
                                    
                    foreach (DirectoryEntry dir in Root.Children)
                    {
                        System.DirectoryServices.PropertyCollection pr = dir.Properties;
                        //AppPoolNameis an existing application pool in  iis
                        if (dir.Name.Trim() == "AppPoolName")
                        {
                            DesiredAppPoolName = dir.Name;

                            using (DirectoryEntry appPoolEntry = new DirectoryEntry("IIS://Servername/W3SVC/AppPools/" + DesiredAppPoolName.Trim(), "username", "password"))
                            {

                              
                                int status = (int)appPoolEntry.InvokeGet("AppPoolState");
                                //Status = 4 represents application pool is stopped
                                if (status != 4)
                                {
                                   //This Line is giving me exception Unknown name exception
                                   appPoolEntry.Invoke("Recycle");
                                                                  
                                }
                                appPoolEntry.Dispose();
                            }
                                                 
                            break;
                        
                        }
                    
                    }

                }
            }

Any One please help.

Thanks in advance
Midhun Bezawada

Answers (7)