How do I add custom ConfigurationSection to Assembly?


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="localhostSQLEXPRESS"/>
<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:



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



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?


<configSections>





Possible duplicate of custom section in app.config throws error in c#
– codeteq
Jul 1 at 22:50




2 Answers
2



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


.cs


types



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.


.cs



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).


configSection


App.config


System.Configuration



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


App.config


Properties



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:


App.config


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





Thank you! the problem was that the assembly name was actually "mssql gui" (with a space), I had tried mssql_gui, because that was the namespace that was generated, so the problem really was just the space character. Thanks for showing me where to look for it!
– Tara Stahler
Jul 1 at 23:36



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





Thank you, yeah, I had tried mssql_gui, but it was generating a similar error...the problem that I was having was the assembly name itself had a space rather than an underscore, but maybe you can help me with something related to your answer. When I use your method, I get "CS0122 'ConfigurationElement.this[ConfigurationProperty]' is inaccessible due to its protection level"
– Tara Stahler
Jul 1 at 23:39






where exactly do you get the error? it means that the class/property you want to use is not accesible.
– codeteq
Jul 1 at 23:48





So the configurationSection class is still defined as above (in the original question). Inside the class in which I want to implement it, I have the following two statements: mssql_gui.TestConfigSection s = ConfigurationManager.GetSection("testSection") as mssql_gui.TestConfigSection; and txtCatalog.Text = s["testKey"].Value; the error is occuring in the second statement
– Tara Stahler
Jul 1 at 23:50






what you want is not possible, you will have to use something like (requires using System.Linq;): txtCatalog.Text = s.Instances.Cast<TestConfigInstanceElement>().FirstOrDefault(kv=>kv.Key == "testKey")?.Value; But, have a look on NameValueSectionHandler-Class too!
– codeteq
Jul 2 at 0:07





hey, that worked! Thanks!
– Tara Stahler
Jul 2 at 0:22






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?