Tara
Tara

Reputation: 397

How do I add custom ConfigurationSection to Assembly?

I've spent a few weeks trying to figure this out, this is a duplicate of a question I asked previously but did not get a response to, so I am refining the question here.

I've created a custom class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Configuration;

namespace mssql_gui
{
    public class TestConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
        public TestConfigInstanceCollection Instances
        {
            get { return (TestConfigInstanceCollection)this[""]; }
            set { this[""] = value; }
        }
    }

    public class TestConfigInstanceCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new TestConfigInstanceElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((TestConfigInstanceElement)element).Key;
        }
    }

    public class TestConfigInstanceElement : ConfigurationElement
    {
        [ConfigurationProperty("key", IsKey = true, IsRequired = true)]
        public string Key
        {
            get { return (string)base["key"]; }
            set { base["key"] = value; }
        }
        [ConfigurationProperty("value", IsRequired = true)]
        public string Value
        {
            get { return (string)base["value"]; }
            set { base["value"] = value; }
        }
    }
}

I've implemented it:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="testSection" type="mssql_gui.TestConfigSection"/>
  </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
    </startup>
  <appSettings>
    <add key="Data Source" value="localhost\SQLEXPRESS"/>
    <add key="Initial Catalog" value="(empty)"/>
    <add key="Integrated Security" value="SSPI"/>
  </appSettings>
  <testSection>
    <add key ="testKey" value="tesValue"/>
  </testSection>
</configuration>

and I have tried to access it, I am getting:

An error occurred creating the configuration section handler for testSection: Could not load type 'mssql_gui.TestConfigSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

I understand that in the type, I should be declare an assembly dll, but I'm confused about that...because in the official instructions by MS, it says to create a new class for the handler:

  1. Create a public class that inherits from the System.Configuration.ConfigurationSection class.

  2. Add code to define the section's attributes and elements.

Adding the class (at least through the visual studio interface) creates a .cs file, not a .dll assembly file, so how to I add that custom class to an assembly file in order to reference it in the <configSections> part of app.config?

Upvotes: 3

Views: 3276

Answers (2)

mwilczynski
mwilczynski

Reputation: 3082

If I understand correctly, you have problem with resolving what actually your Assembly is, since you are only creating .cs files that determine types that this file hold.

Assembly (in maybe not so accurate shorcut) is just the project you have in your solution. It will get compiled into its seperate assembly - the .dll you mentioned - later on. When you add class to any .cs file in given project, on compile it will be included in project's assembly.

By default, if you won't provide assembly for configSection where its corresponding type should be found, App.config defaults to System.Configuration assembly - that's where you get your error from, since you've declared your section in your own assembly (== project).

Right click in Visual Studio on your project that holds App.config file and choose Properties to check its Assembly name:

enter image description here

Then add this name to your App.config section declaration. In my example its ConsoleApp1, so I will add it to configuration accordingly:

<configSections>
    <section name="testSection" type="mssql_gui.TestConfigSection, ConsoleApp1"/>
</configSections>

Upvotes: 5

codeteq
codeteq

Reputation: 1532

Ensure that the type attribute of the section element matches the manifest of the assembly (ensure that you specify both the correct namespace and type name).

You need to add the name of the assembly (where the type relies) to the type attribute:

You'll get the name of the assembly from the AssemblyInfo.cs within the project where TestConfigSection class is defined.

 <section name="testSection" type="mssql_gui.TestConfigSection, ASSEMBLYNAME"/>

Example asuming your assembly names mssql_gui

 <section name="testSection" type="mssql_gui.TestConfigSection, mssql_gui"/>

You read it like this:

 Configuration config =
 ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 TestConfigSection mySec = (TestConfigSection)config.Sections["testSection"];

See more details at MSDN

How to: Create Custom Configuration Sections Using ConfigurationSection

Upvotes: 4

Related Questions