开发者

.NET .config HELL

I have the following projects:

  1. MVC
  2. Console application
  3. Class library
  4. Windows forms application
  5. COM Library

All these applications need to 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 the web projects.

The same configuration need to be accessible in all of these projects. I have read that it's suggested to use machine configuration file, but we won't always have access to that, therefore configuration files must reside within our solution.

I don't fully understand how the configuration files get build. Currently I wrote a simple project where I have the following:

  1. Class library to store for serving configuration files. A have attempted to do this through reflection.
  2. Windows application that should read the app.config from a 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;    

What I get instead is an empty application settings file.

Class library has the fo开发者_如何学JAVAllowing App.config:

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

I have tried using .GetExecutingAssembly() method which I expect to return an assembly of a code that's currently being executed. This didn't work, instead it has returned the assembly of a Windows application.

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

I have a feeling that either I'm doing something fundamentally wrong or Microsoft hasn't made this flexible enough. I have also tried to search MSDN for explanation, but this hasn't been documented well IMO.

I have also left COM in bold because I'm not sure whether any config files would be available to COM library at all. Firstly I would like to get other projects to work.

I understand that this is a lot of information. Any help would be greately appreciated. Previously we have chosen to use registry, but this has turned out to be nasty, mainly because access to registry is not available in some scenarios. Additionally we now have multiple versions of the applications and switching between branches is a half an hour job :(

Thank you

Edit:

If I add the dll's config sections to app.config that means that these settings will be available only from that application. Please correct me if I'm wrong. Example that I have provided is a scaled down version. In total there are about ten windows applications, a single MVC project and range of class libraries all of which need to make a use of that configuration.

Configuration settings are mostly connection strings, lookup values that do not belong in the database and few other minor settings. Main concern at this point are the connection strings. There are few minor releases of the application where each release points to a different database.

What I'd like to get out of this is a good workable solution so that it can be posted online and other people who come across the same problem won't spend days of their time.

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

Write simple XML serializer to read/write config and DLL for serving the configuration.

COM objects are a long story and were implemented with a "hack", since neither App.config or Web.config are available in COM DLLs.


Note ConfigurationManager.OpenExeConfiguration needs to be passed the filename of the config file, not the executable.

You'll need to append .config to the path of the executable. To get the exe assembly use Assembly.GetEntryAssembly.

If you have configuration settings you want to share across multiple pieces of code that are not all in the same .NET Process, I would suggest:

  • Put them in their own myStuff.config.
  • In .NET code use ConfigurationManager.OpenExeConfiguration to open and access myStuff.config.
  • Non-.NET code will need to use an XML parser to load and read the settings. Unless you configuration structures are very complex this shouldn't be too hard to encapsulate.
  • Put the path to myStuff.config in the app.config of each application sharing this configuration for .NET applications. (Not non-.NET applications: depends on what works for that application.)

Another approach, where the configuration structure is the same but the settings are per-application would be a custom configuration section.


A couple of general points -- add the dll's config sections to the app.config file rather than relying on the dll's config file to get picked up; and app.config actually gets renamed to .exe.config on build, so you need to make sure a file of that name's available.

Also -- you're not constrained to using the default config loading mechanism to configure your application. You could create your own configuration classes and just use XML serialization to deserialize and configure at will.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜