3

I have an ASP.NET application that is part of a much larger system; it reads most of its configuration from files that are above the root level of the web application.

As the application is already written and the format of the configuration files was chosen to ease editing by customers I cannot make use of the solution in “Shared configuration files in .NET”.

I am hoping ASP.NET has an API I can call at startup time to tell it which file to watch. (I could write this the hard way using a “FileSystemWatcher” and catching events then doing the restart myself, however that would be a pity as ASP.NET already has the code to watch files and restart when they are changed.)

Also what ASP.NET API do I call to get ASP.NET to restart in a nice way?

These are some links I have found that may help: Using the FileSystemWatcher from ASP.NET and Extending FileSystemWatcher to ASP.NET


In one case I cached the configuration in the ASP.NET Cache and set a CacheDependency to the file. In the other case that needed faster access, I used a FileSystemWatcher and updated a static field when the configuration changed.

(I never did find out how to restart ASP.NET from code in a reasonable way.)

Community
  • 1
  • 1
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317

5 Answers5

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();
  }
Max Toro
  • 28,282
  • 11
  • 76
  • 114
4

If you know your app will never access configuration outside of an HttpContext, consider caching your configuration and adding a CacheDependency to the file. If the file changes, your cached configuration is removed and you can re-add it with updated values.

Something like:

public MyConfigObj GetConfig()
{
    var config = Cache["configkey"] as MyConfigObj;
    if(config == null)
    {
        var configPath = Server.MapPath("myconfigpath");
        config = GetConfigFromFile(configPath);
        Cache.Insert("configkey", config, new CacheDependency(configPath));
    }
    return config;
}
Corbin March
  • 25,526
  • 6
  • 73
  • 100
  • Thanks, this answer will be useful to a lot of people; however we need to do a restart to make use of the changed configuration. If I was starting again this would be an interesting option to consider – Ian Ringrose May 20 '09 at 15:35
  • I am exception this answer, as it to closest to solving my root problem. I cached one of my configuration files; in the other case I used a FileSystemWatcher and updated a static field when the file changed. – Ian Ringrose Aug 27 '09 at 08:57
1

For the second part, you can use the following piece of code to trigger an AppPool recycle (which is the same as restarting IIS, only on application level instead of the whole server).

using System.DirectoryServices;

public void RecycleAppPool(string machine, string appPoolName) 
{
    string path = "IIS://" + machine + "/W3SVC/AppPools/" + appPoolName;
    DirectoryEntry w3svc = new DirectoryEntry(path);
    w3svc.Invoke("Recycle", null);
}

(from here)

Colin
  • 10,630
  • 28
  • 36
-1

I think you are going to need to create a service that uses FileSystemWatcher to monitor for changes to the configuration files outside the web application's directory -- and then when a change is detected, make a non-significant change to the web application's web.config file and save it, which will restart the app.

Timothy Lee Russell
  • 3,719
  • 1
  • 35
  • 43
-1

Stupid answer maybe, but you can always 'touch' (add some whitespace or something) your web.config file and that will trigger a restart. It is the poor man's iisreset =)

(I want to again stress that this is no an ideal solution to general restarting of websites or resetting IIS)

JasonRShaver
  • 4,344
  • 3
  • 32
  • 39
  • The problem is that we get lots of bug reports when people forget to do this! – Ian Ringrose May 20 '09 at 15:13
  • Then I would look to an automated deployment solution such as TeamCity or CruiseControl.net that will automate deployments as well as be configurable enough to 'touch' the web.config file. – Chad Ruppert May 20 '09 at 15:28
  • Our customers run our application on their own servers etc within their own network. (It is an *application* we sell, it just happens to use HTML for part of it’s UI) – Ian Ringrose May 20 '09 at 15:34