ConfigurationOptionsExtensions Class
Definition
- Namespace
- Bodu.Extensions.Configuration.Text
- Assembly
- Bodu.Extensions.Configuration.Text.dll
- Package
- Bodu.Extensions.Configuration.Text 1.0.0
Provides convenience extension methods for binding a Bodu Text Configuration section to an IOptions<TOptions> instance through an IServiceCollection .
public static class ConfigurationOptionsExtensions
- Inheritance
-
ConfigurationOptionsExtensions
- Inherited Members
Examples
// Strongly-typed options bound to the "Logging" section produced by AddTextConfigurationFile.
public sealed class LoggingOptions
{
public string Level { get; init; } = "Information";
public bool IncludeScopes { get; init; }
}
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddTextConfigurationFile("appsettings.boduconfig");
// Bind by section name against the configuration root.
builder.Services.AddConfigurationOptions<LoggingOptions>(
builder.Configuration,
sectionName: "Logging");
// Or bind a pre-resolved section directly.
IConfigurationSection logging = builder.Configuration.GetSection("Logging");
builder.Services.AddConfigurationOptions<LoggingOptions>(logging);
// Consumed via constructor injection.
public sealed class HomeController(IOptions<LoggingOptions> options) { /* options.Value.Level */ }
Remarks
These helpers are thin shims over
Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure; they
exist purely to keep the call-site short and discoverable for consumers who reach for an
AddTextConfiguration* API by name. Callers comfortable with
IOptions<TOptions> binding may continue to call
Configure<TOptions>(IServiceCollection, IConfiguration)
directly.
Two overloads are provided: one that takes the section name and the root IConfiguration, and one that takes a pre-resolved IConfigurationSection. Both produce the same registration; pick whichever shape is more natural at the call site.
Methods
AddConfigurationOptions<TOptions>(IServiceCollection, IConfiguration, string)
Registers an TOptions binding against the section named sectionName
in configuration.
public static IServiceCollection AddConfigurationOptions<TOptions>(this IServiceCollection services, IConfiguration configuration, string sectionName) where TOptions : class
Parameters
servicesIServiceCollectionThe service collection to register the binding with.
configurationIConfigurationThe configuration root that contains the named section.
sectionNamestringThe colon-delimited section name to bind from.
Returns
- IServiceCollection
The supplied
services, for chaining.
Type Parameters
TOptionsThe options type to bind.
Exceptions
- ArgumentNullException
servicesorconfigurationis null.- ArgumentException
sectionNameis null, empty, or whitespace.
AddConfigurationOptions<TOptions>(IServiceCollection, IConfigurationSection)
Registers an TOptions binding against the supplied configuration
section.
public static IServiceCollection AddConfigurationOptions<TOptions>(this IServiceCollection services, IConfigurationSection section) where TOptions : class
Parameters
servicesIServiceCollectionThe service collection to register the binding with.
sectionIConfigurationSectionThe configuration section to bind from.
Returns
- IServiceCollection
The supplied
services, for chaining.
Type Parameters
TOptionsThe options type to bind.
Exceptions
- ArgumentNullException
servicesorsectionis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |