Home >Backend Development >C++ >How to Correctly Implement a ConfigurationSection with a ConfigurationElementCollection in .NET?
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.
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.
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.
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.
Here is the complete code for the necessary classes and configuration:
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; }
public class ServiceConfigurationSection : ConfigurationSection { [ConfigurationProperty("Services", IsDefaultCollection = false)] [ConfigurationCollection(typeof(ServiceCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")] public ServiceCollection Services => (ServiceCollection)base["Services"]; }
<?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>
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!