Custom section in web.config

G

Guest

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"
type="ComIT.Applications.Common.ConfigurationSections.MyHandler"
allowLocation="true" allowDefinition="Everywhere" />
</configSections>

<myCustomSection>
<add key="LastErrorKey1" value="LastError" />
<add key="LastErrorKey2" value="LastError" />
<add key="LastErrorKey3" value="LastError" />
<add key="LastErrorKey4" value="LastError" />
...
</myCustomSection>

Here is my MyHandler class

namespace ComIT.Applications.Common.ConfigurationSections
{
public class MyHandler : ConfigurationSection
{
public MyHandler()
{
}
}
}

I use this code to get my section.
MyHandler config =
(MyHandler)System.Configuration.ConfigurationManager.GetSection("myCustomSection");

This gives me following error:
Unrecognized element 'add'. (c:\\Inetpub\\wwwroot\\MedTime\\web.config line
21).

I searched from msdn. I found this topic
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx. This does not fulfill
my requirement either.

I need a simple section like built-in <appSettings> og <connectionStrings>
section. How can I do this?

Cann’t I define a property to my class MyHandler as
System.Collections.Specialized.NameValueCollection that can hold all the
values under my section like ConfigurationManager.AppSettings.


Aftab Ahmad
(e-mail address removed)
 
G

Guest

Hi Tabi,

Use System.Configuration.NameValueSectionHandler, it exactly does what you
want

rgrds
 
J

Juan T. Llibre

This used to be real easy in 1.1 :

<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="customSection"
type="System.Configuration.NameValueSectionHandler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>
<customSection>
<add key="customSection Key 1" value="Value for customSection Key 1"/>
<add key="customSection Key 2" value="Value for customSection Key 2"/>
</customSection>
</configuration>

See if :
http://msdn2.microsoft.com/en-us/library/ms228062.aspx
sheds any more light on this for ASP.NET 2.0.
 
G

Guest

Hi binvij,

Have you any example for that?

Tabi

binvij said:
Hi Tabi,

Use System.Configuration.NameValueSectionHandler, it exactly does what you
want

rgrds
--
Bindesh Vijayan
MCP [ASP.NET]


Tabi said:
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"
type="ComIT.Applications.Common.ConfigurationSections.MyHandler"
allowLocation="true" allowDefinition="Everywhere" />
</configSections>

<myCustomSection>
<add key="LastErrorKey1" value="LastError" />
<add key="LastErrorKey2" value="LastError" />
<add key="LastErrorKey3" value="LastError" />
<add key="LastErrorKey4" value="LastError" />
...
</myCustomSection>

Here is my MyHandler class

namespace ComIT.Applications.Common.ConfigurationSections
{
public class MyHandler : ConfigurationSection
{
public MyHandler()
{
}
}
}

I use this code to get my section.
MyHandler config =
(MyHandler)System.Configuration.ConfigurationManager.GetSection("myCustomSection");

This gives me following error:
Unrecognized element 'add'. (c:\\Inetpub\\wwwroot\\MedTime\\web.config line
21).

I searched from msdn. I found this topic
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx. This does not fulfill
my requirement either.

I need a simple section like built-in <appSettings> og <connectionStrings>
section. How can I do this?

Cann’t I define a property to my class MyHandler as
System.Collections.Specialized.NameValueCollection that can hold all the
values under my section like ConfigurationManager.AppSettings.


Aftab Ahmad
(e-mail address removed)
 
G

Guest

Hi Juan,

I have a code for .NET Framework versions 1.0 and 1.1 and I already have
read this topic, but they have also written a note in this topic.

In the .NET Framework versions 1.0 and 1.1, a configuration section handler
had to implement the System.Configuration.IConfigurationSectionHandler
interface, which is now deprecated. However, a code example exists in How to:
Create Custom Configuration Sections Using IConfigurationSectionHandler.

I want a real .NET Framework version 2.0 code. Have you any example for
that? Thanks in advanced.

Aftab Ahmad
 
J

Juan T. Llibre

Maybe I'm reading that page wrong, but just because it says that
"a configuration section handler had to implement the
System.Configuration.IConfigurationSectionHandler interface,
which is now deprecated" doesn't mean that page shows how to use
the System.Configuration.IConfigurationSectionHandler interface.

In fact, it doesn't.

The sample for 1.0/1.1 is at :
http://msdn2.microsoft.com/en-us/library/ms228056.aspx
and is a totally different coding method than what
http://msdn2.microsoft.com/en-us/library/ms228062.aspx
and the links on that page demonstrate.

See, in :
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx
the sections titled :

"To create a custom configuration section handler"
"To add a custom section handler to an ASP.NET configuration file"
"To programmatically access your custom configuration data"

Those do *not* use "IConfigurationSectionHandler"; they use
"System.Configuration.ConfigurationSection", a 2.0 class.

See this reference:
http://msdn2.microsoft.com/en-us/library/system.configuration.configurationsection.aspx

There's example code on that page.
 
G

Guest

Hi Juan,

Thanks for your answer. I have already read all these links. I am trying to
find this out for last three days.

If you read my first email. I am using ConfigurationSection class but this
class supports only node without child nodes in the web.config. That means
that you can not populate children to this node. If you try to add “add†node
then you get following error.

"Unrecognized element 'add'. (c:\\Inetpub\\wwwroot\\MedTime\\web.config line
21.)"

When I use IConfigurationSectionHandler with
ConfigurationSettings.GetConfig() method then although it works but at the
same time it gives warning that “This method is obsolete. This method has
been replaced by ConfigurationManager.GetSectionâ€.

Why have they made it so difficult? It was so easy in .NET 1.1. As I have
written before that I have also read this topic
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx where you can add
section group and populate also section child nodes but this is not good
solution. It works fine if you have only one section and child nodes but if
you have 100 sections of different types then your have create 100 classes to
read every section. Is it correct?

Is there any better solution in .NET 2.0? Thanks in advanced.

Aftab
 

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

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top