开发者

Override config settings

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 t开发者_运维技巧he 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 


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).


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>


The elements are changed from the child and what i mean by that is currently your app.config is the parent file and the values are replaced by the ones existing in General.config

Since you are using remove in the parent file what its effectively doing is removing the element you specify in app.config but after that the elements from general.config are pushed in. Now say here in General.config you say remove mykey3 which is on your app.config you will see that the final collection has no key as mykey3.

In short this is not going to work. Hope this helped you.


You can add another config file say Test.config.

<appSettings>
   <add key="mykey1" value="New value"/>
</appSettings>

and in the app.config appsettings section will look like this

<appSettings file="Test.config">
   <add key="mykey1" value="myvalue1"/>
</appSettings>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜