This project has moved and is read-only. For the latest updates, please go here.

Reload Configuration Instance

Mar 9, 2011 at 9:23 PM

Some of my code needs to change my configuration data, and I want to reload it from disk, I've tried refreshing the configuration section:

            TestRunConfiguration trc = TestRunConfiguration.Instance;

            //Refresh the config
            trc = TestRunConfiguration.Instance;
The Instance is a singleton and never reloads, is there a way to make this work?


Mar 11, 2011 at 2:23 PM

The only way I've been able to make it work for Windows applications is with separate code:

''' <summary>
    ''' Class watching for changes of configuration file
    ''' Implemented as restartOnExternalChanges attribute on configurationSection doesn't work as "Supposed"?   
    ''' </summary>
    ''' <remarks></remarks>
    Public Class XXXConfigurationWatcher : Implements IDisposable

        Private watcher As FileSystemWatcher
        Private lastEvent As DateTime

        ''' <summary>
        ''' Event triggered when FileSystemWatcher triggers a Changed event
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Event ConfigChanged(ByVal sender As Object, ByVal e As FileSystemEventArgs)

        Public Sub New()
            watcher = New FileSystemWatcher(Path.GetDirectoryName(XXXConfiguration.Instance.ElementInformation.Source))
            watcher.Filter = Path.GetFileName(XXXConfiguration.Instance.ElementInformation.Source)
            watcher.EnableRaisingEvents = True
            watcher.NotifyFilter = NotifyFilters.LastWrite

            AddHandler watcher.Changed, AddressOf OnConfigChanged
        End Sub

        Private Sub OnConfigChanged(ByVal sender As Object, ByVal e As FileSystemEventArgs)
#If DEBUG Then
            Trace.WriteLine("Config changed, refreshed")
#End If
            ' This avoids double events from the Filesystemwatcher.
            If DateTime.Now > lastEvent.AddSeconds(1) Then
                lastEvent = DateTime.Now

                'Refresh configuration section
                RaiseEvent ConfigChanged(sender, e)
            End If
        End Sub


#Region " IDisposable Support "
        Private disposedValue As Boolean = False        ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    If watcher IsNot Nothing Then
                        RemoveHandler watcher.Changed, AddressOf OnConfigChanged
                    End If
                End If
            End If
            Me.disposedValue = True
        End Sub
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        End Sub
#End Region

    End Class