Skip to content

Instantly share code, notes, and snippets.

@maartenba
Created May 31, 2016 08:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maartenba/d5d4510b5b32e25e77af8798e24c1ba2 to your computer and use it in GitHub Desktop.
Save maartenba/d5d4510b5b32e25e77af8798e24c1ba2 to your computer and use it in GitHub Desktop.
Override .NET's source for AppSettings (hacky, don't use this at home)
public sealed class KeyVaultConfigSystem
: IInternalConfigSystem
{
private const string AppSettingsKey = "appSettings";
private readonly IInternalConfigSystem _internalConfigSystem;
private object _appSettings;
private KeyVaultConfigSystem(IInternalConfigSystem internalConfigSystem)
{
_internalConfigSystem = internalConfigSystem;
}
public static void Install()
{
// Make sure ConfigurationManager is initialized
// ReSharper disable once UnusedVariable
var temp = ConfigurationManager.AppSettings;
// Set KeyVaultConfigSystem as ConfigurationManager's IInternalConfigSystem
// https://github.com/Microsoft/referencesource/blob/master/System.Configuration/System/Configuration/ConfigurationManager.cs#L19
var configSystemField = typeof(ConfigurationManager)
.GetField("s_configSystem", BindingFlags.Static | BindingFlags.NonPublic);
if (configSystemField != null)
{
configSystemField.SetValue(null,
new KeyVaultConfigSystem((IInternalConfigSystem)configSystemField.GetValue(null)));
return;
}
throw new NotSupportedException("Can not set ConfigurationManager's internal configuration system.");
}
public object GetSection(string configKey)
{
if (configKey == AppSettingsKey && _appSettings != null)
{
return _appSettings;
}
object internalSection = _internalConfigSystem.GetSection(configKey);
if (configKey == AppSettingsKey && internalSection is NameValueCollection)
{
// Create a new NameValueCollection
var mergedConfiguration = new NameValueCollection((NameValueCollection)internalSection);
// Inject our own settings
mergedConfiguration["MySampleSecret"] = "Hello from custom config system";
// Store for future use
_appSettings = mergedConfiguration;
return _appSettings;
}
return internalSection;
}
public void RefreshConfig(string sectionName)
{
if (sectionName == AppSettingsKey)
{
_appSettings = null;
}
_internalConfigSystem.RefreshConfig(sectionName);
}
public bool SupportsUserConfig
{
get { return _internalConfigSystem.SupportsUserConfig; }
}
}
class Program
{
static void Main(string[] args)
{
KeyVaultConfigSystem.Install();
Console.WriteLine(ConfigurationManager.AppSettings["MySampleSecret"]);
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment