【问题标题】:Possible to mimic web.config restart behaviour for my own config files?可以为我自己的配置文件模仿 web.config 重新启动行为吗?
【发布时间】:2015-12-04 12:49:47
【问题描述】:

当您修改 ASP.NET (MVC) 应用程序的“web.config”文件时,the application is automatically recompiled/restarted 会强制读入修改后的“web.config”。

我的问题:

是否可以在 ASP.NET 网站的根目录中为我自己的配置文件(比如说“my-config.json”)应用这种更改检测行为?

即当有人修改“my-config.json”文件时,应用程序会重新启动。

【问题讨论】:

  • 您可以使用FileSystemWatcher 来查看您的文件并检测更改,然后重新启动应用程序。
  • @RezaAghaei 好主意。好像只有undocumented ways来重启一个ASP.NET应用?
  • SO中有一些关于重启应用的答案,例如:Restarting (Recycling) an Application Pool,也可以使用重启
  • 或许你不需要重启应用,只需要重新加载设置即可。

标签: c# asp.net .net asp.net-mvc web-config


【解决方案1】:

您可以使用FileSystemWatcher 来查看您的文件并检测更改,然后重新启动应用程序或重新加载您的设置。

也许你不需要重新启动应用程序,你只需要重新加载你的设置

protected void Application_Start()
{
    // Other initializations ...
    // ....

    var watcher = new FileSystemWatcher();
    //Set the folder to watch
    watcher.Path = Server.MapPath("~/Config");
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite;
    //Set a filter to watch
    watcher.Filter = "*.json";
    watcher.Changed += watcher_Changed;

    // Begin watching.
    watcher.EnableRaisingEvents = true;
}

void watcher_Changed(object sender, FileSystemEventArgs e)
{
    //Restart application here
    //Or Reload your settings
}

【讨论】:

  • 太棒了。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 2023-02-16
  • 1970-01-01
  • 2014-03-24
  • 2018-07-22
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多