Override config settings

10,586

Solution 1

A friend of mine answered this question. From MSDN:

You can use the file attribute to specify a configuration file that provides additional settings or overrides the settings that are specified in the appSettings element. You can use the file attribute in source control team development scenarios, such as when a user wants to override the project settings that are specified in an application configuration file. Configuration files that are specified in a file attribute must have the appSettings element rather than configuration element as the root node.

So in this question, the settings in general.config overrides items in app.config, different from that I think(want) app.config items overrides items in general.config. Now I think I have to resolve this issue in C# code(it will inevitable looks ugly).

Solution 2

Your use of the file attribute to load common settings with an expectation that keys added directly to the <appSettings> element would override those common settings is understandable, but unfortunately that is not how it works.

Microsoft's intention was for the file attribute to load common settings that override the individual application's settings.

This is discussed in some detail in the Microsoft Documentation

To overcome this problem, we very occasionally declare base settings in the common file, and then appropriately named overrides in the application configuration. However this does require additional code which is a bit ugly. e.g.

var config = ConfigurationManager.AppSettings["MSG_QUEUE_PROVIDER_OVERRIDE"]
    ?? ConfigurationManager.AppSettings["MSG_QUEUE_PROVIDER"]
    ?? "ActiveMQ";

<appSettings file="common.config"> 
    <!-- Override the common values -->
    <add key="MSG_QUEUE_PROVIDER_OVERRIDE" value="RabbitMQ"/>
</appSettings>
Share:
10,586
Cheng Chen
Author by

Cheng Chen

500 Internal Server Error

Updated on July 20, 2022

Comments

  • Cheng Chen
    Cheng Chen almost 2 years

    I have a config file that is used in several projects, general.config, looks like:

    <?xml version="1.0" encoding="utf-8" ?>
    <appSettings>
       <add key="mykey1" value="myvalue1"/>    
       <add key="mykey2" value="myvalue2"/>
    </appSettings>
    

    In one of the projects, I need to override one of the two settings. So the app.config of this project looks like:

    <?xml version="1.0"?>
    <configuration>
      <appSettings file="general.config">
        <remove key="mykey1"/>
        <add key="mykey1" value="anothervalue"/>
        <add key="mykey3" value="myvalue3"/>
      </appSettings>  
    </configuration>
    

    But remove is not working here. How can I override mykey1 without breaking mykey2? add works in this case. I can get myvalue3 from ConfigurationManager.

    EDIT: general.config is copied to output folder automatically when compiling. Don't worry about the path issue. Currently I got:

    ConfigurationManager.AppSettings["mykey1"] 
         //I got "myvalue1", but I want "anothervalue" here
         //that is, this item is "overrided", just like virtual methods in C#
    ConfigurationManager.AppSettings["mykey2"] 
         //this setting will not be modified, currently it works fine
    ConfigurationManager.AppSettings["mykey3"]   //good 
    
  • Cheng Chen
    Cheng Chen almost 13 years
    Not true. general.config is not a complete config file, it's a common part(section) of a config file, shared by all of my config files.
  • Cheng Chen
    Cheng Chen almost 13 years
    But in this case I lost all other items in general.config, like mykey2.
  • Mahesh KP
    Mahesh KP almost 13 years
    @Danny Chen: its working correctly. it only replaces the value of property with key 'mykey1'. and all other values are same as that in the app.config file