I went a little different way with my code to recycle the application pool. A few comments that differ from what others have provided:
1) I used the using statement to ensure proper disposal of the ServerManager object.
2) I wait until the application pool completes before stopping it so that we don’t encounter any problems when trying to stop the application. Likewise, I wait for the application pool to complete the shutdown before trying to start it.
3) I force the method to accept the actual server name instead of returning to the local server because I decided that you probably know which server you use it on.
4) I decided to start / stop the application, and not process it, so that I could make sure that we did not accidentally start the application pool, which was stopped for another reason, and to avoid problems with the attempt to recycle the already stopped application pool.
public static void RecycleApplicationPool(string serverName, string appPoolName) { if (!string.IsNullOrEmpty(serverName) && !string.IsNullOrEmpty(appPoolName)) { try { using (ServerManager manager = ServerManager.OpenRemote(serverName)) { ApplicationPool appPool = manager.ApplicationPools.FirstOrDefault(ap => ap.Name == appPoolName);
Spazmoose Feb 17 '15 at 1:49 2015-02-17 01:49
source share