Skip to content

Instantly share code, notes, and snippets.

@configureappio
configureappio / Program.cs
Created April 2, 2018 15:56
Example of using a PhysicalFileProvider to access a JSON file outside of the web site
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var webhost = WebHost.CreateDefaultBuilder(args);
@configureappio
configureappio / Startup.cs
Created April 2, 2018 15:38
Example of injecting a decyption service and settings validator into the settings bridge
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings"));
services.AddSingleton(Configuration);
// In a real implementation, you would have a factory here to get the password and salt securely
// from somewhere such as Azure Key Vault, Environmental variable / another json setting (obfuscated in some way)
services.AddSingleton( x => new CryptoFactory().Create<AesManaged>("Password", "Salt"));
@configureappio
configureappio / HomeController.cs
Created March 28, 2018 18:17
Example of change to controller to take IMyAppSettings as the parameter instead of IOptionsSnapshot<MyAppSettings>
public class HomeController : Controller
{
private readonly IMyAppSettings _settings;
public HomeController(IMyAppSettings settings)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
}
public IActionResult Index()
{
@configureappio
configureappio / Startup.cs
Created March 28, 2018 18:16
Example of changes required to Startup.cs to use the reader for the configuration mapping and the bridge as the implementation used by controller
public class Startup
{
// Other methods omitted for brevity in example
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
// use the reader class to map the configuration
services.Configure<MyAppSettingsReader>(Configuration.GetSection("MyAppSettings"));
@configureappio
configureappio / MyAppSettingsClasses.cs
Created March 28, 2018 18:12
Example of creating a reader class and a bridge class that both implement IMyAppSettings
public class MyAppSettingsReader : IMyAppSettings
{
public string ApplicationName { get; set; }
public int CountOfItems { get; set; }
}
public class MyAppSettingsBridge : IMyAppSettings
{
private readonly IOptionsSnapshot<MyAppSettingsReader> _optionsConfig;
@configureappio
configureappio / IMyAppSettings.cs
Created March 28, 2018 18:10
Example of moving the application settings to an interface
public interface IMyAppSettings
{
string ApplicationName { get; }
int CountOfItems { get; }
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings"));
}
@configureappio
configureappio / Startup.cs
Created March 24, 2018 07:50
Example of configuration in Startup.cs that will cause the DI container to generate IOptions<MyAppSettings>
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
@configureappio
configureappio / HomeController.cs
Created March 24, 2018 07:48
Example of a controller taking IOptionsSnapshot<T> as a parameter to the constructor
public class HomeController : Controller
{
private readonly MyAppSettings _settings;
public HomeController(IOptionsSnapshot<MyAppSettings> settings)
{
_settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings));
}
public IActionResult Index()
{
@configureappio
configureappio / HomeController.cs
Created March 24, 2018 07:44
Example of controller taking the domain object for configuration
public class HomeController : Controller
{
private readonly MyAppSettings _settings;
public HomeController(MyAppSettings settings)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
}
public IActionResult Index()
{