I have a web.config file, that I want to parse in case of its' changing. For this purpose I use FileSystemWatcher and IDictionary collection. Here is code of my method:
public void ReadCurrentConfiguration()
{
var settings = System.Configuration.ConfigurationManager.AppSettings;
lock (_dictMonitor)
{
if (_dictionary.Count != 0)
_dictionary.Clear();
foreach (var elem in Enum.GetValues(typeof(ConfigurationParameter)).Cast<ConfigurationParameter>())
_dictionary.Add(elem.ToString(), settings[elem.ToString()]);
}
}
_dictMonitor is Object, that is used to lock; ConfigurationParameter is enum, that is used to define key names. This method is called when FileSystemWatcher changing event is raising. The problem is that program execution halts either on _dictionary.Clear(); or on lock(dictMonitor); with no exeptions. When debugging, manual calling of my method ReadCurrentConfiguration executes fine - it cleans the dictionary normally and fills with new values. But in case of event raising it stops. Also, if execution is passed to clear() method, debugger shows the following message instead of _dictionary value: _dictionary Count = Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack. Could anyone suggest why this problem occurs?
Some time later, I understood the cause of the problem - changes to web.config
makes IIS restart the application, so the implementation above is ridiculous. This is most likely the reason of "hanging" the thread.