繁体   English   中英

ConfigurationSection上无法识别的元素

[英]Unrecognized element on ConfigurationSection

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

<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>

两个问题:集合类型和元素名称。 需要将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;
    }  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM