Using app.config with a class library

.NetConfiguration

.Net Problem Overview


Frequently I need to create a .Net class library that requires an app.config for things such as database connection strings. However, these settings must be in the calling application's app.config or web.config. If I want to distribute the DLL across multiple applications it becomes a pain because I have to copy these settings into each the application's app.config.

I have considered manually reading the config settings via code from inside the class library, but that is also a major pain. Does anyone have any suggestions for the best way to load app.config settings inside a class library?

.Net Solutions


Solution 1 - .Net

I know it's not what you want to hear, but the entry point's config file is the logical place to put these setttings. If every library required its own config file then it would get tedious to have to edit every single file instead of just one.

I would have a text file with the default settings included as part of your library (and as part of the project) which can be distributed with the library for the default settings configuration (it can be copied and pasted into the config file).

Solution 2 - .Net

One thing you could do is to have a seperate settings file just for your library, then you only have to have a reference to it in your entry apps config file.

See the accepted answer in this question for information on this.

Solution 3 - .Net

Have each class library define configuration settings in a custom ConfigurationSection.

Then add custom section handlers to your process.exe.config file.

This MSDN article is pretty comprehensive in its explanation, with examples in both VB and C#.

It involves writing some pretty repetitive code - Dmitryr has created this tool to generate the config sections from a fragment of a XML configuration, which might help.

Solution 4 - .Net

Here is an alternative.

Instead of attempting to find ways to "merge" your app.configs, or even placing configuration items in your DLL (and trying to work around what is "by design") in the first place, try this:

Create your DLL, but where you need a Config Item, create a public Property. When you instantiate the class in the DLL, you populate these properties with config items from the owning application.

Now the configuration is managed at the owner level, and the DLL is portable, as you have to pass what you need to it at run time.

No more DLL.config!

Solution 5 - .Net

Go to App properties -> Settings tab; add your settings there.

Use Properties.Settings.Default.<SettingName> to access the setting. That's it!

Solution 6 - .Net

It is no (big) problem to use its own config file for every class library. If you use vb.net you can add values via the "my project" panel on the "Settings" page.

The only thing you have to do is to call "My.Settings.Save" on your class library settings (and not only on your main application settings file).

That works with c#, too (But probably needs more manual work).

Solution 7 - .Net

If you're using NUnit, name your app.config file with the same name as your *.nunit project filename. So for example, if you called your project "ClassLibraryA.nunit", then name your class library configuration file "ClassLibraryA.config". They also need to reside in the same folder/directory. NUnit is actually using this as the main configuration file.

If you have custom code, then you need to make sure your app.config (or web.config) is referencing the class library's app.config file. The application only cares about the root app.config or web.config. So you need to add a custom configuration section that points to the class library app.config.

Solution 8 - .Net

You could create your own XML file(call it appConfig.xml or something if you want), use System.Xml instead of System.Configuration to read it, and include it alongside your dll.

Solution 9 - .Net

Create your class library in a different solution. If you keep them in the same solution, the app.config at the highest hierarchy will get read instead.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBillkammView Question on Stackoverflow
Solution 1 - .NetcasperOneView Answer on Stackoverflow
Solution 2 - .NetAaronSView Answer on Stackoverflow
Solution 3 - .NetIan NelsonView Answer on Stackoverflow
Solution 4 - .NetJoseph KorinekView Answer on Stackoverflow
Solution 5 - .NetParthibanView Answer on Stackoverflow
Solution 6 - .NethabakukView Answer on Stackoverflow
Solution 7 - .NetMacGyverView Answer on Stackoverflow
Solution 8 - .NetMPOView Answer on Stackoverflow
Solution 9 - .Netuser6490459View Answer on Stackoverflow