Ian Ringrose
Ian Ringrose

Reputation: 51927

How can I get ASP.NET to restart when a file is changed?

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

Upvotes: 3

Views: 2342

Answers (5)

Max Toro
Max Toro

Reputation: 28618

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

Upvotes: 6

Colin
Colin

Reputation: 10638

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)

Upvotes: 1

JasonRShaver
JasonRShaver

Reputation: 4394

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)

Upvotes: -1

Corbin March
Corbin March

Reputation: 25734

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

Upvotes: 4

Timothy Lee Russell
Timothy Lee Russell

Reputation: 3748

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.

Upvotes: -1

Related Questions