【问题标题】:Unrecognized element on ConfigurationSectionConfigurationSection 上无法识别的元素
【发布时间】:2015-09-29 14:55:59
【问题描述】:

我希望能够为以下配置建模:

<bundles>
    <resource type="script">
         <bundle name="common/services">
             <file path="common/consoleService.js" />
             <file path="common/localStorageService.js" />
             <file path="common/restService.js" />
             <!-- ... More files ... -->
         </bundle>
    </resource>
</bundles>

所以我开始创建以下ConfigurationSection

internal class BundlesSection : ConfigurationSection
{
     internal const string TAG_NAME = "bundles";

     [ConfigurationProperty(ResourceCollection.TAG_NAME,
                            IsRequired = false,
                            IsDefaultCollection = true)]
     internal ResourceCollection Resources
     {
         get { return this[ResourceCollection.TAG_NAME] as ResourceCollection; }
     }
}

[ConfigurationCollection(typeof(ResourceElement),
    AddItemName = ResourceElement.TAG_NAME)]
internal class ResourceCollection : ConfigurationElementCollection
{
    internal const string TAG_NAME = "";

    protected override ConfigurationElement CreateNewElement()
    {
        return new ResourceElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ResourceElement)element).Type;
    }
}

[ConfigurationCollection(typeof(BundleElement),
    AddItemName = BundleElement.TAG_NAME)]
internal class ResourceElement : ConfigurationElementCollection
{
     internal const string TAG_NAME = "resource";
     private const string ATTR_TYPE = "type";

     [ConfigurationProperty(ATTR_TYPE,
                           IsRequired = true,
                           IsKey = true)]
     internal string Type
     {
         get { return this[ATTR_TYPE] as string; }
         set { this[ATTR_TYPE] = value; }
     }

    protected override ConfigurationElement CreateNewElement()
    {
        return new BundleElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((BundleElement)element).Name;
    }
}

[ConfigurationCollection(typeof(FileElement),
    AddItemName = FileElement.TAG_NAME)]
internal class BundleElement : ConfigurationElementCollection
{
     internal const string TAG_NAME = "bundle";
     private const string ATTR_NAME = "name";

     [ConfigurationProperty(ATTR_NAME,
                           IsRequired = true,
                           IsKey = true)]
     internal string Name
     {
         get { return this[ATTR_NAME] as string; }
         set { this[ATTR_NAME] = value; }
     }

    protected override ConfigurationElement CreateNewElement()
    {
        return new FileElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((FileElement)element).Path;
    }
}

internal class FileElement : ConfigurationElement
{
    internal const string TAG_NAME = "file";
    private const string ATTR_PATH = "path";

    [ConfigurationProperty(ATTR_PATH,
                           IsRequired = true,
                           IsKey = true)]
    internal string Path
    {
         get { return this[ATTR_PATH] as string; }
         set { this[ATTR_PATH] = value; }
    }
}

虽然看起来一切正常,但在第一次加载该部分时出现以下异常:

无法识别的元素“捆绑”

如您所见,BundleElement.TAG_NAME"bundle",所以我不知道为什么它没有被识别。

我正在加载配置部分如下:

private BundlesSection LoadSection()
{
    return ConfigurationManager.GetSection(String.Format("static/{0}", BundlesSection.TAG_NAME)) as BundlesSection;
}

我的Web.config 中也有以下内容:

<configuration>

    <configSections>
        <sectionGroup name="static">
            <section name="bundles" type="XXX" restartOnExternalChanges="true" />
        </sectionGroup>
    </configSections>

    <static>
        <bundles configSource=".\Configuration\Static\Bundles.xml" />
    </static>

</configuration>

【问题讨论】:

  • 您在配置文件中缺少捆绑部分配置,它告诉应用如何解释您的自定义部分。
  • @dbugger 我认为没有必要发布它,因为事实上,我没有收到“无法识别的配置部分”错误,但我会尽快发布它,谢谢!
  • 我看到您正在为捆绑部分使用单独的配置文件。该文件是否存在于指定位置?
  • @dbugger 它存在并且正在被找到,否则它会抛出一个异常告诉我它没有找到它

标签: c# configuration configurationmanager


【解决方案1】:

两个问题:集合类型和元素名称。需要将 ResourceElement 和 BundleElement 指定为 ConfigurationElementCollectionType.BasicMap

然后适当地覆盖它们各自的 ElementName 属性。

[ConfigurationCollection(typeof(BundleElement))]
    internal class ResourceElement : ConfigurationElementCollection
    {
        internal const string TAG_NAME = "resource";
        private const string ATTR_TYPE = "type";

        [ConfigurationProperty(ATTR_TYPE, IsRequired = true, IsKey = true)]
        internal string Type
        {
            get { return base[ATTR_TYPE] as string; }
            set { base[ATTR_TYPE] = value; }
        }

        protected override string ElementName { get { return BundleElement.TAG_NAME; } }

        public override ConfigurationElementCollectionType CollectionType
        {
            get {return  ConfigurationElementCollectionType.BasicMap;}
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new BundleElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((BundleElement)element).Name;
        }
    }

 [ConfigurationCollection(typeof(FileElement))]
    internal class BundleElement : ConfigurationElementCollection
    {
        internal const string TAG_NAME = "bundle";
        private const string ATTR_NAME = "name";

    [ConfigurationProperty(ATTR_NAME, IsRequired = true, IsKey = true)]
    internal string Name
    {
        get { return this[ATTR_NAME] as string; }
        set { this[ATTR_NAME] = value; }
    }

    protected override string ElementName { get { return FileElement.TAG_NAME; } }

    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.BasicMap; }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new FileElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((FileElement)element).Path;
    }  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多