.Net app.config in the library project

I have a console and library project project (dll) in one solution. The library project has an app.config file, where I store the database connection string. The console application references this DLL.

When I compile the console application and deploy it to the library, I cannot access the app.config file, which belongs to the DLL, when I need to change the connection string after the application is deployed.

These are the files that I see, but not the .config file:

  • Library.dll
  • library.pdb
  • console.application
  • console.exe
  • console.exe.manifest
  • console.pdb
  • console.vshost.application
  • console.vshest.exe

Where am I wrong?

+4
source share
4 answers

By default, there is only 1 .config file for the launched application. This is the .config file associated with the exe that launched the program. You should probably copy the configuration values ​​from the DLL configuration file to the console application configuration file. If you really want to separate them, you cannot use the default ConfigurationSettings.AppSettings dictionary. See this question for more information.

+9
source

By default, each process will use its own configuration file. If you want the console application to have a configuration file, you need to add it to your project. After adding App.config to your project, whenever your project is created, App.config will be copied to the output folder as <application> .exe.config, where <application> is your application name (e.g. ConsoleApplication1.exe.config ) (Web.config is more complicated.)

Typically, configuration is added to this application configuration file.

Thus, the easiest way to configure the library assembly is to add its specific configuration to the configuration file of the hosting application.

Now it can be a little ugly. One way to make it less ugly is to have an application configuration file that simply references your configuration file using the ConfigSource attribute. Thus, you can expand your assembly with your configuration file and just have a hosting application add a few lines to your configuration file to reference your config. First they must add a link to configSections:

<configSections> <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </configSections> 

Then they need to add a link to your configuration file:

 <loggingConfiguration configSource="MyLoggingConfig.xml"/> 

Now, perhaps you don’t want the users of your assembly to even know that there is a configuration file. If so, you can create your own stand-alone configuration file and open it using ConfigurationManager.OpenMappedExeConfiguration . Here is another example on how to use OpenMappedExeConfiguration.

+2
source

Are you app.config an app.config file for exe (and not just for your dll)? You must , and make sure that you copy all the settings that you have in the dll configuration into the exe configuration.

Or you can use Add As Link to link app.config with your exe .

+1
source

Check the "Copy to output directory" property for your configuration file.

-2
source

All Articles