c# - Unrecognized attribute custom configuration (again) -
i'm creating custom configuration section keep getting attribute not recognized
error when trying section.
i'm pretty sure it's dumb typo - here can spot it.
code:
<configsections> <section name="clientfiltersettings" type="espdb.config.clientfiltersettings, espdb"/> </configsections> <clientfiltersettings> <allowedips> <ipaddress ip="255.255.255.255"/> </allowedips> </clientfiltersettings>
section:
namespace espdb.config { public class clientfiltersettings : configurationsection { private static clientfiltersettings _settings = configurationmanager.getsection(typeof(clientfiltersettings).name) clientfiltersettings /*?? new clientfiltersettings()*/; private const string _allowedips = "allowedips"; public static clientfiltersettings settings { { return _settings; } } [configurationproperty(_allowedips, isrequired = true)] [configurationcollection(typeof(ipaddresscollection))] public ipaddresscollection allowedips { { return (ipaddresscollection)this[_allowedips]; } set { this[_allowedips] = value; } } } }
collection:
namespace espdb.config { public class ipaddresscollection : configurationelementcollection { protected override configurationelement createnewelement() { return new ipaddresscollection(); } protected override object getelementkey(configurationelement element) { return (element ipaddresselement).ip; } protected override string elementname { { return "ipaddress"; } } public ipaddresselement this[int index] { { return base.baseget(index) ipaddresselement; } set { if (base.baseget(index) != null) { base.baseremoveat(index); } this.baseadd(index, value); } } public new ipaddresselement this[string responsestring] { { return (ipaddresselement)baseget(responsestring); } set { if (baseget(responsestring) != null) { baseremoveat(baseindexof(baseget(responsestring))); } baseadd(value); } } } }
element
namespace espdb.config { public class ipaddresselement : configurationelement { private const string _ip = "ip"; [configurationproperty(_ip, iskey = true, isrequired = true)] public string ip { { return this[_ip] string; } set { this[_ip] = value; } } } }
there multiple problems in code.
1). recursively creating objects of clientfiltersettings. remove following code, not required.
private static clientfiltersettings _settings = configurationmanager.getsection(typeof(clientfiltersettings).name) clientfiltersettings /*?? new clientfiltersettings()*/; public static clientfiltersettings settings { { return _settings; } }
2). change attribute from
[configurationcollection(typeof(ipaddresscollection))]
to
[configurationcollection(typeof(ipaddresselement), additemname = "ipaddress", collectiontype = configurationelementcollectiontype.basicmap)]
3). creating collection object within collection. modify code below
return new ipaddresscollection();
with
return new ipaddresselement();
Comments
Post a Comment