Skip to main content

IIS Manager - applocation pool restart C# code

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace RecycleApplicationPool
{
    class Program
    {

        static void Main(string[] args)
        {
            string appPoolName = "DefaultAppPool";
            string machineName = "LOCALHOST";
            if (args.Length > 0)
                appPoolName = args[0];
            if (args.Length > 1)
                machineName = args[1];

            string error = null;

            DirectoryEntry root = null;
            try
            {
                Console.WriteLine("Restarting Application Pool " + appPoolName + " on " + machineName + "...");
                root = new DirectoryEntry("IIS://" + machineName + "/W3SVC/AppPools/" +appPoolName);
                Console.WriteLine(root.InvokeGet("Name"));
                root.Invoke("Recycle");                
                Console.WriteLine("Application Pool recycling complete...");

            }
            catch(Exception ex)
            {
                error = "Error: Unable to access AppPool: " + ex.Message;                
            }

            
            if ( !string.IsNullOrEmpty(error) )
            {
                Console.WriteLine(error);
                return;
            }
        }
    }
}
http://codepaste.net/5afgf6 
Refer : http://weblog.west-wind.com/posts/2012/Oct/02/A-tiny-Utility-to-recycle-an-IIS-Application-Pool 

Comments

Popular posts from this blog

IPv4 header to requests when a client is using IPv6

  What is Pseudo IPv4? As a stopgap to accelerate the adoption of IPv6, Cloudflare offers Pseudo IPv4 which supports IPv6 addresses in legacy applications expecting IPv4 addresses. The goal is to provide a nearly unique IPv4 address for each IPv6 address, using Class E IPv4 address space, which is designated as experimental and would not normally see traffic. To learn more see  here . Options Add header:  Add additional Cf-Pseudo-IPv4 header only Overwrite headers:  Overwrite the existing Cf-Connecting-IP and X-Forwarded-For headers with a pseudo IPv4 address Cloudflare

IIS 7 Tip # 5 Run a command when Rapid Fail Protection is triggered

Rapid-Fail Protection disables application pools if they crash multiple times within in a specified time period. This prevents the failing application pool from getting into a continuous loop of crashing and restarting. This protects other application pools running on the server as repeated failures can consume lot of system resources. When rapid-fail protection kicks in it stops the application pool that is repeatedly crashing and your clients will start getting a 503 – Service Unavailable error. An administrator will have to manually enable the application pool again. You also have to option to configure an executable to run when ever rapid-fail protection is triggered. For example below I have configured the application pool to restart the IIS service using iisreset.exe … the /rebootonerror will reboot the whole server if iisreset.exe for some reason fails to restart the services. Refer http://blogs.msdn.com/b/vijaysk/archive/2009/03/13/iis-7-tip-5-run-a-command-when-rapid-f...