简体   繁体   中英

Restart IIS website from MVC controller

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.

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

Create a .bat file name IISresart.bat

and In it i write

iisreset

iisreset stops and then start IIS.

And ASP.net i write

            string str_Path = Server.MapPath(".") + "\\IISresart.bat";
            ProcessStartInfo processInfo = new ProcessStartInfo(str_Path);
            processInfo.UseShellExecute = false;
            Process batchProcess = new Process();
            batchProcess.StartInfo = processInfo;
            batchProcess.Start();

And it restarts IIS.

Note.I tested it on asp.net web form .I should work on MVC also. And for no windows add this line processInfo.CreateNoWindow = true;

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.

Another option is killing the w3wp process. I didn't need any extra permissions for my controller to run the following:

var powershellCommand = @"get-process w3wp | stop-process";
var procInfo = new ProcessStartInfo("powershell")
{
    Arguments = $"/c {powershellCommand}",
    RedirectStandardError = true,
    RedirectStandardOutput = true,
    UseShellExecute = false
};

var p = Process.Start(procInfo);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM