AddConfiguration
Binds a strongly typed options class to a configuration section, validating it against its data annotations at startup so a missing or malformed setting stops the application with a message naming the property.
Resolve the result through IOptions<T> as usual. Set validateDataAnnotations to false for a settings type that is legitimately partial, and validateOnStart to false to defer validation to first resolution.
Usage
csharp
using AlmightyShogun.Utils;
using Microsoft.Extensions.DependencyInjection;
builder.Services.AddConfiguration<MailSettings>(
builder.Configuration.GetSection("Mail")
);csharp
using System.ComponentModel.DataAnnotations;
public sealed class MailSettings
{
[Required]
[EmailAddress]
public required string FromAddress { get; init; }
[Range(1, 300)]
public int TimeoutSeconds { get; init; } = 30;
}json
{
"Mail": {
"FromAddress": "noreply@example.com",
"TimeoutSeconds": 30
}
}Parameters
section: IConfigurationSection
Configuration section the options type is bound to.
validateDataAnnotations: bool
Validates the bound options against their data annotations.
Default: true
validateOnStart: bool
Runs validation during application startup instead of on first resolution.
Default: true
Returns
The sameIServiceCollection instance with the options binding configured.Type signature
csharp
public IServiceCollection AddConfiguration<T>(
IConfigurationSection section,
bool validateDataAnnotations = true,
bool validateOnStart = true
) where T : class;