Adding Custom Configuration Section in .NET Configuration Files

In order to add a Custom Configuration Section in any of .NET configuration files like app.config or web.cofig, the framework provides classes in the System.Configuration namespace such as System.Configuration.ConfigurationSection, System.Configuration.ConfigurationElement, System.Configuration.ConfigurationElementCollection and others.

Lets say we want to add the following Custom Section in the configuration file which can hold a list of assemblies.

<CustomAssemblySection>

    <assemblies>

      <add name="System.Configuration"></add>

      <add name="System.Data.Entity"></add>

      <add name="System.Data"></add>

      <add name="System.Xml"></add>

      <add name="System.Xml.Linq"></add>

      <add name="System.Core"></add>

    </assemblies>

  </CustomAssemblySection>

We need a Custom ConfigurationSection, a Custom ConfigurationElementCollection (assemblies) and a Cusom ConfigurationElement with one property which is “name” of the assembly. The following code is achieving exactly that. We have three classes CustomAssemblySection, CustomAssemblyCollection and CustomAssembly which inherit from the respective classes of System.Configuration.

Following is code for a simple Console Application with Program.cs and App.config files. The Program Main is reading the configuration and writing the assembly names on the Console.

For further reading you can refer to MSDN How To: http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Configuration;

namespace CustomConfigurationSectionConsole

{

    class Program

    {

        static void Main(string[] args)

        {

            CustomAssemblySection customSection = (CustomAssemblySection)System.Configuration.ConfigurationManager.GetSection("CustomAssemblySection");

             foreach (CustomAssemblyElement customAssembly in customSection.Assemblies)

             {

                 Console.WriteLine(customAssembly.Name);

             }

             Console.ReadLine();

        }

    }

    public class CustomAssemblySection : ConfigurationSection

    {

        [ConfigurationProperty("assemblies", IsDefaultCollection = false)]

        [ConfigurationCollection(typeof(CustomAssemblyCollection))]

        public CustomAssemblyCollection Assemblies

        {

            get

            {

                return (CustomAssemblyCollection)base["assemblies"];

            }

        }

    }

    public class CustomAssemblyCollection : ConfigurationElementCollection

    {

        protected override ConfigurationElement CreateNewElement()

        {

            return new CustomAssemblyElement();

        }

        protected override object GetElementKey(ConfigurationElement element)

        {

            return ((CustomAssemblyElement)element).Name;

        }

        public CustomAssemblyElement this[int Index]

        {

            get

            {

                return (CustomAssemblyElement)BaseGet(Index);

            }

            set

            {

                if (BaseGet(Index) != null)

                {

                    BaseRemoveAt(Index);

                }

                BaseAdd(Index, value);

            }

        }

        new public CustomAssemblyElement this[string Name]

        {

            get

            {

                return (CustomAssemblyElement)BaseGet(Name);

            }

        }

    }

    public class CustomAssemblyElement : ConfigurationElement

    {

        [ConfigurationProperty("name", IsRequired = true)]

        public string Name

        {

            get

            {

                return (string)this["name"];

            }

            set

            {

                this["name"] = value;

            }

        }

    }

}

App.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <section name="CustomAssemblySection" type="CustomConfigurationSectionConsole.CustomAssemblySection, CustomConfigurationSectionConsole"/>

  </configSections>

    <startup>

        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

    </startup>

  <CustomAssemblySection>

    <assemblies>

      <add name="System.Configuration"></add>

      <add name="System.Data.Entity"></add>

      <add name="System.Data"></add>

      <add name="System.Xml"></add>

      <add name="System.Xml.Linq"></add>

      <add name="System.Core"></add>

    </assemblies>

  </CustomAssemblySection>

</configuration>

Advertisements

One thought on “Adding Custom Configuration Section in .NET Configuration Files

  1. Pingback: Dynamically Adding DbSet Properties in DbContext for Entity Framework Code First | Aleem's Weblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s