locked
Reading a custom section from app.config RRS feed

  • Question

  • 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 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 posts that have not giving me a solution to this.


    Code is like a box of chocolates!...


    • Edited by VcDeveloper Saturday, November 24, 2012 4:26 PM
    Saturday, November 24, 2012 4:15 PM

All replies

  • Thanks to Randy Levy - Enterprise Library support engineer, I have the solution and didn't realize it was this simple, but its only simple after you have the knowledge right.

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

    foreach (ModuleConfigurationElement module in section.Modules) { RegionTrackingState region = new RegionTrackingState(); region.RegionName = module.ModuleName; region.AssemblyFile = module.AssemblyFile; region.ModuleType = module.ModuleType; region.StartupLoaded = module.StartupLoaded; region.ElementInformation = module.ElementInformation; region.InitializationStatus = InitializationStatus.NotStarted; region.InitializationConstructed = false; region.InitializationInitialized = false; region.InitializationCompleted = false; region.ExpectedDiscoveryMethod = DiscoveryMethod.ConfigurationManifest; region.ModuleDependencies = module.Dependencies; region.ExpectedInitializationMode = module.StartupLoaded ? InitializationMode.WhenAvailable : InitializationMode.OnDemand; region.ExpectedDownloadTiming = DownloadTiming.WithApplication; region.ConfiguredDependencies = CommonServiceNames.ServiceExcel; _regionCollection.Add(module.ModuleName, region); }



    Code is like a box of chocolates!...


    • Edited by VcDeveloper Thursday, November 29, 2012 10:02 PM
    Thursday, November 29, 2012 4:47 PM