首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用.Net配置框架加载带有必需子ConfigurationElement的ConfigurationSection

使用.Net配置框架加载带有必需子ConfigurationElement的ConfigurationSection
EN

Stack Overflow用户
提问于 2010-03-18 23:24:21
回答 3查看 4.6K关注 0票数 11

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

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

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

下面是我的配置节的样子。

代码语言:javascript
复制
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(); }
    }
}

下面是我加载配置节的方法。

代码语言:javascript
复制
   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());
            }
        }

下面是我的配置文件的样子。

代码语言:javascript
复制
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="MyConfigSection" type="configurationFrameworkTestHarness.MyConfigSection, configurationFrameworkTestHarness" />
  </configSections>

  <MyConfigSection>

  </MyConfigSection>

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

代码语言:javascript
复制
var configManager = OpenConfigFile(path);
var configSection = configManager.GetSection("MyConfigSection") as MyConfigSection;
configManager = OpenConfigFile(path);
configSection = configManager.GetSection("MyConfigSection") as MyConfigSection;

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-03-22 20:31:06

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

这是我的代码。

代码语言:javascript
复制
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);
    }
}
票数 4
EN

Stack Overflow用户

发布于 2010-03-19 14:36:21

MS论坛中的Eric has answered this

引用他的回答:

ConfigurationPropertyAttribute的IsRequired成员在应用于子对象(从ConfigurationElement派生)时不起作用

票数 3
EN

Stack Overflow用户

发布于 2010-03-22 20:12:43

您是否尝试将其直接赋值给正确类型的变量,即MyConfigSection而不是var?这是我能看到的两行代码之间的唯一区别。(即在第二行中,var现在采用了一个特定的类型)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2470996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档