Can't get my custom configuration section
我正在尝试创建一个自定义配置部分来加载应用程序监视器的"烤箱"列表。这是我在配置部分的第一次体验,我尝试过遵循这些示例;但是,我不知道我遗漏了什么。
当我尝试获取配置节时,会得到以下异常:
0我主要尝试:system.configuration.config=configurationmanager.openexeconfiguration(configurationuserlevel.none);if(配置节[BurnDimon]=空)…
1 2 3 | BurnInConfigurationSection burnInConfigSection = config.GetSection(BurnInSection) as BurnInConfigurationSection; BurnInConfigurationSection burnInConfigSection = ConfigurationManager.GetSection(BurnInSection) as BurnInConfigurationSection; |
一切似乎都会导致同样的例外
当我查看config.filepath时,它是"c:mksurninpcu swinurnin.ui.vshost.exe.config",我已经验证它与app.config文件匹配。
以下是我的配置类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | namespace BurnIn.UI { /// <summary> /// BurnIn Application configuration section in app.config /// </summary> public class BurnInConfigurationSection : ConfigurationSection { [ConfigurationProperty("Ovens", IsDefaultCollection = false)] [ConfigurationCollection(typeof(OvenCollection), AddItemName ="add", ClearItemsName ="clear", RemoveItemName ="remove")] public OvenCollection Ovens { get { return (OvenCollection)base["Ovens"]; } set { base["Ovens"] = value; } } } /// <summary> /// Oven configuration information /// </summary> public class OvenConfig : ConfigurationElement { public OvenConfig() { } public OvenConfig(string nickName, string mesName, string ip, int slotCount) { NickName = nickName; MesName = mesName; IP = ip; SlotCount = slotCount; } [ConfigurationProperty("NickName", DefaultValue ="OvenName", IsRequired = true, IsKey = true)] public string NickName { get { return (string)this["NickName"]; } set { this["NickName"] = value; } } [ConfigurationProperty("MesName", DefaultValue ="MesName", IsRequired = true, IsKey = true)] public string MesName { get { return (string)this["MesName"]; } set { this["MesName"] = value; } } [ConfigurationProperty("IP", DefaultValue ="10.130.110.20", IsRequired = true, IsKey = false)] public string IP { get { return (string)this["IP"]; } set { this["IP"] = value; } } [ConfigurationProperty("SlotCount", DefaultValue ="20", IsRequired = true, IsKey = false)] public int SlotCount { get { return (int)this["SlotCount"]; } set { this["SlotCount"] = value; } } } /// <summary> /// Collection of Oven Configs /// </summary> public class OvenCollection : ConfigurationElementCollection { public OvenCollection() { } public OvenConfig this[int index] { get { return (OvenConfig)BaseGet(index); } set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } public void Add(OvenConfig ovenConfig) { BaseAdd(ovenConfig); } public void Clear() { BaseClear(); } protected override ConfigurationElement CreateNewElement() { return new OvenConfig(); } protected override object GetElementKey(ConfigurationElement element) { return ((OvenConfig)element).NickName; } public void Remove(OvenConfig ovenConfig) { BaseRemove(ovenConfig.NickName); } public void RemoveAt(int index) { BaseRemoveAt(index); } public void Remove(string name) { BaseRemove(name); } } } |
这是我的app.config:
2您在此处输入了错误的配置节类型名称:
1 2 3 | <section name="Biotronik.NGMP.DAL.Sources.DalBaseSettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> |
您应该在此处使用自定义配置节类型的名称:
1 | type="BurnIn.UI.BurnInConfigurationSection, AssemblyWhereThisTypeIsDeclared" |