Define custom web.config sections with potential child elements and attributes for the properties?

What is the best way to create custom config sections and are there any special considerations to make when retrieving the values? Yes, we can do this . Let us see here How to define custom web.config sections with potential child elements and attributes for the properties?
Using attributes, child config sections and constraints

There is also the possibility to use attributes which automatically takes care of the plumbing, as well as providing the ability to easily add constraints.

Here I am giving an example .

 

Dotnetstock.cs:

namespace Dotnet {

public sealed class Dotnetstock : ConfigurationSection
{
[ConfigurationProperty("userDiskSpace", IsRequired = true)]
[IntegerValidator(MinValue = 0, MaxValue = 1000000)]
public int UserDiskSpace
{
get { return (int)base["userDiskSpace"]; }
set { base["userDiskSpace"] = value; }
}
}
}

In web.config

<configSections>
<!-- Mailcenter configuration file -->
<section name="mailCenter" type="Dotnet.Dotnetstock" requirePermission="false"/>
</configSections>
...
<mailCenter userDiskSpace="25000">
<mail
host="my.hostname.com"
port="366" />
</mailCenter>

Child elements

The child xml element mail is created in the same .cs file as the one above. Here I’ve added constraints on the port. If the port is assigned a value not in this range the run-time will complain when the config is loaded.

Dotnetstock.cs:

public sealed class Dotnetstock : ConfigurationSection
{
[ConfigurationProperty("mail", IsRequired=true)]
public MailElement Mail
{
get { return (MailElement)base["mail"]; }
set { base["mail"] = value; }
}

public class MailElement : ConfigurationElement
{
[ConfigurationProperty("host", IsRequired = true)]
public string Host
{
get { return (string)base["host"]; }
set { base["host"] = value; }
}

[ConfigurationProperty("port", IsRequired = true)]
[IntegerValidator(MinValue = 0, MaxValue = 65535)]
public int Port
{
get { return (int)base["port"]; }
set { base["port"] = value; }
}

To use this practically in code, all you have to do is instantiate the DotnetstockObject, this will automatically read the relevant sections from web.config.

Dotnetstock.cs

private static Dotnetstock instance = null;
public static Dotnetstock Instance
{
get
{
if (instance == null)
{
instance = (Dotnetstock)WebConfigurationManager
.GetSection("mailCenter");
}
return instance;
}
}
Mail.cs

public void SendMail()
{
Dotnetstock conf = Dotnetstock.Instance;
SmtpClient smtpClient = new SmtpClient(conf.Mail.Host, conf.Mail.Port);
}

Load the configuration in Global.asax.cx.Application_Start , if the configuration is invalid you will be notified of this with the means of an exception. Your site won’t start and instead you will be presented detailed exception information in the Yellow screen of death.

Global.asax.cs

protected void Application_ Start(object sender, EventArgs e)
{
Dotnetstock.Instance;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *