.NET.config HELL

I have the following projects:

  • MVC
  • Console application
  • Class library
  • Windows Forms Application
  • COM library

All of these applications must use a single configuration file. As far as I understand, app.config files are for windows, console applications and class libraries when web.config are for web projects.

The same configuration should be available in all of these projects. I read that he suggested using the machine configuration file, but we will not always have access to it, so the configuration files should be inside our solution.

I do not quite understand how configuration files are created. I have currently written a simple project in which I have the following:

  • Class library for storing configuration files. And they tried to do it through reflection.
  • A Windows application that should read app.config from the class library.

When I execute the following code, I expect to get a configuration file with test values:

_applicationSettings = ConfigurationManager.OpenExeConfiguration( System.Reflection.Assembly.GetAssembly(typeof(WCSConfiguration)).Location ).AppSettings; 

Instead, I get an empty application settings file.

The class library has the following App.config file:

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="TestTextKey" value="TestTextValue"/> </appSettings> </configuration> 

I tried using the .GetExecutingAssembly() method, which I expect to return the assembly of code that is currently executing. This did not work; instead, it returned the build of the Windows application.

GetAssembly(type(WCSConfiguration)) returned the right assembly, however the configuration file was not in the bin / debug directory.

I have a feeling that either I'm doing something fundamentally wrong, or Microsoft has not made it flexible enough. I also tried to find the MSDN for an explanation, but this was not well documented by IMO.

I also left COM in bold because I'm not sure that any configuration files will be available for the COM library at all. Firstly, I would like other projects to work.

I understand that this is a lot of information. Any help would be greatly appreciated. Earlier, we decided to use the registry, but this turned out to be unpleasant, mainly because access to the registry is not available in some scenarios. In addition, now we have several versions of applications, and the transition between branches is half an hour :(

thanks

Edit:

If I add dll configuration sections to app.config, this means that these options will be available only from this application. Please correct me if I am wrong. The example I provided is a shortened version. In total there are about ten window applications, one MVC project and a set of class libraries, all of which should use this configuration.

Configuration parameters are mainly connection strings, search values ​​that are not included in the database, and several other minor settings. The main problem with this question is the connection strings. There are several minor releases of the application in which each release points to a different database.

What I would like to get from this is a good workable solution so that it can be published on the Internet, and other people who are faced with the same problem will not waste days of their time.

IMO story moral: Use both App.config and Web.config to store the location of your own configuration file.

Write a simple XML serializer for reading / writing configuration and a DLL for serving the configuration.

COM objects are a long history and were implemented using a β€œhack”, since neither App.config nor Web.config are available in the COM DLL.

+6
c # web-config configuration app-config
source share
2 answers

Note ConfigurationManager.OpenExeConfiguration must pass the name of the configuration file, not the executable.

You need to add .config to the path of the executable. To get the exe assembly, use Assembly.GetEntryAssembly .

If you have configuration options that you want to split into several pieces of code that are not all in the same .NET process, I would suggest:

  • Put them in your own myStuff.config .
  • In the .NET code, use ConfigurationManager.OpenExeConfiguration to open and access myStuff.config .
  • For non-.NET code, you will need to use an XML parser to load and read the settings. If the configuration structure is not very complex, it should not be too complicated for encapsulation.
  • Put the path to myStuff.config in the app.config of each application that uses this configuration for .NET applications. (Not .NET applications: Depends on what works for this application.)

Another approach is when the configuration structure is the same, but the settings for each application will be user-configurable.

+4
source share

A few common points - add the dll configuration sections to the app.config file, rather than relying on the dll configuration file to get it; and app.config is actually renamed to .exe.config for assembly, so you need to make sure that the file of this name is available.

In addition, you cannot use the default boot mechanism to configure your application. You can create your own configuration classes and simply use XML serialization to deserialize and customize as you wish.

+1
source share

All Articles