davidwhitney / System.Configuration.Abstractions

Injectable, mockable, extensible, configuration for .NET
MIT License
42 stars 7 forks source link

Mixed use of .NET and System.Configuration.Abstractions ConfigurationManager causes inconsistent behavior #10

Open gavynriebau opened 8 years ago

gavynriebau commented 8 years ago

When using a mix of the .NET System.Configuration.ConfigurationManager and System.Configuration.Abstractions.ConfigurationManager to retrieve and update appSettings, the returned results aren't consistent.

Here are the full steps to reproduce the problem:

Setup an App.config that contains:

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

Create a System.Configuration.Abstractions.ConfigurationManager instance and load the app setting:

IConfigurationManager config = new ConfigurationManager();
var firstValue = config.AppSettings["something"]; // This returns "A"

Now use a .NET System.Configuration.ConfigurationManager to update the value.

var testConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
testConfig.AppSettings.Settings["something"].Value = "B";
testConfig.Save();

Then try to refresh the section and retrieve the update value using the System.Configuration.Abstractions.ConfigurationManager.

config.RefreshSection("appSettings");
var secondValue = config.AppSettings["something"]; // This returns "A" but _should_ return "B".

The above code should have returned the updated value "B" but returns the original value "A". I've run the above scenario and confirmed the value in App.config file does get updated but I suspect the System.Configuration.Abstractions.ConfigurationManager is using a memory backed cache of app settings.

I'm not sure of the correct solution to this bug other than possible not mixing usage of System.Configuration.Abstractions.ConfigurationManager and the .NET System.Configuration.ConfigurationManager

davidwhitney commented 8 years ago

Interesting - I'd not really considered this scenario.

You're right, I think this counts as a bug just due to it violating the principal of least surprise - you'd expect save to work, and it doesn't.

I'm not entirely sure if it's possible / if there's even an event to hook into when regular ol' configuration manager calls "Save" though.

I'll take a look into this and see if there's something I can do.