Нераспознанное исключение элемента с пользовательским C # config - PullRequest
9 голосов
/ 11 сентября 2011

У меня есть следующие биты в App.config для .NET 3.5 Windows Service:

<configSections>
    <section name="ConfigurationServiceSection" type="SomeApp.Framework.Configuration.ConfigurationServiceSection, SomeApp.Framework"/>
</configSections>

<ConfigurationServiceSection configSource="ConfigSections\configurationServiceSection.config" />

У меня есть это в configurationServiceSection.config:

<ConfigurationServiceSection>
    <ConfigurationServices>
        <ConfigurationService name="LocalConfig" host="localhost" port="40001" location="LON"/>
    </ConfigurationServices>
</ConfigurationServiceSection>

И воткод:

using System.Configuration;

namespace SomeApp.Framework.Configuration
{
    public sealed class ConfigurationServiceSection : ConfigurationSection
    {
        [ConfigurationProperty("ConfigurationServices", IsDefaultCollection = true, IsRequired = true)]
        [ConfigurationCollection(typeof(ConfigurationServices))]
        public ConfigurationServices ConfigurationServices
        {
            get
            {
                return (ConfigurationServices)base["ConfigurationServices"];
            }
        }
    }

    public sealed class ConfigurationServices : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ConfigurationService();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            ConfigurationService configService = (ConfigurationService) element;
            return configService.Name;
        }
    }

    public sealed class ConfigurationService : ConfigurationElement
    {
        /// <summary>
        /// name
        /// </summary>
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        /// <summary>
        /// host
        /// </summary>
        [ConfigurationProperty("host", IsKey = false, IsRequired = true)]
        public string Host
        {
            get { return (string)this["host"]; }
            set { this["host"] = value; }
        }

        /// <summary>
        /// port
        /// </summary>
        [ConfigurationProperty("port", IsKey = false, IsRequired = true)]
        public string Port
        {
            get { return (string)this["port"]; }
            set { this["port"] = value; }
        }

        /// <summary>
        /// location
        /// </summary>
        [ConfigurationProperty("location", IsKey = false, IsRequired = true)]
        public string Location
        {
            get { return (string)this["location"]; }
            set { this["location"] = value; }
        }
    }
}

Когда я пытаюсь получить доступ к конфигурации со следующим:

var configurationServiceSection = (ConfigurationServiceSection)configuration.GetSection("ConfigurationServiceSection");

Я получаю это исключение:

Unrecognized element 'ConfigurationService'. (C:\Code\branches\ConfigurationService\SomeApp\Src\ConfigService\SomeApp.ConfigService.WindowsService\bin\Debug\ConfigSections\configurationServiceSection.config line 3)

Все выглядит в порядкедля меня?

Есть идеи, пожалуйста?Благодаря.

1 Ответ

11 голосов
/ 12 сентября 2011

Хорошо, дошло до этого:

Я добавил AddItemName в класс ConfigurationServiceSection, как показано ниже:

public sealed class ConfigurationServiceSection : ConfigurationSection
{
    [ConfigurationProperty("ConfigurationServices", IsDefaultCollection = true, IsRequired = true)]
    [ConfigurationCollection(typeof(ConfigurationServices), AddItemName = "ConfigurationService")]
    public ConfigurationServices ConfigurationServices
    {
        get
        {
            return (ConfigurationServices)base["ConfigurationServices"];
        }
    }
}

Другой альтернативой было переопределение свойств CollectionType и ElementName, как показано ниже:

public override ConfigurationElementCollectionType CollectionType
{
    get { return ConfigurationElementCollectionType.BasicMap; }
}

protected override string ElementName
{
    get { return "ConfigurationService"; }
}
...