How to create a custom section that behaves like an AppSettings section?
Asked Answered
R

2

12

I want to have the following structure in my config:

<MySection>  
  <add key="1" value="one" />  
  <add key="2" value="two" />
  <add key="3" value="three" />
</MySection>

I have a limitation such that MySection can't use the AppSettingsSection as it has to inherit from a different parent custom section. And I need to resolve this section to a NameValueCollection such that when I call something like:

ConfigurationManager.GetConfig("MySection")

it should return a NameValueCollection. How to go about doing this? I found some information on NameValueConfigurationCollection but that's not what I'm looking for.

Rarefy answered 19/9, 2011 at 12:52 Comment(2)
Possible duplication of #2715060Jankey
Key difference is that I want to return a NameValueCollection and not a custom configurationsection/elementcollection.Rarefy
R
8

This worked --
Code:

class Program
{
    static void Main(string[] args)
    {
        NameValueCollection nvc = ConfigurationManager.GetSection("MyAppSettings") as NameValueCollection;
        for(int i=0; i<nvc.Count; i++)
        {
            Console.WriteLine(nvc.AllKeys[i] + " " + nvc[i]);
        } 
        Console.ReadLine();
    }
}

class ParentSection : ConfigurationSection
{ 
    //This may have some custom implementation
}

class MyAppSettingsSection : ParentSection
{
    public static MyAppSettingsSection GetConfig()
    {
        return (MyAppSettingsSection)ConfigurationManager.GetSection("MyAppSettings");
    }


    [ConfigurationProperty("", IsDefaultCollection = true)]
    public NameValueConfigurationCollection Settings
    {
        get
        {
            return (NameValueConfigurationCollection)base[""];
        }
    }
}

Configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!-- <section name="MyAppSettings" type="CustomAppSettings.MyAppSettingsSection, CustomAppSettings"/> -->
    <section name="MyAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

  </configSections>

  <MyAppSettings>
    <add key="1" value="one"/>
    <add key="2" value="two"/>
    <add key="3" value="three"/>
    <add key="4" value="four"/>
  </MyAppSettings>
</configuration>

My major concern was that my section needed to inherit from a custom section and I wanted to return a NameValueCollection when the ConfigurationManager.GetSection("MyAppSettings") is called.
I changed the type property to AppSettingsSection, even though it's nowhere in the picture and it worked. Now I need to figure out how that worked but for now the good thing is that I have a working sample :)

Update: Unfortunately, this wasn't the expected way of achieving what was intended because now the custom section is not coming into the picture at all so unfortunately this isn't the best way to do it.

Of course, if you just wanted to rename your appsettings section this would work like a charm.

Rarefy answered 20/9, 2011 at 5:2 Comment(3)
You could use NameValueFileSectionHandler or NameValueConfigurationCollection insteadTerminate
Actually I think the MyAppSettings section items should use "name" instead of "key"Grabble
@VasilTrifonov Yes. That's why I couldn't override AppSettings section.Rarefy
B
2

you should create a class which derives from ConfigurationSection

see a full example here: How to: Create Custom Configuration Sections Using ConfigurationSection

Ballplayer answered 19/9, 2011 at 12:55 Comment(3)
I'm doing that but I'm not able to return a NameValueCollection.Rarefy
Wouldn't matter. I was able to return a namevalueconfigurationcollection but not namevaluecollection. Here's the snippet for the former: geekswithblogs.net/sdorman/archive/2008/03/16/…Rarefy
The configuration tool worked great in vs2015! It generated a more complicated section.Rovelli

© 2022 - 2024 — McMap. All rights reserved.