views:

362

answers:

1

When I use typesafe application settings, I have something like this in my app.exe.config file:

<setting name="IntervalTimeout" serializeAs="String">
    <value>10</value>
</setting>

The Settings.Designer.vb does something like this:

<Global.System.Configuration.ApplicationScopedSettingAttribute(),  _
 Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
 Global.System.Configuration.DefaultSettingValueAttribute("10")>  _
Public ReadOnly Property IntervalTimeout() As Integer
    Get
        Return CType(Me("IntervalTimeout"),Integer)
    End Get
End Property

And I can access the setting with something like this:

Settings.IntervalTimeout

What is the best practice for monitoring changes to this setting from the outside. I.e. the setting is used by an application that is running as a service and when somebody opens the corresponsing config file and changes the IntervalTimeout to let's say 30, the service application gets notified and does whatever I tell it to do to reconfigure itself.

Usually, I would have used something like this during initialization:

AddHandler Settings.PropertyChanged, New PropertyChangedEventHandler(AddressOf Settings_PropertyChanged)

And then implement an Eventhandler that checks the name of the property that has changed and reacts appropriately.

However, the PropertyChanged event only gets fired when the Set method is called on a property, not when somebody changes the data directly within the file. Of course this would have been heaven, but understandable that this is not the way it works. ;-)

Most probably, I'd implement a filesystemwatcher to monitor changes to the file and then reload the configuration, find out what changed and then do whatever is necessary to reflect those changes.

Does anyone know a better way? Or even a supported one?

A: 

ASP.NET monitors the Web.config file for changes. It does this using the System.Web.FileChangesMonitor class (which is marked internal and sealed), which uses another internal class System.Web.FileMonitor. Have a look at them in reflector. It might be too complex for your problem, but I'd certainly look into it if I had a similar requirement.

This looks pretty complex. Bear in mind ASP.NET shuts down the old application and starts a new one once it detects a change. App.config wasn't architectured to detect for changes while the application is running. Perhaps reconsider why you need this?

An easier approach is to restart the service once you've made the change.

RichardOD