Table of Contents

ConfigurationOptionsExtensions Class

Definition

Namespace
Bodu.Extensions.Configuration.Text
Assembly
Bodu.Extensions.Configuration.Text.dll
Package
Bodu.Extensions.Configuration.Text 1.0.0
Source
ConfigurationOptionsExtensions.cs

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

services IServiceCollection

The service collection to register the binding with.

configuration IConfiguration

The configuration root that contains the named section.

sectionName string

The colon-delimited section name to bind from.

Returns

IServiceCollection

The supplied services, for chaining.

Type Parameters

TOptions

The options type to bind.

Exceptions

ArgumentNullException

services or configuration is null.

ArgumentException

sectionName is 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

services IServiceCollection

The service collection to register the binding with.

section IConfigurationSection

The configuration section to bind from.

Returns

IServiceCollection

The supplied services, for chaining.

Type Parameters

TOptions

The options type to bind.

Exceptions

ArgumentNullException

services or section is null.

Applies to

ProductVersions
.NET8, 10