自定义配置节属性不是配置元素 [英] Custom Config Section Property is not a configuration element

查看:73
本文介绍了自定义配置节属性不是配置元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题上,我需要另一组眼神.我得到一个

I need another set of eyes on this problem. I am getting a

ConfigurationErrorsException未处理属性 "exportClientSettings"不是ConfigurationElement."

ConfigurationErrorsException was unhandled "Property 'exportClientSettings' is not a ConfigurationElement."

当我呼叫ExportClientConfiguration.GetSection();

这是我的代码:

App.config

App.config

  <configSections>
<section name="exportClientConfiguration" type="ClientConversionUtilityBroker.ConfigurationModels.ExportClientPathConfiguration.ExportClientConfiguration, ClientConversionUtilityBroker"/>
</configSections>
<exportClientConfiguration>
  <exportClientSettings>
    <exportClientSetting name="Example" Path="\\Example\Path" />
  </exportClientSettings>
</exportClientConfiguration>

C#代码

/// <summary>
/// The export client configuration section.
/// </summary>
public class ExportClientConfiguration : ConfigurationSection, IExportClientConfiguration
{
    private const string ExportClientSettingsCollectionName = "exportClientSettings";

    /// <summary>
    /// Gets the export client configuration items.
    /// </summary>
    /// <value>
    /// The export client configuration items.
    /// </value>
    [ConfigurationProperty(ExportClientSettingsCollectionName, IsDefaultCollection = true)]
    public IEnumerable<ExportClientSettings> ExportClientConfigurationItems
    {
        get { return base[ExportClientSettingsCollectionName] as ExportClientSettingsCollection; }
    }

    /// <summary>
    /// Gets the export client path settings.
    /// </summary>
    /// <value>
    /// The export client path settings.
    /// </value>
    public IList<IExportClientSettings> ExportClientSettings
    {
        get { return ExportClientConfigurationItems.ToList<IExportClientSettings>(); }
    }

    /// <summary>
    ///     Gets the section.
    /// </summary>
    /// <param name="configurationSectionName">Name of the configuration section.</param>
    /// <returns>The configuration section.</returns>
    public static IExportClientConfiguration GetSection(string configurationSectionName = "exportClientConfiguration")
    {
        IExportClientConfiguration section = ConfigurationManager.GetSection(configurationSectionName) as ExportClientConfiguration;

        return section;
    }
}

/// <summary>
/// An <c>IEnumerable</c> collection fo <c>ExportClientSettings</c>
/// </summary>
[ConfigurationCollection(typeof(ExportClientSettings), AddItemName = "exportClientSetting")]
public class ExportClientSettingsCollection : ConfigurationElementCollection, IEnumerable<ExportClientSettings>
{
    /// <summary>
    /// Gets the <see cref="ExportClientSettings"/> at the specified index.
    /// </summary>
    /// <value>
    /// The <see cref="ExportClientSettings"/>.
    /// </value>
    /// <param name="index">The index.</param>
    /// <returns></returns>
    public ExportClientSettings this[int index]
    {
        get { return BaseGet(index) as ExportClientSettings; }
    }

    /// <summary>
    /// Returns an enumerator that iterates through the collection.
    /// </summary>
    /// <returns>
    /// A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.
    /// </returns>
    public new IEnumerator<ExportClientSettings> 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;
    }
}

/// <summary>
/// The export client path settings
/// </summary>
public class ExportClientSettings : ConfigurationElement, IExportClientSettings
{
    private const string NameProperty = "name";
    private const string PathProperty = "path";

    /// <summary>
    /// Gets the name.
    /// </summary>
    /// <value>
    /// The name to identify the export path and filename.
    /// </value>
    [ConfigurationProperty(NameProperty, IsKey = true)]
    public string Name
    {
        get { return this[NameProperty].ToString(); }
    }

    /// <summary>
    /// Gets the path.
    /// </summary>
    /// <value>
    /// The path.
    /// </value>
    [ConfigurationProperty(PathProperty)]
    public string Path
    {
        get { return this[PathProperty].ToString(); }
    }
}

我想念什么?

推荐答案

问题对.

  1. ExportClientConfigurationItems属性的类型应为ExportClientSettingsCollection而不是IEnumerable<ExportClientSettings>.
  2. 在您的配置文件中,Path用大写字母表示,它应该用小写字母path=\\Example\Path"表示,因为您已经用小写的ConfigurationProperty装饰了该属性.
  1. ExportClientConfigurationItems property should be of type ExportClientSettingsCollection not IEnumerable<ExportClientSettings>.
  2. In your config file Path is in capital letters, it should be in small letters path=\\Example\Path", as you've decorated the property with ConfigurationProperty in lower case.

修复这两个问题,就可以了.

Fixing these two, it works.

这篇关于自定义配置节属性不是配置元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆