Custom Configuration Sections in C# Example

Why Custom configuration Section Needed?

In Asp.net we can read configuration details from our web.config simply by reading the key value using ConfigurationManager.AppSettings["KeyName"] straightway! then you must be wondering what would be the use of configuration framework ?

In big enterprise business you many need to keep separate version of complex object structure information in some configuration file, writing them all in web config file may be dirty and tedious job.

Separate configuration file helps us to read easily maintainable clean configuration information

Custom Configuration Example

Here we create a small XML file to store some email related information and learn how we can read the file using configuration framework

Namespace Required
using System.Configuration;    
using System.Xml;
using System.Xml.Serialization;
Create Custom config file

Step 1: We create an xml file with name “SystemSettings.xml” and store the information below

<?xml version="1.0" encoding="utf-8" ?>
<SystemSettings>
<SmtpServer>mysmtp.server.ip.com</SmtpServer>
<SmtpUserName>contact@mydomainname.com</SmtpUserName>
<SmtpPassword>my*smtp@pass</SmtpPassword>
<SMTPPort>25</SMTPPort>
<EmailHeader>
<![CDATA[
<div>
WebTrainingRoom.Com - Web Software Development Training
</div>
]]>
</EmailHeader>
<EmailFooter>
<![CDATA[
<div style='background-color:#eee;padding:15px;text-align:right;'>
WebTrainingRoom.Com
</div>
]]>
</EmailFooter>
</SystemSettings>
Read custom config file in C#

Step 2: Create a Handler inherited from IConfigurationSectionHandler and a class called “SystemSettings” with few properties in it.

Notice, above each property there is a XmlElement, and the XmlElement name is kept same as XML tag in above XML file (ex: [XmlElement("SMTPPort")])


public class SystemSettingsHandler : IConfigurationSectionHandler            
{
    public object Create(object parent, object configContext, System.Xml.XmlNode section)
    {
    XmlSerializer ser = new XmlSerializer(typeof(SystemSettings));
    return ser.Deserialize(new XmlNodeReader(section));
    }
}
            
public class SystemSettings
{
    [XmlElement("SmtpServer")]
    public string SmtpServer { get; set; }
            
    [XmlElement("SMTPPort")]
    public int SMTPPort { get; set; }
            
    [XmlElement("SmtpUserName")]
    public string SmtpUserName { get; set; }
            
    [XmlElement("SmtpPassword")]
    public string SmtpPassword { get; set; }
            
    public static SystemSettings GetSystemSettings(string sectionPath)
    {
        return ConfigurationManager.GetSection(sectionPath) as SystemSettings;
    }
}
Add custom section to web.config

Step 3: Now you have to add the custom configuration details in web.config inside configuration tag

<configSections>
<sectionGroup name="CustomSetting">
<section name="SystemSettings" type="MyAssemblyName.Configs.SystemSettingsHandler,MyAssemblyName" />
</sectionGroup>
</configSections>
<CustomSetting>      
<SystemSettings configSource="ConfigurationFiles\SystemSettings.xml" />
</CustomSetting>

Step 4: Let's check if configuration details coming properly, here we have one class called "WTRMail", and SystemSettings sSetting = SystemSettings.GetSystemSettings("CustomSetting/SystemSettings"); this will load the information into custom object.

public class WTRMail                
{
    private MyEmail eMail;
                
    SystemSettings sSetting = SystemSettings.GetSystemSettings("CustomSetting/SystemSettings");
                
    public WTRMail()
    {
        eMail = new MyEmail(sSetting.SmtpServer);
        eMail.SmtpUserName = sSetting.SmtpUserName;
        eMail.SmtpUserPassword = sSetting.SmtpPassword;
        eMail.SMTPPort = sSetting.SMTPPort;
        eMail.EmailHeader = sSetting.EmailHeader;
        eMail.EmailFooter = sSetting.EmailFooter;
    }
}

Enjoy, use the WTRMail class to send email in your application

 
Custom Configuration in C#
Aspnet MVC Training
Asp.net MVC tutorials, learn model view controllers with c#, develop database driven web application using Asp.net MVC framework.
Hire .Net Developer
Free Tutorials
ASP.NET MVC Interview Questions Answers
Asp.Net MVC C# Examples | Join Asp.Net MVC Course | Asp.net Core Tutorial