Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart IIS website from MVC controller

Tags:

c#

asp.net

iis-8

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.

like image 699
CMS Avatar asked May 08 '16 03:05

CMS


People also ask

How do I restart my IIS pool?

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.

Do I need to restart IIS after changing web config?

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.


2 Answers

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.

  1. Call UnloadAppDomain from the code, although full trust permission is required to do so.

    System.Web.HttpRuntime.UnloadAppDomain()

  2. 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);

like image 167
woodykiddy Avatar answered Sep 20 '22 13:09

woodykiddy


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.

like image 37
Sufyan Jabr Avatar answered Sep 20 '22 13:09

Sufyan Jabr