Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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);
}