liveBook · Manning

IConfigureOptions<T>

As explained in ASP.NET Core in Action, 3e

IConfigureOptions<T> is a .NET interface implemented by components that configure instances of an options type.

IConfigureOptions<T> lets a separate configuration class customize an options object. Its Configure method receives the options instance and can populate it using services resolved through dependency injection. section 31.2

Use the interface when options configuration needs application services or clearer separation from registration code:

  • Implement IConfigureOptions<CurrencyOptions> and inject ICurrencyProvider through the constructor. section 31.2
  • Register the implementation with the dependency-injection container after binding configuration when its values should override earlier configuration. section 31.2
  • Register injected dependencies with compatible lifetimes; a singleton configurator must not capture scoped or transient services. section 31.2

Configure currencies from a service

public class ConfigureCurrencyOptions : IConfigureOptions<CurrencyOptions> { private readonly ICurrencyProvider _currencyProvider; public ConfigureCurrencyOptions(ICurrencyProvider currencyProvider) { _currencyProvider = currencyProvider; } public void Configure(CurrencyOptions options) { options.Currencies = _currencyProvider.GetCurrencies(); } } builder.Services.AddOptions<CurrencyOptions>() .BindConfiguration("Currencies"); builder.Services.AddSingleton<IConfigureOptions<CurrencyOptions>, ConfigureCurrencyOptions>(); builder.Services.AddSingleton<ICurrencyProvider, CurrencyProvider>(); When IOptions<CurrencyOptions> is requested, configuration binding occurs first, followed by the configurator. section 31.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains implementation, registration, ordering, and lazy runtime construction without descending into interface internals.
Examples
Demonstrates CurrencyOptions configured from an injected ICurrencyProvider after binding the Currencies configuration section.
What it assumes
Assumes familiarity with IOptions<T>, dependency injection, service registration, and configuration binding.
What you can do after
Apply it via dependency injection to build options from configuration and application services.
How it connects
Connects IOptions<T> with dependency injection, OptionsBuilder<T>, configuration binding, and service lifetimes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.