Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.