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;
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With