tags:

views:

14212

answers:

9
+16  Q: 

C# Dll config file

Hi

Im trying to add a app.config file to my dll, but all attempts have failed.

According to MusicGenesis in 'Putting configuration information in a DLL' this should not be a problem. So obviously im doing something wrong...

The following code should return my connectionstring from my dll :

return ConfigurationManager.AppSettings["ConnectionString"];

However, when I copy the app.config file to my console application, it works fine.

Any ideas?

+1  A: 

When using ConfigurationManager, I'm pretty sure it is loading the process/AppDomain configuration file (app.config / web.config). If you want to load a specific config file, you'll have to specifically ask for that file by name...

You could try:

var config = ConfigurationManager.OpenExeConfiguration("foo.dll.config");
config.ConnectionStrings. [etc]
Marc Gravell
According to the referred post: if the dll's name was MyDll.dll, then the config file should be MyDLL.dll.config. So if you read the config settings from within the dll, it should refer to its own config right?
MegaByte
No... I don't think so. "from with the dll" makes no odds; by default, it is looking at the config file defined for the AppDomain: my.exe.config
Marc Gravell
In particular, the AppDomain.CurrentDomain.SetupInformation.ConfigurationFile setting.
Marc Gravell
note : I tried the OpenExeConfiguration, and I'm not sure that works, either. Perhaps just merge the config with the app.config?
Marc Gravell
It _can_ be done... but not with the same kind of support and safety as the app.config file for an EXE. See my answer.
Chris Ammerman
+4  A: 

ConfigurationManager.AppSettings returns the settings defined for the application, not for the specific DLL, you can access them but it's the application settings that will be returned.

If you're using you dll from another application then the ConnectionString shall be in the app.settings of the application.

Jorge Córdoba
+1 Nicely answered.
Adrian Godong
+1  A: 

As Marc says, this is not possible (although Visual Studio allows you to add an application configuration file in a class library project).

You might want to check out the AssemblySettings class which seems to make assembly config files possible.

Gerrie Schenck
+28  A: 

It is not trivial to create a .NET configuration file for a .DLL, and for good reason. The .NET configuration mechanism has a lot of features built into it to facilitate easy upgrading/updating of the app, and to protect installed apps from trampling each others configuration files.

There is a big difference between how a DLL is used and how an application is used. You are unlikely to have multiple copies of an application installed on the same machine for the same user. But you may very well have 100 different apps or libraries all making use of some .NET DLL.

Whereas there is rarely a need to track settings separately for different copies of an app within one user profile, it's very unlikely that you would want all of the different usages of a DLL to share configuration with each other. For this reason, when you retrieve a Configuration object using the "normal" method, the object you get back is tied to the configuration of the App Domain you are executing in, rather than the particular assembly.

The App Domain is bound to the root assembly which loaded the assembly which your code is actually in. In most cases this will be the assembly of your main .EXE, which is what loaded up the .DLL. It is possible to spin up other app domains within an application, but you must explicitly provide information on what the root assembly of that app domain is.

Because of all this, the procedure for creating a library-specific config file is not so convenient. It is the same process you would use for creating an arbitrary portable config file not tied to any particular assembly, but for which you want to make use of .NET's XML schema, config section and config element mechanisms, etc. This entails creating an ExeConfigurationFileMap object, loading in the data to identify where the config file will be stored, and then calling ConfigurationManager.OpenMappedExeConfiguration to open it up into a new Configuration instance. This will cut you off from the version protection offered by the automatic path generation mechanism.

Statistically speaking, you're probably using this library in an in-house setting, and it's unlikely you'll have multiple apps making use of it within any one machine/user. But if not, there is something you should keep in mind. If you you use a single global config file for your DLL, regardless of the app that is referencing it, you need to worry about access conflicts. If two apps referencing your library happen to be running at the same time, each with their own Configuration object open, then when one saves changes, it will cause an exception next time you try to retrieve or save data in the other app.

The safest and simplest way of getting around this is to require that the assembly which is loading your DLL also provide some information about itself, or to detect it by examining the App Domain of the referencing assembly. Use this to create some sort of folder structure for keeping separate user config files for each app referencing your DLL.

If you are certain you want to have global settings for your DLL no matter where it is referenced, you'll need to determine your location for it rather than .NET figuring out an appropriate one automatically. You'll also need to be aggressive about managing access to the file. You'll need to cache as much as possible, keeping the Configuration instance around ONLY as long as it takes to load or to save, opening immediately before and disposing immediately after. And finally, you'll need a lock mechanism to protect the file while it's being edited by one of the apps that use the library.

Chris Ammerman
+1 Very informative, thank you. I had basically the same question.
Odrade
+1 great explanation! thanks
Ralph Willgoss
+1  A: 

I had the same problem, after struggling with it for some days I found a solution.

I wrote a post about it http://moebiusit.com/blog/pabloromano/2009/07/13/changing-dll-settings-without-re-compiling/

Hope it helps!

+1 Linking configs could prove useful
Pedro
+2  A: 

Seems like this config files are really confusing to clarify as their behaviour changes from the dev environment to deployment. Apparently a DLL can have its own config file, but once you copy and paste the dll (together with their config file) elsewhere, the whole thing stopped working. The only solution is to manually merge the app.config files into a single file, which will only be used by the exec. For e.g. myapp.exe will have a myapp.exe.config file that contains all settings for all dlls used by myapp.exe. I'm using VS 2008.

Kenny Liew

kenny
+6  A: 

if you want to read settings from the DLL's config file but not from the the root applications web.config or app.config use below code to read configuration in the dll.

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string dllConfigData = appConfig.AppSettings.Settings["dllConfigData"].Value;
Morbia
+1  A: 

I know this is late to the party, however I thought I would share the solution I use for DLL's.

I am more of the K.I.S.S. school of thinking, so when I have a .NET DLL that wants to store external data points that control how it works or where it goes, etc. I simply create a "config" class that has only public properties that store all the data points it needs and that I would like to be able to have controlled external to the DLL to prevent recompiling it to make the changes. Then I use .Net's XML Serializing to save and load the object representation of the class to a file.

There are a lot of ways then to handle reading it and accessing it, from a Singleton, a static utility class, to extension methods, etc. This depends on how your DLL is structured and what method will fit your DLL best.

Rodney Foley
A: 

Since the assembly resides in a temporary cache, you should combine the path to get the dll's config:

var appConfig = ConfigurationManager.OpenExeConfiguration( Path.Combine(Environment.CurrentDirectory, Assembly.GetExecutingAssembly().ManifestModule.Name));

Lin Song Yang