Take the 2-minute tour ×

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 to have to keep copying these settings in the application's app.config.

I have consider manually reading the config settings via code 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?

share|improve this question
add comment

7 Answers

up vote 2 down vote accepted

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.

share|improve this answer
add comment

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

share|improve this answer
This gets to be a royal PITA for something like a project which is for a web service reference where you may keep updating the reference and keep updating the associated app.config file. I disagree with your analysis - I say it makes more sense to keep the web service settings in the class library than whatever references it. –  Jez Mar 13 '13 at 16:49
@Jez Then you should present that as an answer. Disagreeing is fine, but make a case for it. =) That said, the problem is that you have to take manual steps in order to update this external configuration file whereas an app.config file would not require those steps. Also, if you have to switch endpoints (more of a concern in corporate environments/internal networks) in one application and not another, the general approach of having a separate config file for the library fails miserably because of the manual updates required to make those changes. –  casperOne Mar 13 '13 at 17:06
Well, what would be better in that circumstance is that the referencing configuration overrode the 'default' configuration given in the class library. What really sucks is that the configuration in the class library is completely ignored. I don't present it as an answer because it isn't an answer to the question. I wish I did know how to use app.config with a class library. –  Jez Mar 13 '13 at 17:08
add comment

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.

share|improve this answer
add comment

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!

share|improve this answer
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Hint: Call you config file after assebmly name

share|improve this answer
add comment

Your Answer


