更改文件时如何让 ASP.NET 重新启动?

Ian*_*ose 3 .net asp.net configuration

我有一个 ASP.NET 应用程序,它是一个更大系统的一部分;它从 Web 应用程序根级别以上的文件中读取大部分配置。

由于已经编写了应用程序并且选择了配置文件的格式以方便客户进行编辑,因此我无法使用“.NET 中的共享配置文件”中的解决方案

我希望 ASP.NET 有一个 API,我可以在启动时调用它来告诉它要观看哪个文件。(我可以使用“FileSystemWatcher”以艰难的方式编写此代码并捕获事件然后自己重​​新启动,但是这很遗憾,因为 ASP.NET 已经有代码来监视文件并在更改时重新启动。)

另外我调用什么 ASP.NET API 来让 ASP.NET 以一种很好的方式重新启动?

这些是我发现的一些可能有帮助的链接: Using the FileSystemWatcher from ASP.NET and Extending FileSystemWatcher to ASP.NET


在一种情况下,我将配置缓存在 ASP.NET 缓存中,并为该文件设置了 CacheDependency。在需要更快访问的另一种情况下,我使用了 FileSystemWatcher 并在配置更改时更新了静态字段。

(我从来没有发现如何以合理的方式从代码中重新启动 ASP.NET。)

Max*_*oro 6

  static FileSystemWatcher ConfigWatcher;

  static void StartWatchConfig() {

     ConfigWatcher = new FileSystemWatcher("configPath") {
        Filter = "configFile"
     };

     ConfigWatcher.Changed += new FileSystemEventHandler(ConfigWatcher_Changed);
     ConfigWatcher.EnableRaisingEvents = true;
  }

  static void ConfigWatcher_Changed(object sender, FileSystemEventArgs e) {
     HttpRuntime.UnloadAppDomain();
  }
Run Code Online (Sandbox Code Playgroud)