Skip to content

Instantly share code, notes, and snippets.

@configureappio
configureappio / Startup.cs
Created March 24, 2018 07:39
An example of using a lambda function to create a bridge between a settings class and IOptionsSnapshot<T>
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings"));
// use a lambda to act as the bridge between requesting an instance of MyAppSettings but
// getting it from IOptionsSnapshot<MyAppSettings>
services.AddTransient<MyAppSettings>((opt) => opt.GetService<IOptionsSnapshot<MyAppSettings>>().Value);
}
@configureappio
configureappio / appsettings.json
Created March 24, 2018 07:41
Example appsettings.json file
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"MyAppSettings": {
"ApplicationName": "Hello World",
"CountOfItems": 10
@configureappio
configureappio / MyAppSettings.cs
Created March 24, 2018 07:43
Example class that will be mapped to configuration in appsettings.json
public class MyAppSettings
{
public string ApplicationName { get; set; }
public int CountOfItems { get; set; }
}
@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()
{
@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 / 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.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings"));
}
@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; }
}
@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 / 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"));