I want to add a custom configuration section to my App.Config file in my C# 2010 project. I want the section to look like the following.

<DmModules>
      <Module Name="Module1">
            <add key="DbType" value="...">
            <add key="ConnType" value="...">
      </Module>
      <Module Name="Module2">
            <add key="DbType" value="...">
            <add key="ConnType" value="...">
      </Module>
</DmModules>

Of course there can be as many <Module> elements as needed, and each <Module> must contain two keys as specified.

Note : DbType and ConnType doesn't necessarily have to be "key", but even something like the following would do as long as I can read that value later from another part of my program.

<Module Name="Module1">
      <DbType Name="..."/>
      <ConnType Name="..."/>
</Module>

So as said in the above note, I need to be able to read these values later from inside my program and use the values. How can it be done?

Dani AI

Generated

A few clarifications that fix the two issues seen in this thread (null from GetSection, then the “Could not read the type … from the assembly System.Configuration” exception) and a minimal, practical approach to model repeatable <Module> elements.

First, class‑library App.config files are not used at runtime — the CLR reads the host application’s config (the running .exe.config). That is why the DLL’s App.config returned nothing when called from the console app; the section must live in the executable’s config. (learn.microsoft.com)
As discovered, copying the settings into the calling app’s config is required; ’s pointer to MSDN/How‑to examples was on the right track.

Second, the exception is caused by how the config system resolves the type name. The section declaration’s type must be assembly‑qualified (namespace + type, then the assembly name). A common fix is to change the section declaration from a bare type to an assembly-qualified form, for example:

<configSections>
  <sectionGroup name="pageAppearanceGroup">
    <section name="pageAppearance"
             type="DB_APPROACH_3.PageAppearanceSection, DB_APPROACH_3"
             allowLocation="true" allowDefinition="Everywhere" />
  </sectionGroup>
</configSections>

(The assembly name after the comma must match the assembly that contains the PageAppearanceSection class.) (stackoverflow.com)

To represent multiple <Module> elements, implement a ConfigurationSection containing a ConfigurationElementCollection of Module elements. A compact skeleton:

public class ModuleElement : ConfigurationElement {
  [ConfigurationProperty("name", IsKey=true, IsRequired=true)]
  public string Name => (string)this["name"];
  [ConfigurationProperty("DbType")] public string DbType => (string)this["DbType"];
  [ConfigurationProperty("ConnType")] public string ConnType => (string)this["ConnType"];
}

public class ModuleCollection : ConfigurationElementCollection { /* override CreateNewElement/GetElementKey */ }

public class DmModulesSection : ConfigurationSection {
  [ConfigurationProperty("modules")]
  [ConfigurationCollection(typeof(ModuleCollection), AddItemName="Module")]
  public ModuleCollection Modules => (ModuleCollection)base["modules"];
}

The framework docs show this pattern and attributes in detail. (learn.microsoft.com)

Quick troubleshooting checklist: confirm the host app has an appname.exe.config in the output folder (App.config is renamed on build); check the exact assembly name used in the section’s type; ensure System.Configuration is referenced; and print the active config path with ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath to verify the file being read. (stackoverflow.com)

This addresses the core causes seen by and gives a minimal path to a strongly‑typed DmModules section that will deserialize multiple Module entries.

Recommended Answers

All 12 Replies

I tired the first, but it gives an error as follows.

Could not read the type DB_APPROACH_3.PageAppearanceSection from the assembly System.Configuration, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.

Note : DB_APPROACH_3 is my namespace, and according to the example DB_APPROACH_3 = Samples.AspNet

Any ideas?

I should have said "Exception", not error.

And the exception is given at the first line of code when I try to read it, which is the following line.

DB_APPROACH_3.PageAppearanceSection config = (DB_APPROACH_3.PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection("pageAppearanceGroup/pageAppearance");

No Idea let me check have you checked the second link too?

is there exists the section which you are trying to access in this code where exception raised?

is there exists the section which you are trying to access in this code where exception raised?

Sorry I did not understand your question. Can you please elaborate?

I want to ask in this code

DB_APPROACH_3.PageAppearanceSection config = (DB_APPROACH_3.PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection("pageAppearanceGroup/pageAppearance");

you are getting the section "pageAppearanceGroup/pageAppearance" have you checked it in you file is it present there?

I want to ask in this code

DB_APPROACH_3.PageAppearanceSection config = (DB_APPROACH_3.PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection("pageAppearanceGroup/pageAppearance");

you are getting the section "pageAppearanceGroup/pageAppearance" have you checked it in you file is it present there?

First of all, I made a silly mistake, because I put that line of code which generate the exception in a different project. I rectified it, now I do not get the exception.
However, now, the config object of type DB_APPROACH_3.PageAppearanceSection does not receive any value after that line of code is executed. It remains "null".

To answer your question, yes that line IS there. I just copied and pasted (contents of App.Config file) from Link1 you provided and changed appropriately. This is how my App.Config looks like now.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<!-- Configuration section-handler declaration area. -->
	<configSections>
		<sectionGroup name="pageAppearanceGroup">
			<section
			  name="pageAppearance"
			  type="DB_APPROACH_3.PageAppearanceSection"
			  allowLocation="true"
			  allowDefinition="Everywhere"
			  />
		</sectionGroup>
		<!-- Other <section> and <sectionGroup> elements. -->
	</configSections>

	<!-- Configuration section settings area. -->

	<!-- Configuration section-handler declaration area. -->

	<!-- Configuration section settings area. -->
	<pageAppearanceGroup>
		<pageAppearance remoteOnly="true">
			<font name="TimesNewRoman" size="18"/>
			<color background="000000" foreground="FFFFFF"/>
		</pageAppearance>
	</pageAppearanceGroup>

	<!-- Other configuration settings, such as system.web -->
</configuration>

Have you checked the second link provided by abellazm? it has a more detailed example with source code

Have you checked the second link provided by abellazm? it has a more detailed example with source code

Yes, but I am kind of new to this thing and is finding it hard to wrap my brains around that articles. I'm trying slowly to understand it but no luck so far.

what's the exception you are facing now?

commented: Great helper! +26

what's the exception you are facing now?

Okay, first I understood one thing which is pretty weird.

Let us say I have a Windows Console Application, called ConfigTestApp.
I have a DLL named CustomConfig, in which I implement all the code above mentioned, and it also contain the App.Config file.
I also have a static method inside a class in CustomConfig which merely returns an object of type PageAppearanceSection as follows.

public static PageAppearanceSection GetValue()
{
         PageAppearanceSection config =
         (CustomConfig.PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection(
        "pageAppearanceGroup/pageAppearance");

         return config
}

Then I call the above GetValue() method FROM ConfigTestApp.

But it returns NULL, because for some weird reason Windows access the AppConfig of whoever runs the DLL, not the AppConfig of the DLL. Since ConfigTestApp does not have an AppConfig, it returns null.

So I copied the same AppConfig to ConfigTestApp, and now it gives the same Exception I mentioned above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.