ObjectConfigurationExtensions lets you add a concrete object directly to Microsoft.Extensions.Configuration.
The library supports object graphs containing scalar values, nested objects, collections, and dictionaries, and targets both netstandard2.0 and net10.0.
dotnet add package Kralizek.Extensions.Configuration.ObjectsAddObject follows the normal configuration-provider convention: the object is added with higher precedence than providers registered before it.
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddObject(new
{
Value = 123,
ManyValues = new ComplexObject[]
{
new("New value", 234),
new("Another value", 345)
},
Flag = true,
Text = "Something"
}, "Test");
var app = builder.Build();
app.MapGet("/", (IConfiguration configuration) => configuration
.GetSection("Test")
.AsEnumerable()
.OrderBy(c => c.Key)
.ToDictionary(c => c.Key, v => v.Value));
app.Run();
public record ComplexObject(string Text, int Number);The root section name is optional. To add the properties directly to the root configuration:
builder.Configuration.AddObject(new
{
IsEnabled = false
});Configuration paths can also be used as root section names:
builder.Configuration.AddObject(
new { Enabled = true },
"Features:Payments");The value is then available as Features:Payments:Enabled.
Use AddObjectAsFallback when the object contains defaults that should be overridden by the configuration already registered by the host:
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddObjectAsFallback(new
{
FeatureEnabled = false,
RetryCount = 3
});The fallback is inserted at the lowest precedence, so appsettings.json, environment variables, command-line arguments, and any other higher-precedence providers can override those values. This is true even when AddObjectAsFallback is called after those providers have already been registered.
Both AddObject and AddObjectAsFallback have overloads accepting JsonTypeInfo<T>. Use them when reflection-based System.Text.Json serialization is not appropriate, including trimming and Native AOT scenarios.
[JsonSerializable(typeof(MySettings))]
internal partial class AppJsonContext : JsonSerializerContext;
builder.Configuration.AddObject(
new MySettings { FeatureEnabled = true },
AppJsonContext.Default.MySettings);IConfigurationBuilder AddObject<T>(
T? value,
string? rootSectionName = "");
IConfigurationBuilder AddObject<T>(
T? value,
JsonTypeInfo<T> jsonTypeInfo,
string? rootSectionName = "");
IConfigurationBuilder AddObjectAsFallback<T>(
T? value,
string? rootSectionName = "");
IConfigurationBuilder AddObjectAsFallback<T>(
T? value,
JsonTypeInfo<T> jsonTypeInfo,
string? rootSectionName = "");The library follows Semantic Versioning. Stable releases and public prereleases are published to NuGet.org.
The repository uses the .NET SDK directly.
dotnet restore
dotnet build --configuration Release
dotnet test --configuration Release
dotnet pack --configuration Release