如何确定“ConfigurationElement”在“ConfigurationSection”自定义配置设置中可用
Posted
技术标签:
【中文标题】如何确定“ConfigurationElement”在“ConfigurationSection”自定义配置设置中可用【英文标题】:How to determine "ConfigurationElement" is available on "ConfigurationSection" custom configuration settings 【发布时间】:2017-07-06 14:11:58 【问题描述】:我已经为我的应用程序编写了一个自定义配置,但是我没有找到正确的方法来确定 ConfigurationSection 中是否不存在任何 ConfigurationElement。
这是我的自定义配置 app.config 代码
<?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>
类 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;
这是从 App.config 文件中读取设置的代码
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;
这是从 app.config 文件中读取设置的代码
DataExtractorConfig settigns = ConfigurationReader.GetSettings<DataExtractorConfig>();
我如何确定“parallelProcessing”设置是否存在于配置文件中,就像目前它在应用程序配置文件中注释一样,因此 parallelProcessingExists 应该设置为 false。
【问题讨论】:
通常,您不需要确定是否存在配置元素。您只需使用您声明的默认值。 @SimonMourier 问题是我在配置文件中还有一个部分是通用配置,如果其他任何设置没有,它也有“executionLog”、“eMail”和“parallelProcessing”节点有应该从公共部分加载的节点。例如,“DataExtractorSettings”部分中不存在“parallelProcessing”,因此它应该从公共部分加载。 【参考方案1】:您创建了DataExtractorConfig
类,所以让我们使用它。从GetSection
方法返回的对象可以转换为DataExtractorConfig
。现在您可以轻松访问ParallelProcessing
、Email
、ExecutionLog
属性并检查是否存在相应的配置部分。例如:
var customConfig = (DataExtractorConfig)config.GetSection(sectionName);
if (customConfig != null && customConfig.ElementInformation.IsPresent)
if(customConfig.ParallelProcessing.ElementInformation.IsPresent)
// TODO
else
// TODO
dynSettingClass = customConfig;
【讨论】:
如果您从映射的 exe 配置中获取该部分,这似乎不起作用。有没有办法从映射的exe配置文件中做到这一点?以上是关于如何确定“ConfigurationElement”在“ConfigurationSection”自定义配置设置中可用的主要内容,如果未能解决你的问题,请参考以下文章