473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Custom configuration section handlers

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="fo*@bar.com" />
</mode>
<mode value="dev">
<add key="ConnectionString" value="blah" />
<add key="WebmasterEmail" value="fo*@bar.com" />
</mode>
</site>
<site url="/intranet/">
<mode value="live">
<add key="ConnectionString" value="blah2" />
<add key="WebmasterEmail" value="fr**@bar.com" />
</mode>
<mode value="dev">
<add key="ConnectionString" value="blah2" />
<add key="WebmasterEmail" value="fr**@bar.com" />
</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...
Nov 18 '05 #1
4 1893
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
"Nick Gilbert" <ne**@nickgilbert.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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="fo*@bar.com" />
</mode>
<mode value="dev">
<add key="ConnectionString" value="blah" /> <add key="WebmasterEmail"
value="fo*@bar.com" />
</mode>
</site>
<site url="/intranet/">
<mode value="live">
<add key="ConnectionString" value="blah2" /> <add key="WebmasterEmail"
value="fr**@bar.com" />
</mode>
<mode value="dev">
<add key="ConnectionString" value="blah2" /> <add key="WebmasterEmail"
value="fr**@bar.com" />
</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...

Nov 18 '05 #2
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...oducingxmlseri
alization.asp?frame=true

#Examples of XML Serialization
http://msdn.microsoft.com/library/en...ampleofxmlseri
alizationwithxmlserializer.asp?frame=true

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/en...rollingseriali
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

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

Thanks,

Nick...
Nov 18 '05 #4
Hi Nick,

Thanks for the response. Well, if you have any further questions later or
need any other help, please also feel free to post here. Have a good day!
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

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andreas Jung | last post by:
I am trying to write a custom logger with a custom handler: class MyHandler(StreamHandler): pass class Logger: def __init__(self): self._l = logging.getLogger('someident')...
0
by: Søren Lund | last post by:
Hello, I have implemented a custom config section handler by implementing the IConfigurationSectionHandler interface. I have registered this handler in web.config and everything works fine ......
2
by: murl | last post by:
Below is what i have as the custom section in the web.config file... <?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- register local configuration handlers --> <configSections>...
3
by: rdcpro | last post by:
Hi all, I've been building a nifty deserializing configuration handler that I use in conjunction with my web.config in an ASP.NET web app. This is working quite well, but I'm planning on...
6
by: Tabi | last post by:
Hi, I want to create a custom section in my web.config that can hold my custom values. I created a section in web.config as written below. <configSections> <section name="myCustomSection"...
1
by: npaulus | last post by:
Hi I am trying to experiment with a custom configuration section in app.config but it just doesnt work. app.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections>...
7
by: =?Utf-8?B?RG91Z2llIEJyb3du?= | last post by:
Hi I've written custom configuration section (inherits from System.Configuration.ConfigurationSection) to simplify the contents of the config file and to make life easier when accessing them in...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
0
by: =?Utf-8?B?UGhpbGlw?= | last post by:
I have a web.config custom configuration section using asp.net 2.0 configuration APIs. My custom configuration section inherits from System.Configuration.ConfigurationSection. I have a IIS root...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.