I'm looking for a way to restart the current running website (from the admin page) from an MVC controller, with C#.NET
Why i need it? i have a bug in a website but it only happens after the website runs a random amount of time which makes it really hard to find, meanwhile i still need a remote way to restart the site from my phone quickly if needed.
Select Restart IIS Site/Application Pool from the options, and then click Configure Action. Expand Restart IIS Site/Application Pool Settings. Select the IIS Action to Perform from the drop down list. Choose the Site or Application Pool.
The core IIS services (Http. sys, W3SVC, WAS) never need to be restarted. Instead, they automatically detect any relevant configuration changes (e.g. new website being created, or a change in your application pool settings) and apply them on the fly.
ASP.NET application could restart because really a lot of reasons. It probably happens more often than you think. ASP.NET web application will restart because of:
- Change in Web.Config file. Change of any parameter, adding of space character or even changing file's modified date will cause app restart.
- Change in machine.config, just like for web.config.
- Change in /bin folder. Application will restart if you add, update or delete any file in /bin folder.
- Change in App_Code folder. Adding, deleting or editing classes inside App_Code will cause application restart.
- Change in Global.asax file, like in web.config.
- Change in App_LocalResources folder
- Change in App_GlobalResources folder
- Change in App_WebReferences folder
- Change in Profile configuration
- Reached compilation limit, defined with numRecompilesBeforeAppRestart in machine.config file. Default value is 15. ASP.NET watches for any changes in files. When certain number (15 by default) of files is changed, web application is restarted automatically. The reason for this is to save memory on server. On some websites where files are generated dynamically, this could cause frequent restarts.
- Antivirus software changes file's modified date. Antivirus program scans files like Web.config or Global.asax. Some antivirus programs change Modified date of these files and that cause pretty frequent restarts, which decrease website performances.
- IIS restarts, and all websites will restart too.
- Change of physical path to web application
- IIS recycled website due to inactivity (20 minutes by default). If website has no visitors, IIS will unload it from memory to save resources. Application will be started again on when next visitor comes.
Here's what I would normally do to restart the application.
Call UnloadAppDomain
from the code, although full trust permission is required to do so.
System.Web.HttpRuntime.UnloadAppDomain()
Deliberately change the gloabl.asax or web.config file modified date attribute
File.SetLastWriteTimeUtc(MapPath("~/global.asax"), DateTime.UtcNow);
File.SetLastWriteTimeUtc(MapPath("~/web.config"), DateTime.UtcNow);
A very easy solution which i used to use whenever i want to logout all users from an application is by creating / deleting (if exists) a dummy .dll file from inside the bin folder.
you can name this dll file whatever you want but by creating / deleting it will restart the whole application (pool) and fix your problem..
The beauty about this solution is that you don't need a special permissions for your web application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With