Nested ConfigurationElementCollections

Discussion in 'ASP .Net' started by Mark Olbert, Jan 25, 2006.

  1. Mark Olbert

    Mark Olbert Guest

    I'd like to create a ConfigurationSection to handle something that looks like this:

    <SiteSettings>
    <site name="develop" smtp="mail.arcabama.com" host="localhost">
    <mappings>
    <membership name="a" column="cola" />
    <membership name="b" column="colb" />
    </mappings>
    </site>
    <site name="production" smtp="smtp.alentus.com" host="www.arcabama.com">
    <mappings>
    <membership name="a" column="cola" />
    <membership name="b" column="colb" />
    </mappings>
    </site>
    </SiteSettings>

    I thought I could do this with the following classes (sorry about the length):

    public class SiteSection : ConfigurationSection
    {
    [ ConfigurationProperty("", IsDefaultCollection = true) ]
    public SiteCollection Sites
    {
    get { return (SiteCollection) base[""]; }
    }
    }

    public class SiteCollection : ConfigurationElementCollection
    {
    public new SiteElement this[string name]
    {
    get
    {
    if( IndexOf(name) < 0 ) return null;

    return (SiteElement) BaseGet(name);
    }
    }

    public SiteElement this[int index]
    {
    get { return (SiteElement) BaseGet(index); }
    }

    public int IndexOf( string name )
    {
    name = name.ToLower();

    for( int idx = 0; idx < base.Count; idx++ )
    {
    if( this[idx].Name.ToLower() == name )
    return idx;
    }

    return -1;
    }

    public override ConfigurationElementCollectionType CollectionType
    {
    get { return ConfigurationElementCollectionType.BasicMap; }
    }

    protected override ConfigurationElement CreateNewElement()
    {
    return new SimpleSiteSetting();
    }

    protected override object GetElementKey( ConfigurationElement element )
    {
    return ( (SiteElement) element ).Name;
    }

    protected override string ElementName
    {
    get { return "site"; }
    }
    }

    public class SiteElement : ConfigurationElement
    {
    [ ConfigurationProperty("name", DefaultValue = "develop", IsRequired = true, IsKey = true) ]
    public string Name
    {
    get { return (string) this["name"]; }
    set { this["name"] = value; }
    }

    [ ConfigurationProperty("smtp", DefaultValue = "mail.arcabama.com", IsRequired = true, IsKey = false) ]
    public string SmtpServer
    {
    get { return (string) this["smtp"]; }
    set { this["smtp"] = value; }
    }

    [ ConfigurationProperty("host", DefaultValue = "localhost", IsRequired = true, IsKey = false) ]
    public string Host
    {
    get { return (string) this["host"]; }
    set { this["host"] = value; }
    }

    [ ConfigurationProperty("mappings", IsDefaultCollection = false) ]
    public MembershipCollection Mappings
    {
    get { return (MembershipCollection) base["mappings"]; }
    }
    }

    public class MembershipCollection : ConfigurationElementCollection
    {
    public new MembershipElement this[string name]
    {
    get
    {
    if( IndexOf(name) < 0 ) return null;

    return (MembershipElement) BaseGet(name);
    }
    }

    public MembershipElement this[int index]
    {
    get { return (MembershipElement) BaseGet(index); }
    }

    public int IndexOf( string name )
    {
    name = name.ToLower();

    for( int idx = 0; idx < base.Count; idx++ )
    {
    if( this[idx].Name.ToLower() == name )
    return idx;
    }
    return -1;
    }

    public override ConfigurationElementCollectionType CollectionType
    {
    get { return ConfigurationElementCollectionType.BasicMap; }
    }

    protected override ConfigurationElement CreateNewElement()
    {
    return new MembershipElement();
    }

    protected override object GetElementKey( ConfigurationElement element )
    {
    return ( (MembershipElement) element ).Name;
    }

    protected override string ElementName
    {
    get { return "membership"; }
    }
    }

    public class MembershipElement : ConfigurationElement
    {
    [ ConfigurationProperty("name", DefaultValue = "", IsRequired = true, IsKey = true) ]
    public string Name
    {
    get { return (string) this["name"]; }
    set { this["name"] = value; }
    }

    [ ConfigurationProperty("column", DefaultValue = "", IsRequired = true, IsKey = false) ]
    public string Column
    {
    get { return (string) this["column"]; }
    set { this["column"] = value; }
    }
    }

    But while I can watch the methods in MembershipCollection get called -- and see that the web.config entries corresponding to the
    MembershipElements are being parsed -- the Mappings collection in SiteElement always ends up being empty.

    How do I go about nesting one ConfigurationElementCollection within another?

    - Mark
     
    Mark Olbert, Jan 25, 2006
    #1
    1. Advertising

  2. Hello Mark,

    I got different results with same code, the Mappings collection in
    SiteElement return correct result. Here is the code I used (I tested in a
    winform application):

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>



    <configSections>

    <section
    name="SiteSettings"
    type="WindowsApplication8.SiteSection,WindowsApplication8"
    allowLocation="true"
    allowDefinition="Everywhere" />


    </configSections>


    <SiteSettings>
    <site name="develop" smtp="mail.arcabama.com" host="localhost">
    <mappings>
    <membership name="a" column="cola" />
    <membership name="b" column="colb" />
    </mappings>
    </site>
    <site name="production" smtp="smtp.alentus.com" host="www.arcabama.com">
    <mappings>
    <membership name="a" column="cola" />
    <membership name="b" column="colb" />
    </mappings>
    </site>



    </SiteSettings>


    </configuration>



    SiteSection configSection =
    (SiteSection)System.Configuration.ConfigurationManager.GetSection("SiteSetti
    ngs");

    propertyGrid1.SelectedObject = configSection;
     
    Luke Zhang [MSFT], Jan 25, 2006
    #2
    1. Advertising

  3. Mark Olbert

    Mark Olbert Guest

    Interesting. But I'm using this as part of a website. Why would the same code work in a WinForm, but not in a website?

    - Mark
     
    Mark Olbert, Jan 25, 2006
    #3
  4. Mark Olbert

    Mark Olbert Guest

    Okay, problem solved (it was a boneheaded mistake on my end).

    - Mark
     
    Mark Olbert, Jan 25, 2006
    #4
  5. Thank you for update. I am glad the problem was resolved.

    Luke
     
    Luke Zhang [MSFT], Jan 26, 2006
    #5
  6. "Mark Olbert" wrote:

    > Okay, problem solved (it was a boneheaded mistake on my end).
    >
    > - Mark
    >
     
    =?Utf-8?B?Q3JhaWc=?=, Feb 28, 2006
    #6
  7. Hi Mark

    Any change you could post the code to SimpleSiteSetting class?

    Thanks

    "Mark Olbert" wrote:

    > Okay, problem solved (it was a boneheaded mistake on my end).
    >
    > - Mark
    >
     
    =?Utf-8?B?Q3JhaWc=?=, Feb 28, 2006
    #7
  8. Mark Olbert

    Anderson Olympio

    Joined:
    Feb 23, 2011
    Messages:
    1
    Hi folk!

    Just substitute SimpleSiteSetting by SiteElement and it works!

    Kind Regards,
    Anderson
     
    Anderson Olympio, Feb 23, 2011
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Perry Jr
    Replies:
    2
    Views:
    4,288
    Russ Perry Jr
    Aug 20, 2004
  2. Chad E. Dollins
    Replies:
    3
    Views:
    682
    Kai-Uwe Bux
    Nov 8, 2005
  3. request@no_spam.com
    Replies:
    5
    Views:
    458
  4. Ultrus
    Replies:
    3
    Views:
    412
    Stefan Behnel
    Jul 9, 2007
  5. Replies:
    3
    Views:
    494
    Victor Bazarov
    Dec 7, 2007
Loading...

Share This Page