Reputation: 26281
I want to be able to model the following configuration:
<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>
So I proceeded to create the following 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; }
}
}
Although everything seems OK, I am getting the following exception when the section is first loaded:
Unrecognized element 'bundle'
As you can see, BundleElement.TAG_NAME
is "bundle"
, so I don't know why it isn't being recognized.
I'm loading the configuration section as follows:
private BundlesSection LoadSection()
{
return ConfigurationManager.GetSection(String.Format("static/{0}", BundlesSection.TAG_NAME)) as BundlesSection;
}
I also have the following in my 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>
Upvotes: 0
Views: 809
Reputation: 16399
Two issues: Collection Types and Element Names. Need to specify the ResourceElement and BundleElement as ConfigurationElementCollectionType.BasicMap
And then override their respective ElementName properties appropriately.
[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;
}
}
Upvotes: 1