Monday, 15 March 2010

c# - App.conf custom configuration collection - Unrecognized attribute -



c# - App.conf custom configuration collection - Unrecognized attribute -

i have created own set of classes custom nested configuration collection in app.config. receiving error states "unrecognized attribute 'name'. note attribute names case-sensitive."

below classes, relevant portion of app.config , line of code grab custom configuration:

public class custom : configurationsection { [configurationproperty("autosyncconfiguration")] public backuplocationelementcollection autosyncconfiguration { { homecoming this["autosyncconfiguration"] backuplocationelementcollection; } } } public class backuplocationelementcollection : configurationelementcollection { protected override configurationelement createnewelement() { homecoming new backuplocationelement(); } protected override object getelementkey(configurationelement element) { homecoming ((backuplocationelement)element).name; } public override configurationelementcollectiontype collectiontype { { homecoming configurationelementcollectiontype.basicmap; } } protected override string elementname { { homecoming "backuplocation"; } } public backuplocationelement this[int index] { { homecoming (backuplocationelement)baseget(index); } set { if (baseget(index) != null) { baseremoveat(index); } baseadd(index, value); } } new public backuplocationelement this[string backupname] { { homecoming (backuplocationelement)baseget(backupname); } } public bool containskey(string key) { bool result = false; object[] keys = basegetallkeys(); foreach (object obj in keys) { if ((string)obj == key) { result = true; break; } } homecoming result; } } public class backuplocationelement : configurationelement { [configurationproperty("name", isrequired = true, iskey = true)] public string name { { homecoming this["name"] string; } set { this["name"] = value; } } [configurationproperty("details", isrequired = true, iskey = false)] public string details { { homecoming this["details"] string; } set { this["details"] = value; } } [configurationproperty("watchedfolder")] public watchedfolderelementcollection watchedfolder { { homecoming this["watchedfolder"] watchedfolderelementcollection; } } } public class watchedfolderelementcollection : configurationelementcollection { protected override configurationelement createnewelement() { homecoming new watchedfolderelement(); } protected override object getelementkey(configurationelement element) { homecoming ((watchedfolderelement)element).name; } public override configurationelementcollectiontype collectiontype { { homecoming configurationelementcollectiontype.basicmap; } } protected override string elementname { { homecoming "watchedfolder"; } } public watchedfolderelement this[int index] { { homecoming (watchedfolderelement)baseget(index); } set { if (baseget(index) != null) { baseremoveat(index); } baseadd(index, value); } } new public watchedfolderelement this[string foldername] { { homecoming (watchedfolderelement)baseget(foldername); } } public bool containskey(string key) { bool result = false; object[] keys = basegetallkeys(); foreach (object obj in keys) { if ((string)obj == key) { result = true; break; } } homecoming result; } } public class watchedfolderelement : configurationelement { [configurationproperty("name", isrequired = true, iskey = true)] public string name { { homecoming this["name"] string; } set { this["name"] = value; } } [configurationproperty("localfolder", isrequired = true, iskey = false)] public string localfolder { { homecoming this["localfolder"] string; } set { this["localfolder"] = value; } } [configurationproperty("remotefolder", isrequired = true, iskey = false)] public string remotefolder { { homecoming this["remotefolder"] string; } set { this["remotefolder"] = value; } } [configurationproperty("filespec", isrequired = true, iskey = false)] public string filespec { { homecoming this["filespec"] string; } set { this["filespec"] = value; } } }

the next app.config:

<configuration> <configsections> <section name="custom" type="autosync.custom, autosync" /> </configsections> <custom> <autosyncconfiguration> <backuplocation name="s3" details="accesskey=asdf;secretkey=asdf;bucketname=asdf"> <watchedfolder name="test1" localfolder="c" remotefolder="z" filespec="*"/> </backuplocation> <backuplocation name="external" details="mappeddrive=x;"> <watchedfolder name="test" localfolder="d" remotefolder="xphotos" filespec="*.jpeg" /> </backuplocation> </autosyncconfiguration> </custom> </configuration>

and code follows:

custom config = (custom)configurationmanager.getsection("custom");

can see error is?

it appears way classes written, have left out piece of hierarchy. changing following:

[configurationproperty("watchedfolder")] public watchedfolderelementcollection watchedfolder { { homecoming this["watchedfolder"] watchedfolderelementcollection; } }

to

[configurationproperty("watchedfolders")] public watchedfolderelementcollection watchedfolders { { homecoming this["watchedfolders"] watchedfolderelementcollection; } }

and modifying app.config have watchedfolders element surrounding watchedfolder elements fixed issue.

c# .net configuration

No comments:

Post a Comment