AddOptions<T>() creates an OptionsBuilder<T> in the ASP.NET Core service collection. The builder lets you configure a strongly typed options object with fluent calls such as BindConfiguration() and Configure(). section 31.2 section 31.4
Build an options configuration in layers:
- Call Configure() to apply additional values or use Configure<TDep>() to receive dependencies from the DI container. section 31.2
- Add validation when configuration must be checked as the options object is loaded. section 31.2
Configure currency options
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services .AddOptions<CurrencyOptions>() .BindConfiguration("Currencies") .Configure(opts => opts.Currencies = new string[] { "GBP", "USD" }); WebApplication app = builder.Build(); app.MapGet("/", (IOptions<CurrencyOptions> opts) => opts.Value); app.Run(); The service collection registers the builder configuration, and resolving IOptions<CurrencyOptions> produces the configured value.
