2

I have this in my app.config file:

<Configuration>
 <configsections>
  <section name="FeaturesSection" type="SampleCatalog.FeaturesSection" />
 </configsections>
 <FeaturesSection>
   <Feature Name="CCH" US="true" EM="false" Sequence="1" />
   <Feature Name="PLT" US="true" EM="false" Sequence="1" />
   <Feature Name="PD" US="true" EM="false" Sequence="1" />
 </FeaturesSection>
<Configuration>

My code in the class goes as below:

public class FeaturesSection:ConfigurationSection
{
 public FeatureCollection Features
 {
    get{return (FeatureCollection)base["Features"};
 }
}

public class FeatureCollection:ConfigurationElementCollection
{
   public Feature this[int index]{
     get{ return (Feature)BaseGet(index);}
     set{
        if(BaseGet(index)!= null)
          BaseRemoveAt(index);
        BaseAdd(index,value);
        }
    }

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

    protected override object GetElementKey(ConfigurationElement          element){
      return ((Feature)element);
    }
}

public class Feature: ConfigurationElement
{
   [ConfigurationProperty("Name",IsRequired=true)]
   public string Name {get; set;}

   [ConfigurationProperty("US",IsRequired=true)]
   public bool US {get; set;}

   [ConfigurationProperty("EM",IsRequired=true)]
   public bool EM {get; set;}

   [ConfigurationProperty("Sequence",IsRequired=true)]
   public string Sequence {get; set;}
  }

Now when I run this code:

var mysection = (FeaturesSection)ConfigurationManager.GetSection("FeaturesSection");

I'm getting exceptions.

An error occurred creating the configuration section handler for FeaturesSection: Could not load type 'SampleCatalog.FeaturesSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (C:\Users\Venkata_Poruri_Pavan\Documents\Visual Studio 2013\Projects\SampleCatalog\SampleCatalog\bin\Debug\SampleCatalog.vshost.exe.Con‌​fig line 4)

Please help, kindly accept my apologies as I couldn't paste the code here.

Thanks

7
  • 2
    WHAT exceptions do you get? Please post the full and exact error message(s) you get! Commented Jan 6, 2016 at 21:51
  • Couldn't load type SampleCatalog.FeaturesSection Commented Jan 6, 2016 at 22:53
  • Am I missing out on anything? Commented Jan 6, 2016 at 23:21
  • Do you have namespace SampleCatalog.FeaturesSection? Commented Jan 7, 2016 at 0:31
  • SampleCatalog is the namespace and the FeaturesSection is the class within the namespace. Commented Jan 7, 2016 at 4:05

1 Answer 1

1

If you have a config section type in your own assembly, you need to define that assembly in the <configSection> - try using this:

<configsections>
   <section name="FeaturesSection" 
            type="SampleCatalog.FeaturesSection, SampleCatalog" />
</configsections>

You need to specify the type= as fully-qualified class name, and then after a comma, define the assembly where that type is stored in. If you omit it (as you did in your post), .NET will check in the System.Configuration assembly - but of course, it won't find your custom class there!

Update: OK your code and config need a few little tweaks:

On the FeaturesSection, you need to add a ConfigurationProperty attribute to define under what name that collection of entries will be stored - something like this:

[ConfigurationProperty("Features")]
public class FeaturesSection : ConfigurationSection
{
    public FeatureCollection Features
    {
        get{return (FeatureCollection)base["Features"};
    }
}

On your FeatureCollection class, you need to define (with an attribute) what **type* of elements that collection will contain, and what to call the individual elements inside the collection:

[ConfigurationCollection(typeof(Feature), AddItemName = "Feature")]
public class FeatureCollection : ConfigurationElementCollection
{
    public Feature this[int index]
    {
        get { return (Feature)BaseGet(index); }
        set 
        {
            if(BaseGet(index) !=  null)
            {
                BaseRemoveAt(index);
            }

            BaseAdd(index,value);
        }
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((Feature)element);
    }
}

And then your config needs to look like this:

<Configuration>
    <configSections>
        <!-- add the ASSEMBLY after the fully-qualified class name -->
        <section name="FeaturesSection" 
                 type="SampleCatalog.FeaturesSection, SampleCatalog" />
    </configSections>
    <FeaturesSection>
        <!-- this is the name defined on the FeaturesSection -->         
        <Features>
            <Feature Name="CCH" US="true" EM="false" Sequence="1" />
            <Feature Name="PLT" US="true" EM="false" Sequence="1" />
            <Feature Name="PD" US="true" EM="false" Sequence="1" />
        </Features>              
    </FeaturesSection>
<Configuration>

With this setup, you should be able to properly read out your custom config section.

Sign up to request clarification or add additional context in comments.

8 Comments

ConfigurationErrorException: Unrecognized Element 'Feature'
@user3581188: updated my response with some code changes and a change in structure of the config section; I tested this on .NET 4.5 and it works for me
Error 18 Attribute 'ConfigurationProperty' is not valid on this declaration type. It is only valid on 'property, indexer' declarations.
@user3581188: where did you add the ConfgurationProperty attribute? My code as posted works (I testet it "live")
[ConfigurationProperty("Features")] public class FeaturesSection : ConfigurationSection { public FeatureCollection Features { get { return (FeatureCollection) base["Features"]; } } }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.