views:

213

answers:

5

I have an Asp.net application that is part of a match larger system, it reads most of it’s 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 can not make use of the solution in “Shared configuration files in .NET”.

I am hoping Asp.net has an API I can call at start uptime to tell it witch 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 Aps.net API do I call to get Aps.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.)

A: 

See this other question

David
Thanks, it does not give the complete solution but looking at how log4net uses the FileSystemWatcher class will give me a partial example.
Ian Ringrose
A: 

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
I hope there is a better way
Ian Ringrose
+2  A: 

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
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
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
A: 

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
The problem is that we get lots of bug reports when people forget to do this!
Ian Ringrose
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
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
+1  A: 

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