J'essaie d'écrire une section de configuration personnalisée très simple pour une application .NET4. Mon objectif est le suivant:Section de configuration personnalisée simple avec collection dans .NET4
<configuration>
<configSections>
<section name="myServices" type="My.ConfigSection, My.Assembly" />
</configSections>
<myServices>
<add name="First" />
<add name="Second" />
</myServices>
</configuration>
Cependant, je continue à obtenir un ConfigurationErrorsException
: « élément Unrecognized « ajouter » » quand je l'appelle ConfigurationManager.GetSection("myServices")
. Je l'ai regardé pendant un moment maintenant mais n'ai pas encore compris ce que je fais mal. Voici mon code. Il s'agit de trois classes: ConfigSection
, MyServiceSettingsCollection
et MyServiceSettings
.
Première classe représentant la section de configuration entière. Il a une collection par défaut sans nom de type MyServiceSettingsCollection
. La propriété IsDefaultCollection
devrait me permettre de 'ajouter' directement à ma collection à partir de l'élément racine.
public sealed class ConfigSection : ConfigurationSection
{
private static readonly ConfigurationProperty _propMyServices;
private static readonly ConfigurationPropertyCollection _properties;
public static ConfigSection Instance { get { return _instance; } }
static ConfigSection()
{
_propMyServices = new ConfigurationProperty(
null, typeof(MyServiceSettingsCollection), null,
ConfigurationPropertyOptions.IsDefaultCollection);
_properties = new ConfigurationPropertyCollection { _propMyServices };
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public MyServiceSettingsCollection MyServices
{
get { return (MyServiceSettingsCollection) base[_propMyServices]; }
set { base[_propMyServices] = value; }
}
protected override ConfigurationPropertyCollection Properties
{ get { return _properties; } }
}
Ensuite, la classe de collecte elle-même. Il est de type AddRemoveClearMap
.
[ConfigurationCollection(typeof(MyServiceSettings),
CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
public sealed class MyServiceSettingsCollection : ConfigurationElementCollection
{
public MyServiceSettings this[int index]
{
get { return (MyServiceSettings) BaseGet(index); }
set
{
if (BaseGet(index) != null) { BaseRemoveAt(index); }
BaseAdd(index, value);
}
}
public new MyServiceSettings this[string key]
{
get { return (MyServiceSettings) BaseGet(key); }
}
protected override ConfigurationElement CreateNewElement()
{
return new MyServiceSettings();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((MyServiceSettings) element).Key;
}
}
Et enfin une classe pour les éléments de la collection. Pour l'instant, cette classe a une propriété mais il y en aura plus tard (ce qui m'empêche d'utiliser NameValueSectionHandler
).
public class MyServiceSettings : ConfigurationElement
{
private static readonly ConfigurationProperty _propName;
private static readonly ConfigurationPropertyCollection properties;
static MyServiceSettings()
{
_propName = new ConfigurationProperty("name", typeof(string), null, null,
new StringValidator(1),
ConfigurationPropertyOptions.IsRequired |
ConfigurationPropertyOptions.IsKey);
properties = new ConfigurationPropertyCollection { _propName };
}
[ConfigurationProperty("name", DefaultValue = "",
Options = ConfigurationPropertyOptions.IsRequired |
ConfigurationPropertyOptions.IsKey)]
public string Name
{
get { return (string) base[_propKey]; }
set { base[_propKey] = value; }
}
protected override ConfigurationPropertyCollection Properties
{ get { return properties; } }
}
mais bon de savoir comment cela fonctionne - aura quelque chose de similaire à réaliser dans les prochaines semaines, alors merci :-) – Yahia
C'est étrange. 'IsDefaultCollection' et' Options' fonctionnent tous les deux pour moi. Mais avoir une chaîne vide pour le nom de la propriété semble être obligatoire. –
En regardant la source de référence pour ConfigurationPropertyAttribute.cs, la propriété IsDefaultCollection est juste un wrapper sur la propriété Options flags - http://referencesource.microsoft.com/#System.Configuration/System/Configuration/ConfigurationPropertyAttribute.cs,62 –