Home >Backend Development >C++ >How to Correctly Implement a ConfigurationSection with a ConfigurationElementCollection in .NET?

How to Correctly Implement a ConfigurationSection with a ConfigurationElementCollection in .NET?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-27 05:38:09812browse

How to Correctly Implement a ConfigurationSection with a ConfigurationElementCollection in .NET?

Implementing a ConfigurationSection with a ConfigurationElementCollection

When customizing configurations, you may encounter issues while implementing a configuration section. This article addresses the use of a ConfigurationSection with a ConfigurationElementCollection, clarifying misconceptions and providing guidance on correct implementation.

Context

You have defined a custom configuration section in your App.config file with a ServicesSection and a ServiceCollection element. However, despite creating element classes (ServiceConfig and ServiceCollection), you encountered exceptions with the configuration section handler.

Solution

Configuration Section Handler

You initially attempted to use the IConfigurationSectionHandler interface, which is now deprecated. The correct approach is to create a configuration section class that derives from ConfigurationSection. In your case, you need a ServiceConfigurationSection class. This class should define a Services property that is a ConfigurationCollection of ServiceConfig elements.

Configuration Collection Implementation

Within your ServiceCollection class, be sure to extend ConfigurationElementCollection and override the necessary methods to create new elements (CreateNewElement), retrieve element keys (GetElementKey), and perform operations such as adding, removing, and clearing elements.

Final Code

Here is the complete code for the necessary classes and configuration:

ServiceConfig:

public class ServiceConfig : ConfigurationElement
{
    [ConfigurationProperty("Port", DefaultValue = 0, IsRequired = true, IsKey = true)]
    public int Port { get; set; }

    [ConfigurationProperty("ReportType", DefaultValue = "File", IsRequired = true, IsKey = false)]
    public string ReportType { get; set; }
}

ServiceCollection:

public class ServiceCollection : ConfigurationElementCollection
{
    public override ConfigurationElement CreateNewElement() => new ServiceConfig();
    protected override object GetElementKey(ConfigurationElement element) => ((ServiceConfig)element).Port;
}

ServiceConfigurationSection:

public class ServiceConfigurationSection : ConfigurationSection
{
    [ConfigurationProperty("Services", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(ServiceCollection),
        AddItemName = "add",
        ClearItemsName = "clear",
        RemoveItemName = "remove")]
    public ServiceCollection Services => (ServiceCollection)base["Services"];
}

App.config Configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="ServicesSection" type="RT.Core.Config.ServiceConfigurationSection, RT.Core" />
    </configSections>
    <ServicesSection>
        <Services>
            <add Port="6996" ReportType="File" />
            <add Port="7001" ReportType="Other" />
        </Services>
    </ServicesSection>
</configuration>

Consumption Example:

ServiceConfigurationSection serviceConfigSection = ConfigurationManager.GetSection("ServicesSection") as ServiceConfigurationSection;
ServiceConfig serviceConfig = serviceConfigSection.Services[0];

By following these steps, you can successfully implement a custom configuration section with a collection element.

The above is the detailed content of How to Correctly Implement a ConfigurationSection with a ConfigurationElementCollection in .NET?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn