This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Logging": { | |
"IncludeScopes": false, | |
"LogLevel": { | |
"Default": "Warning" | |
} | |
}, | |
"MyAppSettings": { | |
"ApplicationName": "Hello World", | |
"CountOfItems": 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyAppSettings | |
{ | |
public string ApplicationName { get; set; } | |
public int CountOfItems { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller | |
{ | |
private readonly MyAppSettings _settings; | |
public HomeController(MyAppSettings settings) | |
{ | |
_settings = settings ?? throw new ArgumentNullException(nameof(settings)); | |
} | |
public IActionResult Index() | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller | |
{ | |
private readonly MyAppSettings _settings; | |
public HomeController(IOptionsSnapshot<MyAppSettings> settings) | |
{ | |
_settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings)); | |
} | |
public IActionResult Index() | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
services.AddOptions(); | |
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IMyAppSettings | |
{ | |
string ApplicationName { get; } | |
int CountOfItems { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyAppSettingsReader : IMyAppSettings | |
{ | |
public string ApplicationName { get; set; } | |
public int CountOfItems { get; set; } | |
} | |
public class MyAppSettingsBridge : IMyAppSettings | |
{ | |
private readonly IOptionsSnapshot<MyAppSettingsReader> _optionsConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
OlderNewer