自定义配置节属性不是配置元素
我需要另外一套眼睛来解决这个问题。 我得到了一个
ConfigurationErrorsException未处理“属性’exportClientSettings’不是ConfigurationElement。”
当我调用ExportClientConfiguration.GetSection();
时抛出exceptionExportClientConfiguration.GetSection();
这是我的代码:
App.config中
C#代码
/// /// The export client configuration section. /// public class ExportClientConfiguration : ConfigurationSection, IExportClientConfiguration { private const string ExportClientSettingsCollectionName = "exportClientSettings"; /// /// Gets the export client configuration items. /// /// /// The export client configuration items. /// [ConfigurationProperty(ExportClientSettingsCollectionName, IsDefaultCollection = true)] public IEnumerable ExportClientConfigurationItems { get { return base[ExportClientSettingsCollectionName] as ExportClientSettingsCollection; } } /// /// Gets the export client path settings. /// /// /// The export client path settings. /// public IList ExportClientSettings { get { return ExportClientConfigurationItems.ToList(); } } /// /// Gets the section. /// /// Name of the configuration section. /// The configuration section. public static IExportClientConfiguration GetSection(string configurationSectionName = "exportClientConfiguration") { IExportClientConfiguration section = ConfigurationManager.GetSection(configurationSectionName) as ExportClientConfiguration; return section; } } /// /// An IEnumerable collection fo ExportClientSettings /// [ConfigurationCollection(typeof(ExportClientSettings), AddItemName = "exportClientSetting")] public class ExportClientSettingsCollection : ConfigurationElementCollection, IEnumerable { /// /// Gets the at the specified index. /// /// /// The . /// /// The index. /// public ExportClientSettings this[int index] { get { return BaseGet(index) as ExportClientSettings; } } /// /// Returns an enumerator that iterates through the collection. /// /// /// A that can be used to iterate through the collection. /// public new IEnumerator GetEnumerator() { return (from item in Enumerable.Range(0, Count) select this[item]).GetEnumerator(); } protected override ConfigurationElement CreateNewElement() { return new ExportClientSettings(); } protected override object GetElementKey(ConfigurationElement element) { var lConfigElement = element as ExportClientSettings; return lConfigElement != null ? lConfigElement.Name : null; } } /// /// The export client path settings /// public class ExportClientSettings : ConfigurationElement, IExportClientSettings { private const string NameProperty = "name"; private const string PathProperty = "path"; /// /// Gets the name. /// /// /// The name to identify the export path and filename. /// [ConfigurationProperty(NameProperty, IsKey = true)] public string Name { get { return this[NameProperty].ToString(); } } /// /// Gets the path. /// /// /// The path. /// [ConfigurationProperty(PathProperty)] public string Path { get { return this[PathProperty].ToString(); } } }
我错过了什么?
几个问题。
-
ExportClientConfigurationItems
属性应为ExportClientSettingsCollection
类型,ExportClientSettingsCollection
不是IEnumerable
。 - 在你的配置文件中,
Path
是大写字母,它应该是小写字母path=\\Example\Path"
,因为你用小写的ConfigurationProperty
装饰了属性。
修复这两个,它的工作原理。