简体   繁体   English

从MVC控制器重新启动IIS网站

[英]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 我正在寻找一种使用C#.NET从MVC控制器重新启动当前正在运行的网站(从管理页面)的方法。

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. 我在网站上有一个bug,但仅在网站运行了一段随机时间后才会发生,这真的很难找到,同时如果需要,我仍然需要一种远程方法来从手机中快速重新启动网站。

ASP.NET application could restart because really a lot of reasons. 由于许多原因,ASP.NET应用程序可能会重新启动。 It probably happens more often than you think. 它可能比您想像的更多地发生。 ASP.NET web application will restart because of: ASP.NET Web应用程序将由于以下原因而重新启动:

  • Change in Web.Config file. Web.Config文件中的更改。 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. 更改machine.config,就像更改web.config一样。
  • Change in /bin folder. 在/ bin文件夹中更改。 Application will restart if you add, update or delete any file in /bin folder. 如果您添加,更新或删除/ bin文件夹中的任何文件,应用程序将重新启动。
  • Change in App_Code folder. 在App_Code文件夹中更改。 Adding, deleting or editing classes inside App_Code will cause application restart. 在App_Code中添加,删除或编辑类将导致应用程序重新启动。
  • Change in Global.asax file, like in web.config. 像在web.config中一样,在Global.asax文件中进行更改。
  • Change in App_LocalResources folder App_LocalResources文件夹中的更改
  • Change in App_GlobalResources folder App_GlobalResources文件夹中的更改
  • Change in App_WebReferences folder App_WebReferences文件夹中的更改
  • Change in Profile configuration 配置文件配置更改
  • Reached compilation limit, defined with numRecompilesBeforeAppRestart in machine.config file. 已达到的编译限制,在machine.config文件中使用numRecompilesBeforeAppRestart定义。 Default value is 15. ASP.NET watches for any changes in files. 默认值为15。ASP.NET监视文件中的任何更改。 When certain number (15 by default) of files is changed, web application is restarted automatically. 更改一定数量(默认为15个)的文件时,Web应用程序将自动重新启动。 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. 防病毒程序将扫描Web.config或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. IIS重新启动,所有网站也将重新启动。
  • Change of physical path to web application 更改Web应用程序的物理路径
  • IIS recycled website due to inactivity (20 minutes by default). IIS由于不活动而回收了网站(默认为20分钟)。 If website has no visitors, IIS will unload it from memory to save resources. 如果网站没有访问者,IIS将从内存中将其卸载以节省资源。 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. 从代码中调用UnloadAppDomain ,尽管这样做需要完全信任权限。

    System.Web.HttpRuntime.UnloadAppDomain()

  2. Deliberately change the gloabl.asax or web.config file modified date attribute 故意更改gloabl.asax或web.config文件的修改日期属性

    File.SetLastWriteTimeUtc(MapPath("~/global.asax"), DateTime.UtcNow); File.SetLastWriteTimeUtc(MapPath("~/web.config"), DateTime.UtcNow);

Create a .bat file name IISresart.bat 创建一个.bat文件名IISresart.bat

and In it i write 然后我写

iisreset

iisreset stops and then start IIS. iisreset停止,然后启动IIS。

And ASP.net i write 我写的和ASP.net

            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. 并重新启动IIS。

Note.I tested it on asp.net web form .I should work on MVC also. 注意:我在asp.net Web表单上对其进行了测试。我也应该在MVC上工作。 And for no windows add this line processInfo.CreateNoWindow = true; 对于没有窗口的情况,请添加以下行: 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. 每当我想从应用程序中注销所有用户时,我曾经使用的一种非常简单的解决方案是通过从bin文件夹中创建/删除(如果存在)虚拟的.dll文件。

you can name this dll file whatever you want but by creating / deleting it will restart the whole application (pool) and fix your problem.. 您可以根据需要命名该dll文件,但是通过创建/删除该dll文件,它将重新启动整个应用程序(池)并解决您的问题。

The beauty about this solution is that you don't need a special permissions for your web application. 此解决方案的优点在于,您不需要Web应用程序的特殊权限。

Another option is killing the w3wp process. 另一个选择是终止w3wp进程。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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