Custom Config Section属性不是配置元素

tdb*_*ett 4 c# app-config

我需要另外一套眼睛来解决这个问题.我得到了一个

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

我打电话时会抛出异常 ExportClientConfiguration.GetSection();

这是我的代码:

App.config中

  <configSections>
<section name="exportClientConfiguration" type="ClientConversionUtilityBroker.ConfigurationModels.ExportClientPathConfiguration.ExportClientConfiguration, ClientConversionUtilityBroker"/>
</configSections>
<exportClientConfiguration>
  <exportClientSettings>
    <exportClientSetting name="Example" Path="\\Example\Path" />
  </exportClientSettings>
</exportClientConfiguration>
Run Code Online (Sandbox Code Playgroud)

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(); }
    }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Sri*_*vel 9

几个问题.

  1. ExportClientConfigurationItems属性应该是类型的ExportClientSettingsCollection不是IEnumerable<ExportClientSettings>.
  2. 在你的配置文件Path是大写字母,它应该是小写字母path=\\Example\Path",因为你用小写字母装饰属性ConfigurationProperty.

修复这两个,它的工作原理.

  • 在补充这个答案,你可以保持你的界面在机智`IExportClientConfiguration`,通过使私人吸气的.NET配置框架,然后返回,作为`的IEnumerable <ExportClientSettings>`从后面的接口契约的公共的getter. (2认同)