Created
September 23, 2013 13:13
-
-
Save yemrekeskin/6670216 to your computer and use it in GitHub Desktop.
IIS Application Pool Controller Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.Web.Administration; | |
| // C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll | |
| public class AppPoolController | |
| { | |
| // Singleton Class | |
| private static AppPoolController _current = null; | |
| private static object _lockObj = new object(); | |
| public static AppPoolController Current | |
| { | |
| get | |
| { | |
| if (_current == null) | |
| { | |
| lock (_lockObj) | |
| { | |
| if (_current == null) | |
| { | |
| _current = new AppPoolController(); | |
| } | |
| } | |
| } | |
| return _current; | |
| } | |
| } | |
| public IList<ApplicationPool> AppPools { get; set; } | |
| public int RetryCount { get; set; } | |
| private ServerManager serverManager; | |
| public AppPoolController() | |
| { | |
| this.serverManager = new ServerManager(); | |
| Intialize(); | |
| } | |
| private void Intialize() | |
| { | |
| this.AppPools = GetAppPoolList(); | |
| this.RetryCount = 3; | |
| } | |
| public IList<string> GetAppPoolNames() | |
| { | |
| List<string> appPoolNames = new List<string>(); | |
| foreach (var item in this.serverManager.ApplicationPools) | |
| appPoolNames.Add(item.Name); | |
| return appPoolNames; | |
| } | |
| private IList<ApplicationPool> GetAppPoolList() | |
| { | |
| List<ApplicationPool> appPools = new List<ApplicationPool>(); | |
| foreach (var item in this.serverManager.ApplicationPools) | |
| appPools.Add(item); | |
| return appPools; | |
| } | |
| public ApplicationPool FindAppPool(string appPoolName) | |
| { | |
| return this.serverManager.ApplicationPools.First(p => p.Name == appPoolName); | |
| } | |
| public void Stop() | |
| { | |
| foreach (ApplicationPool item in AppPools) | |
| { | |
| try | |
| { | |
| StopAppPool(item); | |
| } | |
| catch (SystemCriticalException ex) | |
| { | |
| CriticalExceptionLogger.Log(ex, "", item); | |
| } | |
| } | |
| } | |
| public void Stop(ApplicationPool appPool) | |
| { | |
| try | |
| { | |
| StopAppPool(appPool); | |
| } | |
| catch (SystemCriticalException ex) | |
| { | |
| CriticalExceptionLogger.Log(ex, "", appPool); | |
| } | |
| } | |
| public void Stop(string appPoolName) | |
| { | |
| try | |
| { | |
| ApplicationPool appPool = FindAppPool(appPoolName); | |
| StopAppPool(appPool); | |
| } | |
| catch (SystemCriticalException ex) | |
| { | |
| CriticalExceptionLogger.Log(ex, "", appPoolName); | |
| } | |
| } | |
| private void StopAppPool(ApplicationPool applicationPool) | |
| { | |
| ObjectState state = applicationPool.State; | |
| switch (state) | |
| { | |
| case ObjectState.Started: | |
| applicationPool.Stop(); | |
| Console.WriteLine("Application Pool {0}'s state has gone from {1} to {2}", applicationPool.Name, state, applicationPool.State); | |
| break; | |
| case ObjectState.Starting: | |
| case ObjectState.Unknown: | |
| for (int i = 0; i < RetryCount; i++) | |
| { | |
| Console.WriteLine("Application Pool {0}'s state is {1}. Waiting for state to become Started", applicationPool.Name, state); | |
| state = applicationPool.State; | |
| if (applicationPool.State == ObjectState.Started) { break; } | |
| } | |
| if (state == ObjectState.Started) | |
| { | |
| applicationPool.Start(); | |
| Console.WriteLine("Application Pool {0}'s state has gone from {1} to {2}", applicationPool.Name, state, applicationPool.State); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Error starting Application Pool {0}: Application Pool never Started", applicationPool.Name); | |
| } | |
| break; | |
| case ObjectState.Stopped: | |
| case ObjectState.Stopping: | |
| Console.WriteLine("Application Pool {0} was already in a {1} state and has not been modified", applicationPool.Name, state); | |
| break; | |
| default: | |
| Console.WriteLine("Error stopping Application Pool {0}: Unexpected ObjectState \"{1}\"", applicationPool.Name, state); | |
| break; | |
| } | |
| state = applicationPool.State; | |
| for (int i = 0; i < RetryCount && state != ObjectState.Stopped; i++) | |
| { | |
| Console.WriteLine("Application Pool {0}'s state is {1}. Waiting for state to become Stopped", applicationPool.Name, state); | |
| state = applicationPool.State; | |
| } | |
| this.serverManager.CommitChanges(); | |
| } | |
| public void Start() | |
| { | |
| foreach (ApplicationPool item in AppPools) | |
| { | |
| try | |
| { | |
| StartAppPool(item); | |
| } | |
| catch (SystemCriticalException ex) | |
| { | |
| CriticalExceptionLogger.Log(ex, "", item); | |
| } | |
| } | |
| } | |
| public void Start(ApplicationPool appPool) | |
| { | |
| try | |
| { | |
| StartAppPool(appPool); | |
| } | |
| catch (SystemCriticalException ex) | |
| { | |
| CriticalExceptionLogger.Log(ex, "", appPool); | |
| } | |
| } | |
| public void Start(string appPoolName) | |
| { | |
| try | |
| { | |
| ApplicationPool appPool = FindAppPool(appPoolName); | |
| StartAppPool(appPool); | |
| } | |
| catch (SystemCriticalException ex) | |
| { | |
| CriticalExceptionLogger.Log(ex, "", appPoolName); | |
| } | |
| } | |
| private void StartAppPool(ApplicationPool applicationPool) | |
| { | |
| ObjectState state = applicationPool.State; | |
| switch (state) | |
| { | |
| case ObjectState.Stopped: | |
| applicationPool.Start(); | |
| Console.WriteLine("Application Pool {0}'s state has gone from {1} to {2}", applicationPool.Name, state, applicationPool.State); | |
| break; | |
| case ObjectState.Stopping: | |
| case ObjectState.Unknown: | |
| for (int i = 0; i < RetryCount; i++) | |
| { | |
| Console.WriteLine("Application Pool {0}'s state is {1}. Waiting for state to become Stopped", applicationPool.Name, state); | |
| state = applicationPool.State; | |
| if (state == ObjectState.Stopped) { break; } | |
| } | |
| if (state == ObjectState.Stopped) | |
| { | |
| applicationPool.Start(); | |
| Console.WriteLine("Application Pool {0}'s state has gone from {1} to {2}", applicationPool.Name, state, applicationPool.State); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Error starting Application Pool {0}: Application Pool never stopped", applicationPool.Name); | |
| } | |
| break; | |
| case ObjectState.Started: | |
| case ObjectState.Starting: | |
| Console.WriteLine("Application Pool {0} was already in a {1} state and has not been modified", applicationPool.Name, state); | |
| break; | |
| default: | |
| Console.WriteLine("Error starting Application Pool {0}: Unexpected ObjectState \"{1}\"", applicationPool.Name, state); | |
| break; | |
| } | |
| this.serverManager.CommitChanges(); | |
| } | |
| public void Recycle() | |
| { | |
| foreach (ApplicationPool item in AppPools) | |
| { | |
| try | |
| { | |
| RecycleAppPool(item); | |
| } | |
| catch (SystemCriticalException ex) | |
| { | |
| CriticalExceptionLogger.Log(ex, "", item); | |
| } | |
| } | |
| } | |
| public void Recycle(ApplicationPool appPool) | |
| { | |
| try | |
| { | |
| RecycleAppPool(appPool); | |
| } | |
| catch (SystemCriticalException ex) | |
| { | |
| CriticalExceptionLogger.Log(ex, "", appPool); | |
| } | |
| } | |
| public void Recycle(string appPoolName) | |
| { | |
| try | |
| { | |
| ApplicationPool appPool = FindAppPool(appPoolName); | |
| RecycleAppPool(appPool); | |
| } | |
| catch (SystemCriticalException ex) | |
| { | |
| CriticalExceptionLogger.Log(ex, "", appPoolName); | |
| } | |
| } | |
| private void RecycleAppPool(ApplicationPool applicationPool) | |
| { | |
| ObjectState state = applicationPool.State; | |
| switch (state) | |
| { | |
| case ObjectState.Started: | |
| applicationPool.Recycle(); | |
| Console.WriteLine("Application Pool {0} was recycled at {1}", applicationPool.Name, DateTime.Now); | |
| break; | |
| case ObjectState.Starting: | |
| case ObjectState.Unknown: | |
| for (int i = 0; i < RetryCount; i++) | |
| { | |
| Console.WriteLine("Application Pool {0}'s state is {1}. Waiting for state to become Started", applicationPool.Name, state); | |
| state = applicationPool.State; | |
| if (applicationPool.State == ObjectState.Started) { break; } | |
| } | |
| if (state == ObjectState.Started) | |
| { | |
| applicationPool.Recycle(); | |
| Console.WriteLine("Application Pool {0} was recycled at {1}", applicationPool.Name, DateTime.Now); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Error starting Application Pool {0}: Application Pool never Started", applicationPool.Name); | |
| } | |
| break; | |
| case ObjectState.Stopped: | |
| case ObjectState.Stopping: | |
| Console.WriteLine("Application Pool {0} was in a {1} state and will not be recycled", applicationPool.Name, state); | |
| return; | |
| default: | |
| Console.WriteLine("Error stopping Application Pool {0}: Unexpected ObjectState \"{1}\"", applicationPool.Name, state); | |
| return; | |
| } | |
| this.serverManager.CommitChanges(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment