Skip to content

AddConfiguration

Registers a strongly typed options class and binds it to an IConfigurationSection. The method enables options binding, data-annotation validation, and startup validation through ValidateOnStart.

Use this helper when a package or application has a configuration record or class that should be injected through IOptions<T>. The method expects the caller to pass the specific section that should be bound, such as builder.Configuration.GetSection("Email").

Usage

csharp
using AlmightyShogun.Utils;

builder.Services.AddConfiguration<ExampleSettings>(
    builder.Configuration.GetSection("Example")
);
json
{
    "Example": {
        "Name": "Importer",
        "Enabled": true
    }
}
csharp
public sealed record ExampleSettings
{
    public required string Name { get; init; }

    public bool Enabled { get; init; }
}

Parameters

section: IConfigurationSection
Configuration section to bind to options type T.

Returns

The IServiceCollection instance with the configured options registration.

Type signature

csharp
public IServiceCollection AddConfiguration<T>(
    IConfigurationSection section
) where T : class;

All packages are released under the MIT License.