Problem with ConfigurationStore and multiple config files.

Oct 27, 2008 at 2:32 PM
Why does the ConfigurationStore class look for a modules section i all .config files in the application directory?

If you have multiple applications (and config files) in the same directory, this will not work as you don't know what config file the applications will use.




Regards,
Per K. Jensen
Oct 28, 2008 at 4:33 PM
Edited Oct 28, 2008 at 4:34 PM

Hi

 

As you said, the ConfigurationStore class iterates through all the files with .config extension in the application base directory and retrieves the first module configuration section found. This allows you to have the enumeration of your modules in a separated file of the App.config without having to specify the complete name of the file.

 

If you want to have multiple Composite WPF Applications in the same directory that use the ConfigurationModuleEnumerator class to enumerate the modules, you could use a different base directory for each of your applications where the ConfigurationStore class will search for *.config files:

protected override IModuleEnumerator GetModuleEnumerator()

{

    ConfigurationStore store = new ConfigurationStore("MyApplication1");

    return new ConfigurationModuleEnumerator(store);

}

If you use this approach you must ensure that your configuration file is copied to the base directory.

 

Another approach could be creating a class that inherits from the ConfigurationStore class and override the RetrieveModuleConfigurationSection method to retrieve the module configuration section from a determined configuration file. A possible implementation of the CustomConfigurationStore class could be as follows:

using System;

using System.IO;

using System.Configuration;

using Microsoft.Practices.Composite.Modularity;

 

public class CustomConfigurationStore : ConfigurationStore

{

    private readonly string _configFilePath;

 

    public CustomConfigurationStore(string configFile)

    {

        _configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile ?? "App.config");

    }

 

    public override ModulesConfigurationSection RetrieveModuleConfigurationSection()

    {

        Configuration configuration = GetConfiguration(_configFilePath);

        ModulesConfigurationSection section = (ModulesConfigurationSection)configuration.GetSection("modules");

 

        if (section != null)

        {

            return section;

        }

 

        return null;

    }

 

    private static Configuration GetConfiguration(string configFilePath)

    {

        ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };

        return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

    }

}            

Then, in the GetModuleEnumerator method of your application’s Bootstrapper, use the CustomConfigurationStore class like this:

protected override IModuleEnumerator GetModuleEnumerator()

{

    ConfigurationStore store = new CustomConfigurationStore("MyApplication1.config");

    return new ConfigurationModuleEnumerator(store);

}

 

Please let me know if this helps.

 

Mariano Converti

http://blogs.southworks.net/mconverti