As you may know, the new Configuration Settings functionality added in .Net 2.0 brought strong-typing to configuration values. So, rather than just having
<add key=”setting1″ value=”123″/>
we now can have a typed value
<setting name=”setting1″ serializeAs=”int”>
This has a lot of value from a programming perspective, because we do not have to deal with parsing strings and converting them, etc; the framework does it for us, and we also have compile-time checking.
Also, there is a simple UI for a Project – Properties – Settings, where we can specify the various settings which creates a new ApplicationSettings section with the values.
Unfortunately, all this has also caused a lot of confusion in the community, especially when there are multiple calling apps.
I, too, faced challenges with this to work and here’s my story…
In my scenario, I have a component in a Dll project with it’s application-level Properties. The component is consumed by both a Web Application project and a website project. At runtime, the caller needs to pass in the required values. The trick is to add the settings from your dll.config to the caller config.
For a web application or website project, copy the entire applicationSettings section from the dll.config.
Also, you also need to add a definition for an applicationSettings section, in the web.config, like so:
<sectionGroup name=”applicationSettings” type=”System.Configuration.ApplicationSettingsGroup, System, Version=184.108.40.206, Culture=neutral, PublicKeyToken=b77a5c561934e089″>
<section name=”foo.Properties.Settings” type=”System.Configuration.ClientSettingsSection, System, Version=220.127.116.11, Culture=neutral, PublicKeyToken=b77a5c561934e089″ requirePermission=”false” />
This should be added as a new node in the <configSections> node.
Now, the required values should be picked up in the dll code at runtime.
If you get a compile error stating that there is an unknown section in config file, ensure you have declared/defined the sectionGroup properly.
If things still don’t work, ensure (as in one of my previous posts) that the node name + setting name = the fully qualified name of the property of the settings class.
<setting name=”setting1” …>
would map to a property called setting1 in a (partial) class called Settings in the foo.Properties namespace, defined in a file settings.designer.cs.
Hope that helped clarify some of the mysteries around config settings