gpt4 book ai didi

c# - 使用 .Net 配置框架加载具有所需子 ConfigurationElement 的 ConfigurationSection

转载 作者:可可西里 更新时间:2023-11-01 08:47:01 24 4
gpt4 key购买 nike

我有一个控制台应用程序试图从 web.config 文件加载 CustomConfigurationSection。

自定义配置部分有一个必需的自定义配置元素。这意味着当我加载配置部分时,如果该配置元素不存在于配置中,我希望看到异常。问题是 .NET 框架似乎完全忽略了 isRequired 属性。因此,当我加载配置部分时,我只是创建了一个自定义配置元素的实例并将其设置在配置部分。

我的问题是,为什么会这样?我希望 GetSection() 方法触发 ConfigurationErrors 异常,因为配置中缺少必需的元素。

这是我的配置部分的样子。

public class MyConfigSection : ConfigurationSection
{
[ConfigurationProperty("MyConfigElement", IsRequired = true)]
public MyConfigElement MyElement
{
get { return (MyConfigElement) this["MyConfigElement"]; }
}
}
public class MyConfigElement : ConfigurationElement
{
[ConfigurationProperty("MyAttribute", IsRequired = true)]
public string MyAttribute
{
get { return this["MyAttribute"].ToString(); }
}
}

这是我加载配置部分的方式。

   class Program
{
public static Configuration OpenConfigFile(string configPath)
{
var configFile = new FileInfo(configPath);
var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
var wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/", vdm);
return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
}

static void Main(string[] args)
{
try{
string path = @"C:\Users\vrybak\Desktop\Web.config";

var configManager = OpenConfigFile(path);
var configSection = configManager.GetSection("MyConfigSection") as MyConfigSection;

MyConfigElement elem = configSection.MyElement;
} catch (ConfigurationErrorsException ex){
Console.WriteLine(ex.ToString());
}
}

这是我的配置文件的样子。

<?xml version="1.0"?>
<configuration>
<configSections>
<section name="MyConfigSection" type="configurationFrameworkTestHarness.MyConfigSection, configurationFrameworkTestHarness" />
</configSections>

<MyConfigSection>

</MyConfigSection>

奇怪的是,如果我打开配置文件并连续加载该部分 2 次,我将得到我预期的异常。

var configManager = OpenConfigFile(path);
var configSection = configManager.GetSection("MyConfigSection") as MyConfigSection;
configManager = OpenConfigFile(path);
configSection = configManager.GetSection("MyConfigSection") as MyConfigSection;

如果我使用上面的代码,则会触发异常并告诉我需要 MyConfigElement。问题是为什么不是第一次抛出这个异常??

最佳答案

我发现最好的解决方法是手动遍历 ConfigurationElement 类型的所有嵌套属性,并在获取该部分后自行检查它们。如果一个元素是必需的但文件中不存在,我只是抛出一个 ConfigurationErrorsException。

这是我的代码。

private void ProcessMissingElements(ConfigurationElement element)
{
foreach (PropertyInformation propertyInformation in element.ElementInformation.Properties)
{
var complexProperty = propertyInformation.Value as ConfigurationElement;
if (complexProperty == null)
continue;

if (propertyInformation.IsRequired && !complexProperty.ElementInformation.IsPresent)
throw new ConfigurationErrorsException("ConfigProperty: [{0}] is required but not present".FormatStr(propertyInformation.Name));
if (!complexProperty.ElementInformation.IsPresent)
propertyInformation.Value = null;
else
ProcessMissingElements(complexProperty);
}
}

关于c# - 使用 .Net 配置框架加载具有所需子 ConfigurationElement 的 ConfigurationSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2470996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com