tags:

views:

2822

answers:

6

The web applications I develop often require co-dependant configuration settings and there are also settings that have to change as we move between each of our environments.

All our settings are currently simple key value pairs but it would be useful to create custom config sections so that it is obvious when two values need to change together or when the settings need to change for an environment.

What's the best way to create custom config sections and are there any special considerations to make when retrieving the values?

+2  A: 

You can accomplish this with Section Handlers. There is a basic overview of how to write one at http://www.codeproject.com/KB/aspnet/ConfigSections.aspx however it refers to app.config which would be pretty much the same as writing one for use in web.config. This will allow you to essentially have your own XML tree in the config file and do some more advanced configuration.

John Downey
+3  A: 

Quick'n Dirty:

First create your ConfigurationSection and ConfigurationElement classes:

public class MyStuffSection : ConfigurationSection
{
    ConfigurationProperty _MyStuffElement;

    public MyStuffSection()
    {
        _MyStuffElement = new ConfigurationProperty("MyStuff", typeof(MyStuffElement), null);

        this.Properties.Add(_MyStuffElement);
    }

    public MyStuffElement MyStuff
    {
        get
        {
            return this[_MyStuffElement] as MyStuffElement;
        }
    }
}

public class MyStuffElement : ConfigurationElement
{
    ConfigurationProperty _SomeStuff;

    public MyStuffElement()
    {
        _SomeStuff = new ConfigurationProperty("SomeStuff", typeof(string), "<UNDEFINED>");

        this.Properties.Add(_SomeStuff);
    }

    public string SomeStuff
    {
        get
        {
            return (String)this[_SomeStuff];
        }
    }
}

Then let the framework know how to handle your configuration classes in web.config:

<configuration>
  <configSections>
    <section name="MyStuffSection" type="MyWeb.Configuration.MyStuffSection" />
  </configSections>
  ...

And actually add your own section below:

  <MyStuffSection>
    <MyStuff SomeStuff="Hey There!" />
  </MyStuffSection>

Then you can use it in your code thus:

MyWeb.Configuration.MyStuffSection configSection = ConfigurationManager.GetSection("MyStuffSection") as MyWeb.Configuration.MyStuffSection;

if (configSection != null && configSection.MyStuff != null)
{
    Response.Write(configSection.MyStuff.SomeStuff);
}
Ishmaeel
+6  A: 

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.

I here present an example from code I use myself in one of my sites. With a constraint I dictate the maximum amount of disk space any one user is allowed to use.

MailCenterConfiguration.cs:

namespace Ani {

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

}

This is set up in web.config like so

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

Child elements

The child xml element mail is created 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 runtime will complain when the config is loaded.

MailCenterConfiguration.cs:

public sealed class MailCenterConfiguration : 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; }
}

Use

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

MailCenterConfiguration.cs

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

AnotherFile.cs

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

Check for validity

I previously mentioned that the runtime will complain when the configuration is loaded and some data does not comply to the rules you have set up (e.g. in MailCenterConfiguration.cs). I tend to want to know these things as soon as possible when my site fires up. One way to solve this is 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)
{
MailCenterConfiguration.Instance;
}
Andreas H.R. Nilsson
A: 

Even though im blowing my own trumpet (ooerr!) there is an easier way to generate Configuration Sections - I wrote a generator a few years ago, after being sick and tired of writing them over and over, it handles collections quite decently too - id appreaciate any feedback! PickledHedgehog Generator. If theres enough demand, I can add more features to it...

pzycoman
+1  A: 

The custom configuration are quite handy thing and often applications end up with a demand for an extendable solution.

For .NET 1.1 please refer the article http://aspnet.4guysfromrolla.com/articles/020707-1.aspx

Note: The above solution works for .NET 2.0 as well.

For .NET 2.0 specific solution, please refer the article http://aspnet.4guysfromrolla.com/articles/032807-1.aspx

A: 

what is MyWeb.Configuration ?

irina