从 MVC 控制器重启 IIS 网站

Restart IIS website from MVC controller

我正在寻找重新启动当前 运行 网站的方法(从管理页面) 来自 MVC 控制器,使用 C#.NET

为什么我需要它? 我在一个网站上有一个错误,但它只在网站运行一段随机时间后才会发生,这使得它很难找到, 同时,如果需要,我仍然需要一种远程方式来从我的 phone 快速重启网站。

创建.bat 文件名IISresart.bat

我在里面写了

iisreset

iisreset 停止然后启动 IIS。

还有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();

并重新启动 IIS。

Note.I 在 asp.net 网络表单上对其进行了测试。我也应该在 MVC 上工作。 对于没有 windows 添加此行 processInfo.CreateNoWindow = true;

每当我想从应用程序中注销所有用户时,我都会使用一个非常简单的解决方案,即从 bin 文件夹中创建/删除(如果存在)虚拟 .dll 文件。

您可以随意命名此 dll 文件,但通过创建/删除它将重新启动整个应用程序(池)并解决您的问题..

此解决方案的优点在于您不需要为您的 Web 应用程序授予特殊权限。

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.

以下是我通常会执行的重启应用程序的操作。

  1. 从代码调用 UnloadAppDomain,尽管这样做需要完全信任权限。

    System.Web.HttpRuntime.UnloadAppDomain()

  2. 故意更改gloabl.asax或web.config文件修改日期属性

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

另一种选择是终止 w3wp 进程。我的控制器不需要任何额外权限即可 运行 以下内容:

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