Custom configuration section handlers

N

Nick Gilbert

Hi,

I would like the ability to store the configuration settings for all
versions of my site in a single web.config file by using different
sections. Eg:

<siteConfig>
<machine name="XENON">
<site url="/blog/">
<mode value="live">
<add key="ConnectionString" value="blah" />
<add key="WebmasterEmail" value="(e-mail address removed)" />
</mode>
<mode value="dev">
<add key="ConnectionString" value="blah" />
<add key="WebmasterEmail" value="(e-mail address removed)" />
</mode>
</site>
<site url="/intranet/">
<mode value="live">
<add key="ConnectionString" value="blah2" />
<add key="WebmasterEmail" value="(e-mail address removed)" />
</mode>
<mode value="dev">
<add key="ConnectionString" value="blah2" />
<add key="WebmasterEmail" value="(e-mail address removed)" />
</mode>
</site>
</machine>

<machine name="NEON">
...(variation on above)
</machine>
</siteConfig>

How can I write a configuration section handler that will allow me to do
this? There are some examples on the web but most of them don't have any
attributes on the custom section names, so I'm not sure it's possible to
do what I want.

If it's possible to retrieve the values somehow using the structure
above, then it will be simple for me to write the code that accesses the
correct nodes based on things like Server.MachineName and Request.Url etc.

If anyone has seen any code which might help me, please let me know.

Thanks,

Nick...
 
K

Kyril Magnos

Ok, here you go :)

First off, you need to have a <configSections> tag. This tag will contain
child tags named <section>

<configSections>
<section name="MyWebAppV1" type="MyWebAppV1.V1ConfigSection, MyWebAppV1"
/>
<section name="MyWebAppV2" type="MyWebAppV1.V2ConfigSection, MyWebAppV1"
/>
</configSections>

Then, you will create a tag with the name of the section that you named in
the configSections.

<MyWebAppV1>
<MachineName>XENON</MachineName>
<SiteUrl>/blog/</SiteUrl>
</MyWebAppV1>

<MyWebAppV2>
<MachineName>XENON - V2</MachineName>
<SiteUrl>/blog/v2/</SiteUrl>
</MyWebAppV2>

Now, you are probably wondering about how you can access this information,
right? Well, it's easy. You need to create a class that inherits from
IConfigurationSectionHandler and a config class that matches the layout of
your custom section.

using System.Xml;
using System.Xml.Serialization;
namespace MyWebAppV1
{
public class V1ConfigSection: IConfigurationSectionHandler
{
public object Create(
object parent,
object configContext,
XmlNode section )
{

MyWebAppV1Config config;
XmlSerializer serializer = new
XmlSerializer(typeof(MyWebAppV1Config));
XmlNodeReader reader = new XmlNodeReader(section);
config = (MyWebAppV1Config)serializer.Deserialize(reader);
reader.Close();

return config;
}
}

[Serializable]
[XmlType]
public class MyWebAppV1Config
{
[XmlElement]
public string MachineName;

[XmlElement]
public string SiteUrl;
}
}

Finally, to call your custom config section, you use the
System.Configuration namespace.

MyWebAppV1Config config =
(MyWebAppV1Config)ConfigurationSettings.GetConfig("MyWebAppV1");

You can also use the ConfigurationSettings.GetConfig by itself, but I have
found that doing it this way is a lot nicer. Now, there are all sorts of
ways that you can go forward with this. I would start by reading up on Xml
Serialization and what you can do with it.

HTH,

Kyril
 
S

Steven Cheng[MSFT]

Thanks a lot for Kyril's detaild and nice explanation.

Hi Nick,

As Nick has mentioned, the most important thing in addition to defining a
SectionHandler is to be familiar with the .NET 's XmlSerialization feature.
This can help use Serialize a class object(instance) into Xml Data also
Deserialize Xml Data into a class object. All these magics are mostly
provided by the .net's Custom Attributes which can help controling the
format of the Serializing class. Here are some reference in the MSDN on
XmlSerialization:

#Introducing XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconintroducingxmlseri
alization.asp?frame=true

#Examples of XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconanexampleofxmlseri
alizationwithxmlserializer.asp?frame=true

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcontrollingseriali
zationbyxmlserializerwithattributes.asp?frame=true

Hope also helps. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
N

Nick Gilbert

Thanks to Kyril and Steven for your detailed replies! I'll have a go and
see what I can come up with...

Thanks,

Nick...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top