How to prevent the configuration file watcher thread in Microsoft Enterprise Library 3.1
I have not blogged very much the last time, but now I'm back to write about some interesting things again. Lets start with the Microsoft Enterprise Library. For some reasons we are still bound to version 3.1, so the following is valid for that version only - it might not work with the newer version 4.x yet available.
So what I'm writing about? If you ever run a trace tool for an application using the Entlib you might have considered a separate thread "ConfigurationChangeFileWather" (right, it is misspelled - but that thread name is set by the Entlib, sorry). How comes? If you don't need this service (config changes are monitored while the application is running) there is not much information about it how to disable that (especially if you run in ASP.NET with the Entlib). Google around, the most answers I found are: you cannot disable that without modifying the Entlib source code (you can, but who will ever really do this and maintain it over the versions?). So I will describe a scenario, how we use it and how we disable that thread (consuming resources, because it polls the file change date).
For all, that don't like to read the full story: you just have to call
The filePath variable should point to the Entlib configuration file you use, the important parameter is the boolean. That's it!
But, you know that filePath is also a configuration entry, and hard-coding that is not really what you want. Our app.config files are looking like this, your file might be similar:
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=184.108.40.206, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
<add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=220.127.116.11, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" filePath="Module.EnterpriseLibrary.config" />
The interesting parts are marked red. As you can see: you can have multiple sources, but only the selected source is important here. It has the filePath attribute, that points to the real Enterprise Library configuration file (that is monitored by the thread). So to make the call above dynamic, you have to get the value from the filePath attribute:
public static string ModuleConfigurationFile
ConfigurationSourceSection section = ConfigurationSourceSection.GetConfigurationSourceSection();
if (section != null && !String.IsNullOrEmpty(section.SelectedSource) &&
section.Sources.Count > 0)
FileConfigurationSourceElement e = section.Sources.Get(section.SelectedSource) as FileConfigurationSourceElement;
if (e != null)
First we get the section (enterpriseLibrary.ConfigurationSource) to read the selectedSource attribute content. If it is set, we read the corresponding source element and return the file path.
So now call the function mentioned above with the dynamic ModuleConfigurationFile property somewhere in your application startup phase -- no configuration file watcher anymore!