Reputation: 7750
I have to completely restart application when any part of it's configuration changes. IIS does it's best to watch for configuration in asp.net apps, but how do I to restart such applications as console or services?
Is it ok to monitor configuration file changes with FileSystemWatcher and start new instance of application, closing current one?
something like:
var file = new FileInfo(AppDomain.CurrentDomain.
SetupInformation.ConfigurationFile);
var watcher = new FileSystemWatch(file.Directory.FullName)
{
Filter = "...",
NotifyFilter = NotifyFilter.LastWrite }
);
watcher.Changed += .. // Process.Start(..), current process shutdown
watcher.EnableRaisingEvents = true;
Upvotes: 6
Views: 668
Reputation: 51927
When debugger remember that Visual Studio starts up XXX.vshost.exe rather then XXX.exe, so you need to watch a different file when debugging.
The FileSystemWatch is the easy bit, you need to use a 2nd app domain to run your main application or have a "launcher" app, or get your app to call Process.Start() on its self when it need to restart. The error checking is what makes this hard...
Upvotes: 1
Reputation: 6627
I am not sure about Asp.net but maybe you could load your application in a different AppDomain and when configuration changes Reload the domain. This will basically reinitialize you app with the new settings.
Here is how you could go about it:
AppDomainSetup setup = new AppDomainSetup()
{
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
// ... maybe more options can go here, inspect the class and see your options
};
domain = AppDomain.CreateDomain("MyDomainName", null, setup);
domain.UnhandledException += new UnhandledExceptionEventHandler(domain_UnhandledException);
MyApplication app = (MyApplication)domain.CreateInstanceFromAndUnwrap("assemblyfilename.dll", "The_Type_Name.MyApplication" );
app.Launch();
Upvotes: 1
Reputation: 49280
You could run your actual application logic in a separate AppDomain. The initial (or default) AppDomain would only be used to (a) start a thread with the file system watcher and (b) start the application logic in the separate AppDomain. The key is, that you can pass a path to a configuration file when creating an AppDomain.
When you identify a change in the configuration file, you stop your application logic (whatever that implies in your case - and this is the really tricky part!) and unload the AppDomain, then restarting it, which will use the updated configuration file.
Upvotes: 2
Reputation: 17603
The standard application configuration of .NET does not allow dynamic updates as far as I know. But if I were you, I would rather write my own application config class which reads an xml or whatever config, watch for changes of this configuration file and if something changed, my own class would reload the configuration file dynamically. If this is possible, it should be much more elegant than killing and starting new processes.
If your application has no problems with being called multiple times (there are two instances when you start the new process) and being stopped, and if configuration changes are rather infrequent, there should be no drawback in your approach. I don't know if you can start windows services at good will.
Upvotes: 1