Reputation: 9776
How to handle web.config content's update event? I mean not for administrator purposes (wmi etc) but for programming - I need recalculate some data inside the web service in case configuration parameter has changed.
There are no any suitable events in the WebConfigurationManager, ConfigurationManager, Configuration, ConfigurationSection, SectionInformation... So where to dig for them?
P.S. .net 3.5
Upvotes: 1
Views: 1339
Reputation: 9776
Using System.IO.FileSystemWatcher brings two architecture problems (and one headache - security). First is impossibility to lock "whole configuration" (static and recalculable) during changes and second is the question where to hoste the monitoring thread.
It would be nice to have framework event, unfortunalty there are no such event. But I found that there is comfortable solution - have custom config section with overridden PostDeserialize method. There is some risk since I don't understand whole process, there is a lot of mystic: custom section instance is recreated on each config edit TWICE, constructor called twice, Init method called twice, when PostDeserialize - called only once - so all is ok for us - but who knows what we can meet in production...
public class CustomSettings : ConfigurationSection
{
protected override void PostDeserialize()
{
base.PostDeserialize();
findCertificate(this["Thumbprint"]); // searching for certificate in windows certificate repository and setup Certificate property
}
public CustomSettings()
{
InternalProperties = new ConfigurationPropertyCollection(/*... */)
}
//...
public X509Certificate Certificate { get; private set; }
}
Upvotes: 1
Reputation: 2819
You can use the System.IO.FileSystemWatcher to monitor changes on any file, including Web.config. It will raise an event when the file changes.
Upvotes: 2