4

I have written a custom configuration for my application but I am not getting proper way to determine if any ConfigurationElement not present in ConfigurationSection.

this is my custom configuration app.config code

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="ApplicationsSettings" type="App.Configuration.ConfigurationGroup, App.Configuration.Core">
      <section name="DataExtractorSettings" type="App.Configuration.DataExtractorConfig, App.Configuration.Core" />
    </sectionGroup>
  </configSections>

  <ApplicationsSettings>
    <DataExtractorSettings>
      <executionLog>
        <enabled value="Y" />
        <copyOnReportDirectory value="Y" />
        <logFilePath value="D:\MyBatchProcessLog\MasterDataExtractor" />
      </executionLog>
      <eMail>
        <fromEmailID value="gupta@gmail.com" />
        <webURL value="http://PROD/login.aspx" />
      </eMail>

  <!--<parallelProcessing>
        <allowed value="Y" />
        <threds value="6" />
      </parallelProcessing>-->

    </DataExtractorSettings>

  </ApplicationsSettings>

</configuration>

Class DataExtractorConfig

namespace App.Configuration
{
    [SettingProperty(Name: "dataExtractorSettings")]
    public class DataExtractorConfig : ConfigurationSection
    {
        [ConfigurationProperty("executionLog")]
        public LogConfig ExecutionLog
        {
            get
            {
                if (base["executionLog"] != null)
                {
                    return (LogConfig)base["executionLog"];
                }
                else
                {
                    return null;
                }

            }
        }

        [ConfigurationProperty("parallelProcessing")]
        public ParallelProcessConfig ParallelProcessing
        {
            get
            {
                if (base["parallelProcessing"] != null)
                {
                    return (ParallelProcessConfig)base["parallelProcessing"];
                }
                else
                {
                    return null;
                }
            }
        }

        [ConfigurationProperty("eMail")]
        public EmailConfig Email
        {
            get
            {
                if (base["eMail"] != null)
                {
                    return (EmailConfig)base["eMail"];
                }
                else
                {
                    return null;
                }
            }
        }
    }
}

This is the code to read the settings from App.config file

public static class ConfigurationReader
{

private const string _customSeetingParentNode = "ApplicationsSettings";

public static T GetSettings<T>(string configFileName = "AppSettings.xml", string sectionGroupName = "ApplicationsSettings")
{
    System.Configuration.Configuration config;
    ConfigurationSection customConfig;
    ExeConfigurationFileMap fileMap;
    T result;
    string settingKey;

    try
    {
        fileMap = new ExeConfigurationFileMap();
        fileMap.ExeConfigFilename = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, configFileName);

        config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        var caSetting = typeof(T).GetCustomAttributes(typeof(SettingProperty), false);

        string sectionName;
        dynamic dynSettingClass = null;
        if (caSetting != null && caSetting.Length > 0)
        {
            settingKey = (caSetting[0] as SettingProperty).Name;
            sectionName = string.Concat(_customSeetingParentNode, "/", settingKey);
            customConfig = (ConfigurationSection)config.GetSection(sectionName);

            if (customConfig != null && customConfig.ElementInformation.IsPresent)
            {
                //Here how do I determine the <parallelProcessing> setting is present or not in configuration file, 
                //like currently it is commented in app config file so the parallelProcessingExists should set with false

                //bool parallelProcessingExists = ?

                dynSettingClass = customConfig;
            }
            else
            {
                dynSettingClass = Activator.CreateInstance(typeof(T));
            }

        }
        else
        {
            dynSettingClass = Activator.CreateInstance(typeof(T));
        }

        result = (T)Convert.ChangeType(dynSettingClass, typeof(T));
        return result;
    }
    finally
    {
        config = null;
        customConfig = null;
        fileMap = null;
    }

}
}

Here is a code to read the setting from app.config file

DataExtractorConfig settigns = ConfigurationReader.GetSettings<DataExtractorConfig>();

How do I determine the "parallelProcessing" setting is present or not in configuration file, like currently it is commented in app config file so the parallelProcessingExists should set with false.

Neeraj Kumar Gupta
  • 2,157
  • 7
  • 30
  • 58
  • Usually, you don't need to determine it a configuration element is present or not. You just use the default value you declared. – Simon Mourier Feb 20 '17 at 06:49
  • @SimonMourier the issue is that I have one more section in config file that is common config it also has "executionLog", "eMail" & "parallelProcessing" nodes, if any of the other setting does not have node that should be loaded from common section. like in example "parallelProcessing" is not present in "DataExtractorSettings" section so it should load from common section. – Neeraj Kumar Gupta Feb 20 '17 at 07:13

1 Answers1

6

You created DataExtractorConfig class so let's use it. The object that is returned from GetSection method can be casted to DataExtractorConfig. Now you can access easily ParallelProcessing, Email, ExecutionLog properties and check if corresponding configuration sections exist. For example:

var customConfig = (DataExtractorConfig)config.GetSection(sectionName);

if (customConfig != null && customConfig.ElementInformation.IsPresent)
{
   if(customConfig.ParallelProcessing.ElementInformation.IsPresent)
   {
      // TODO
   }
   else
   {
      // TODO
   }

   dynSettingClass = customConfig;
}
Michał Komorowski
  • 6,198
  • 1
  • 20
  • 24
  • This doesn't seem to work if you are getting the section from a mapped exe configuration. Is there a way to do this from a mappedexeconfiguration file? – Hooplator15 Jun 13 '19 at 17:27