Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple App.Config Files in .NET Class library project

I am creating one class library project. Now by default I have one App.Config file so that I am putting all environment specific data in that Config file.

Now based on the Environment (whether Dev / Test / Production), I am planning to have three App.Config files in VS 2010 such as

App.Dev.Config

App.Test.Config

App.Prod.Config

Wondering how would the application know which config file to use.

Anyone implemented this scenario. Any Code samples / Articles would be helpful.

Thanks

like image 402
Rita Avatar asked Dec 15 '10 19:12

Rita


People also ask

Can we have multiple app config files?

You cannot use multiple configuration files (i.e. one per library project) without coding.

Can we have multiple config file for single project?

Yes you can have two web. config files in application. There are situations where your application is divided in to modules and for every module you need separate configuration.

Can a class library have an app config file?

No, class libraries can hold setting files, but their values will be defined in the application configuration (web. config, app.

How many app config file in ASP NET application?

Application configuration files Executable–hosted app. These apps have two configuration files: a source configuration file, which is modified by the developer during development, and an output file that is distributed with the app.


3 Answers

The app will use the config file named YourExcecutable.exe.config which is by default the file App.config included in your (executable) project. Note, that .NET only loads one config file for the whole application. You cannot use multiple configuration files (i.e. one per library project) without coding.

  1. Option: You can use postbuild events and different solution configurations to copy one or another App.Config file to the output folder

  2. Option: You can use the ConfigurationManager Class to load an alternate config file by code.

like image 184
Jan Avatar answered Sep 18 '22 07:09

Jan


Now there is an even better solution: SlowCheetah - XML Transforms

like image 42
duedl0r Avatar answered Sep 20 '22 07:09

duedl0r


Loading a different application configuration file at run time can be done using mapped configuration file. You need to add reference to System.Configuration.dll in your project.

Set the value of Copy to Output Directory property of all the additional config files (e.g. App1.config, App2.config etc.) other than the default one (App.config) to Copy if newer. This way they will be available in the project output directory (\bin\debug) after the project has been built. Default value of this property is Do not copy.

enter image description here

Here is the code snippet on how to read configuration data from non-default config files:

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "App1.config"; // app1.config should be present in root directory from where application exe is kicked off

 // Get the mapped configuration file
 var config = ConfigurationManager.OpenMappedExeConfiguration( 
        configFileMap, ConfigurationUserLevel.None);

 //get the relevant section from the config object
AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");

//get key value pair
var keyValueConfigElement = section.Settings["appSettingsKey"];
var appSettingsValue = keyValueConfigElement.Value;

If you have multiple app configuration files then you can keep a setting in default App.config file with the help of which you can make a decision at run time about which additional config file to load e.g. App1.config

Note: Be aware that the code like ConfigurationManager.AppSettings["DeployEnv"] will still read the data from the default App.config file. This behavior can't be changed. Loading of default App.config file can't be prohibited. You have to use alternate means as shown above to read the data from non-default configuration files

like image 40
RBT Avatar answered Sep 19 '22 07:09

RBT