none
Custom config section in app.config - IsDefaultCollection = true RRS feed

  • Question

  • I'm busy writing my own custom config section and I'm having difficulty understanding what the IsDefaultCollection property of the ConfigurationProperty attribute does. Can anyone please explain to me how this property works?

    I've got the following structure that I'd like to implement in my config file.
    <MyConfigSection>
    <Assembly assemblyFullName="SomeAssembly">
    <Method name="method1">
    <Parameters>
    <add seqNr="0" type="System.String" value="blah" />
    <add seqNr="1" type="System.Int32" value="32" />
    </Parameters>
    </Method>
    <Method name="method2">
    <Parameters>
    <add seqNr="0" type="System.String" value="blah" />
    </Parameters>
    </Method>
    </Assembly>
    <Assembly assemblyFullName="SomeOtherAssembly">
    <Method name="method">
    <Parameters>
    <add seqNr="0" type="System.String" value="blah" />
    <add seqNr="1" type="System.Int32" value="32" />
    </Parameters>
    </Method>
    <Method name="method1">
    <Parameters>
    <add seqNr="0" type="System.String" value="blah" />
    </Parameters>
    </Method>
    </Assembly>
    </MyConfigSection>

    Now I created a ParameterElement : ConfigurationElement class, and as I understand it, I need to create a ParameterElementCollection class as well. This is what I fully understand so far.

    Now the Assembly tag may contain more than 1 Method sub-tag. Do I need to create a MethodElement class and MethodElementCollection class as well? When should I set the IsDefaultCollection property of the ConfigurationPropertyAttribute class to true, and when should I set it to false?
    Tuesday, January 13, 2009 10:16 AM

All replies

  • No replies at all, that's sad. I'm having a similar problem. Did you find a solution elsewhere ?

    Tuesday, August 25, 2009 10:10 AM
  • Since this comes up pretty high on Google for this search, I thought I'd finally fill in an answer.  I found a good example on this URL:  http://olondono.blogspot.com/2008/01/advanced-net-configuration-techniques.html .  Posting an excerpt below.  I tried this several different ways myself and it never really occurred to me (nor is there any documentation on this) to use an empty string for the property name as shown below.

    =====================

    If we don't want to use the named collection property ("users" in this example), we need to change the Users property to include the IsDefaultCollection property and set the property's name to an empty string.  [Note the empty string in both the Attribute and inside the get.]

    [ConfigurationProperty("", IsRequired=false, IsDefaultCollection=true)]
    public myUsersCollection Users
    {
      get { return (myUsersCollection) this[""]; }
    }
    

    This will produce the following result:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
     <configSections>
      <section name="myConfiguration" type="WindowsFormsApplication1.myConfigurationSection, WindowsFormsApplication3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
     
    ...
     
     </configSections>
     <myConfiguration>
      <add name="john" pwd="pass" />
      <add name="mary" pwd="pass" />
     </myConfiguration>
     
    
    • Proposed as answer by David Storfer Friday, August 20, 2010 6:54 PM
    Friday, August 20, 2010 6:54 PM
  • Thanks David. That was exactly what I was looking for. This, as expected, works for multiple nested collections as well for implementing something like this...

     <listenerConfig>
       <listener queue="myQueue">
         <subscriber address="me@me.com" />
         <subscriber address="you@you.com" />
       </listener>
       <listener queue="yourQueue">
         <subscriber address="me@me.comm" />
         <subscriber address="you@you.com" />
       </listener>
     </listenerConfig>
    

    The listeners collection is the default collection on the root config section and the subscriber as the default collection on the listener element. Nice to get rid of the verbose <listeners></listeners> and <subscribers></subscribers> tags I had to add before.

    Friday, October 1, 2010 4:22 PM
  • Could a moderator please move this thread to an appropriate forum, perhaps the BCL forum?

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects

    Monday, June 11, 2012 8:08 PM
  • Could a moderator please move this thread to an appropriate forum, perhaps the BCL forum?

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects

    What is the BCL forum?

    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer

    Jeff Wharton
    MSysDev (C.Sturt), MDbDsgnMgt (C.Sturt), MCT, MCPD, MCSD, MCITP, MCDBA
    Blog: Mr. Wharty's Ramblings
    Twitter: @Mr_Wharty
    MC ID: Microsoft Transcript

    Tuesday, June 12, 2012 12:29 AM
  • I meant the .NET Base Class Library forum.

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects

    Tuesday, June 12, 2012 12:38 AM