Skip to content

Instantly share code, notes, and snippets.

@stuartleeks
Last active October 13, 2016 12:23
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 stuartleeks/e356956ebf5f1e38718c to your computer and use it in GitHub Desktop.
Save stuartleeks/e356956ebf5f1e38718c to your computer and use it in GitHub Desktop.
CustomConfigurationManager - pre ASP.NET 5 helper for working with environment variables for app settings
using System;
using System.Configuration;
public static class CustomConfigurationManager
{
private static CustomAppSettings _appSettings = new CustomAppSettings();
public static CustomAppSettings AppSettings { get { return _appSettings; } }
}
public class CustomAppSettings
{
const string AppSettingsPrefix = "APPSETTING_";
public string AppPrefix { get; set; }
public string Get(string key)
{
return this[key];
}
public string this[string key]
{
get
{
string fullKey;
if (!string.IsNullOrEmpty(AppPrefix))
{
fullKey = AppPrefix + "_" + key;
}
else
{
fullKey = key;
}
string value = Environment.GetEnvironmentVariable(AppSettingsPrefix + fullKey)
?? Environment.GetEnvironmentVariable(fullKey)
?? ConfigurationManager.AppSettings[fullKey]
?? ConfigurationManager.AppSettings[key];
return value;
}
}
}
@exalted
Copy link

exalted commented Apr 8, 2016

@stuartleeks could you perhaps use CloudConfigurationManager instead? Is there anything am I underestimating in your implementation please? Thx 🍻

@stuartleeks
Copy link
Author

Does CloudConfigurationManager check environment variables? IIRC it checks the role config for cloud services

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment