Reading a custom section from app.config

Nov 24, 2012 at 4:22 PM
Edited Nov 24, 2012 at 4:25 PM

Hi,

Can't seem to find a simple answer or there must not be one for reading section attributes from the application config file.  All I want to do is read a Section that has assembly element properties.

App.Config:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="modules"
             type="Microsoft.Practices.Prism.Modularity.ModulesConfigurationSection, Microsoft.Practices.Prism"/>
  </configSections>
  <modules>
    <module
      assemblyFile="ServiceCommon.dll"
      moduleType="ServiceCommon.CommonServiceModule, ServiceCommon, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      moduleName="ServiceCommon"
      startupLoaded="true">
    </module>
    <module
      assemblyFile="ExcelService.dll"
      moduleType="ExcelService.ExcelServiceModule, ExcelService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      moduleName="ExcelService"
      startupLoaded="false">
    </module>
    <module
      assemblyFile="RegionTurahList.dll"
      moduleType="RegionTurahList.TurahListRegionModule, RegionTurahList, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      moduleName="RegionTurahList"
      startupLoaded="false">
      <dependencies>
        <dependency moduleName="ExcelService"/>
      </dependencies>
    </module>
    <module
      assemblyFile="RegionTurahBooks.dll"
      moduleType="RegionTurahBooks.TurahBooksRegionModule, RegionTurahBooks, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      moduleName="RegionTurahBooks"
      startupLoaded="false">
      <dependencies>
        <dependency moduleName="ExcelService"/>
      </dependencies>
    </module>
    <module
      assemblyFile="RegionTurahHebrew.dll"
      moduleType="RegionTurahHebrew.TurahHebrewRegionModule, RegionTurahHebrew, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      moduleName="RegionTurahHebrew"
      startupLoaded="false">
      <dependencies>
        <dependency moduleName="ExcelService"/>
      </dependencies>
    </module>
    <module
      assemblyFile="RegionTurahAleppo.dll"
      moduleType="RegionTurahAleppo.TurahAleppoRegionModule, RegionTurahAleppo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      moduleName="RegionTurahAleppo"
      startupLoaded="false">
      <dependencies>
        <dependency moduleName="ExcelService"/>
      </dependencies>
    </module>
    <module
      assemblyFile="RegionTurahNames.dll"
      moduleType="RegionTurahNames.TurahNamesRegionModule, RegionTurahNames, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      moduleName="RegionTurahNames"
      startupLoaded="false">
      <dependencies>
        <dependency moduleName="ExcelService"/>
      </dependencies>
    </module>
  </modules>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

I want to read and get the properties of all the elements <module> in the Section <modules> and put them in a Dictionary when I load a module (assembly):

    public class CommonService : ICommonService, IRegionTracker
    {
        private readonly IEventAggregator eventAggregator;
        private readonly IUnityContainer container;
        private IModuleManager moduleManager;
        private Dictionary<string, RegionTrackingState> _regionCollection = new Dictionary<string, RegionTrackingState>();

        public CommonService(IEventAggregator eventAggregator, IUnityContainer container, IModuleManager moduleManager)
        {
            this.eventAggregator = eventAggregator;
            this.moduleManager = moduleManager;
            this.container = container;
        }

...
..
.
        public void loadModule(string parameter)
        {
            if (!container.IsRegistered<ExcelService>())
                this.moduleManager.LoadModule(RegionNames.ExcelService);

            if (!_regionCollection.ContainsKey(parameter))
            {
                Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) as Configuration;
                ConfigurationSection section = appConfig.GetSection("modules");
                SectionInformation sInfo = section.SectionInformation;

                RegionTrackingState region = new RegionTrackingState(
                    parameter,
                    DiscoveryMethod.ConfigurationManifest,
                    InitializationMode.OnDemand,
                    DownloadTiming.WithApplication,
                    RegionNames.ExcelService);

                _regionCollection.Add(parameter, region);
                this.moduleManager.LoadModule(parameter);
            }
        }
....
...
}

I can only get as far as getting the Section and don't know where to go from there.  I've read a lot of posts that have not giving me a solution to this.

Nov 26, 2012 at 4:06 AM

Not sure how much this is related to Unity but this should get you started:

var section = ConfigurationManager.GetSection("modules") as ModulesConfigurationSection;

// Can make this whatever <K,T> dictionary you want
Dictionary<string, string> moduleDictionary = new Dictionary<string, string>();
 
foreach (ModuleConfigurationElement module in section.Modules)
{
    // can create Type here if you want
    moduleDictionary.Add(module.ModuleName, module.ModuleType);
}

Hope that helps,

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Nov 27, 2012 at 7:51 PM

Thank you very much for your help!  It  solved my solution.  I apology for straying away from the purpose of this discussion forum and I as of today no one on WPF or the Social forums have posted a solution. :(

Again, thanks for your help, because I really appreciate it! :)