Answered by:
How To Restart Application Pool Using Asp.net button

Question
-
User-807418713 posted
Hello
Is that any code which restart application pool using asp.net button click?
Thanking You
Tuesday, July 21, 2020 9:07 AM
Answers
-
User288213138 posted
Hi Gopi.MCA,
Is that any code which restart application pool using asp.net button click?You can also try use ApplicationPool.Recycle Method to restart application pool.
Note: You need to publish your application to iis, and then test.
ServerManager serverManager = new ServerManager(); ApplicationPool appPool = serverManager.ApplicationPools["applicationpool name"]; if (appPool != null) { if (appPool.State == ObjectState.Stopped) { appPool.Start(); } else { appPool.Recycle(); } }
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 22, 2020 3:22 AM
All replies
-
User753101303 posted
Hi,
Either using the proper management classes such as https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.administration.applicationpool?view=iis-dotnet (may require extra permissions ?) or indirectly by doing something triggering the restart.
If I ever do that it would be rather in a separate application rather then embedded into the app itself.
Tuesday, July 21, 2020 9:43 AM -
User-18289217 posted
You can use WMI methods ApplicationPool.Stop and ApplicationPool.Start (calling them one after another) to restart the app pool
Also you could use the ServerManager Class (Microsoft.Web.Administration) which is very easy to use
Tuesday, July 21, 2020 9:44 AM -
User-807418713 posted
Hello
Thanks for your reply
Can you please give me the code so that i can write in button click
Thanking You
Tuesday, July 21, 2020 9:50 AM -
User-18289217 posted
Gopi.MCA
Hello
Thanks for your reply
Can you please give me the code so that i can write in button click
Thanking You
protected void btnRestartAppPool_Click(object sender, EventArgs e) { var manager = new ServerManager(); var appPool = manager.ApplicationPools["Your_AppPool_Name"]; appPool.Stop(); appPool.Start(); }
P.S. don't forget to install Microsoft.Web.Administration from the NuGet (right-click the project and select Manage NuGet Packages from the context menu) and then add the using directive for Microsoft.Web.Administration. Voila!
HTH
Tuesday, July 21, 2020 9:56 AM -
User-807418713 posted
hello
how to check is that code is working or not
sp_configure can show result or have to see other ways
for older version how would be this var
var appPool = manager.ApplicationPools["Your_AppPool_Name"];
Tuesday, July 21, 2020 10:10 AM -
User-18289217 posted
Buddy you don't even try to do something by yourself. Please be aware that this is not a place where we write code for you. Rather, we provide help after you have given your best. Have you?
OK I cannot guarantee that the following will work as I am not sure if the wanted attribute name is correct (You may want to google the attributes list). Anyway here we go
ServerManager manager = new ServerManager(); var appPool = manager.ApplicationPools["Your_AppPool_Name"]; if(appPool != null) { if(appPool.State == ObjectState.Started) { // latest recycle time Response.Write(appPool.GetAttribute("StartTime").Value + "<br />"); appPool.Stop(); appPool.Start(); // minutes since the last recycle (should be zero if the code works) Response.Write(appPool.Recycling.PeriodicRestart.Time.TotalMinutes); } }
Tuesday, July 21, 2020 11:03 AM -
User288213138 posted
Hi Gopi.MCA,
Is that any code which restart application pool using asp.net button click?You can also try use ApplicationPool.Recycle Method to restart application pool.
Note: You need to publish your application to iis, and then test.
ServerManager serverManager = new ServerManager(); ApplicationPool appPool = serverManager.ApplicationPools["applicationpool name"]; if (appPool != null) { if (appPool.State == ObjectState.Stopped) { appPool.Start(); } else { appPool.Recycle(); } }
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 22, 2020 3:22 AM