using Microsoft.Extensions.Options; public class MyService { private readonly MyOptions _options; public MyService(IOptionsServiceoptionsService) { if (!optionsService.TryGetRequiredOption(out _options)) { throw new InvalidOperationException("MyOptions is missing"); } } public void DoSomething() { // access configuration options from _options object } } public class MyOptions { public int MaxValue { get; set; } }
using Microsoft.Extensions.Options; public class MySettings { public string ConnectionString { get; set; } public int MaxValue { get; set; } } public class MyService { private readonly MySettings _settings; public MyService(IOptionsServiceIn this example, we create a class MySettings which has two properties, ConnectionString and MaxValue, that are used to store configuration data. We then create a service class and use IOptionsService to retrieve the MySettings object. The service can then access configuration data from the MySettings object as needed. The Microsoft.Extensions.Options package library contains the IOptionsService interface.optionsService) { optionsService.TryGetRequiredOption(out _settings); } public void DoSomething() { // access configuration from _settings object } }