54

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?

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
Billkamm
  • 1,064
  • 1
  • 9
  • 22
  • Possible duplicate of [create your own settings in xml](http://stackoverflow.com/questions/396139/create-your-own-settings-in-xml) – Lex Li Oct 31 '16 at 09:44

9 Answers9

15

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

casperOne
  • 73,706
  • 19
  • 184
  • 253
  • 2
    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
  • 1
    @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
5

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.

Community
  • 1
  • 1
AaronS
  • 7,649
  • 5
  • 30
  • 56
3

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!

Joseph Korinek
  • 521
  • 4
  • 2
  • 6
    The whole point of using a class library is for the express purpose of *not* having to the host app developer have to know about all these things. "Now the configuration is managed at the owner level" is precisely what we're trying to avoid. – as9876 Nov 30 '15 at 22:14
3

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.

Ian Nelson
  • 57,123
  • 20
  • 76
  • 103
2

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

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

Parthiban
  • 41
  • 2
  • How are you going to change the value without recompiling the project? I have changed the value in the config file, but the calling application does not get the new value of the config file but the old one when calling the library – Code Pope Aug 14 '18 at 16:23
0

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.

MPO
  • 91
  • 1
  • 1
0

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.

0

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

habakuk
  • 2,712
  • 2
  • 28
  • 47
0

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.

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245