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

T.S.
  • 18,195
  • 11
  • 58
  • 78
Pavan Kumar
  • 85
  • 11
  • 2
    ***WHAT*** exceptions do you get? Please post the full and exact error message(s) you get! – marc_s Jan 06 '16 at 21:51
  • Couldn't load type SampleCatalog.FeaturesSection – Pavan Kumar Jan 06 '16 at 22:53
  • Am I missing out on anything? – Pavan Kumar Jan 06 '16 at 23:21
  • Do you have namespace `SampleCatalog.FeaturesSection`? – T.S. Jan 07 '16 at 00:31
  • SampleCatalog is the namespace and the FeaturesSection is the class within the namespace. – Pavan Kumar Jan 07 '16 at 04:05
  • Full Message: 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.Config line 4) – Pavan Kumar Jan 07 '16 at 05:06
  • And there is nothing in front of `SampleCatalog`? – T.S. Jan 07 '16 at 05:39

1 Answers1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • ConfigurationErrorException: Unrecognized Element 'Feature' – Pavan Kumar Jan 07 '16 at 06:28
  • @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 – marc_s Jan 07 '16 at 07:22
  • Error 18 Attribute 'ConfigurationProperty' is not valid on this declaration type. It is only valid on 'property, indexer' declarations. – Pavan Kumar Jan 07 '16 at 10:24
  • @user3581188: where did you add the `ConfgurationProperty` attribute? My code as posted works (I testet it "live") – marc_s Jan 07 '16 at 10:44
  • [ConfigurationProperty("Features")] public class FeaturesSection : ConfigurationSection { public FeatureCollection Features { get { return (FeatureCollection) base["Features"]; } } } – Pavan Kumar Jan 07 '16 at 10:58
  • Now, I've tried by adding the ConfigurationProperty attribute to the public FeatureCollection Features property of the FeaturesSection class. But data from the section is still not being bound – Pavan Kumar Jan 07 '16 at 11:17
  • @user3581188: uploaded my code and test project to http://1drv.ms/1SBXXOG - go and get it - for me, this **works** .... – marc_s Jan 07 '16 at 11:21
  • Thank you very much marc, for your co-operation. Yup, Got it, I added the Configuration Property attribute to Features property in FeatureSection. And, made some type casting in the get set properties in the Feature class that inherits the ConfigurationElement. Below is the sample code for the each property in Feature Class. [ConfigurationProperty("Name", IsRequired = true)] public string Name { get { return (string) this["Name"]; } set { this["Name"] = value; } } – Pavan Kumar Jan 07 '16 at 11:28