ToolBX.AutoConfig
4.0.0-beta1
dotnet add package ToolBX.AutoConfig --version 4.0.0-beta1
NuGet\Install-Package ToolBX.AutoConfig -Version 4.0.0-beta1
<PackageReference Include="ToolBX.AutoConfig" Version="4.0.0-beta1" />
<PackageVersion Include="ToolBX.AutoConfig" Version="4.0.0-beta1" />
<PackageReference Include="ToolBX.AutoConfig" />
paket add ToolBX.AutoConfig --version 4.0.0-beta1
#r "nuget: ToolBX.AutoConfig, 4.0.0-beta1"
#:package ToolBX.AutoConfig@4.0.0-beta1
#addin nuget:?package=ToolBX.AutoConfig&version=4.0.0-beta1&prerelease
#tool nuget:?package=ToolBX.AutoConfig&version=4.0.0-beta1&prerelease
AutoConfig
A .NET library to make it easier to use appsettings sections using [AutoConfig] attributes directly on classes.
Prerequisites
- .NET 10 or later
How does it work?
You write a configuration type, as you normally would except that you add a [AutoConfig] attribute with a string on top of it.
[AutoConfig("MyConfig")]
public sealed record Configuration
{
public string Name { get; init; }
public bool IsAwesome { get; init; }
}
The string is the section name inside your appsettings.json file.
{
"MyConfig": {
"name": "Roger",
"isAwesome": true
}
}
Nested sections
Section paths use the standard .NET : separator:
[AutoConfig("Outer:Inner")]
public sealed record NestedOptions { /* ... */ }
Getting started
In order for your configuration to be injected as an IOptions<T>, you need to call the following method where you configure your application services :
services.AddAutoConfig(configuration);
Alternatively, you can also specify which assembly to use :
services.AddAutoConfig(Assembly.GetExecutingAssembly(), configuration);
The latter is more performant because the former will look through all your assemblies looking for anything with the [AutoConfig] attribute on it. It is more convenient but it comes at certain a cost. Use the Assembly overload if performance is a concern.
Binding types you don't own
When the configuration type lives in an assembly you can't modify (a third-party POCO, for example), use the generic form at the class level on a marker class, or directly on the assembly:
// In any file in your project:
[assembly: AutoConfig<SomeExternalOptions>("ThirdParty")]
The generic attribute supports AllowMultiple = true, so you can bind as many external types as you need.
Validation
AutoConfig supports optional validation via System.ComponentModel.DataAnnotations. Enable it on a single class by setting ValidateDataAnnotations and/or ValidateOnStart on the attribute:
using System.ComponentModel.DataAnnotations;
[AutoConfig("MyConfig", ValidateDataAnnotations = true, ValidateOnStart = true)]
public sealed record Configuration
{
[Required]
public string Name { get; init; }
[Range(1, 100)]
public int MaxRetries { get; init; }
}
ValidateDataAnnotations— validates properties using data annotation attributes such as[Required],[Range],[StringLength], etc.ValidateOnStart— triggers validation when the application starts rather than on first access, causing the app to fail fast if configuration is invalid.
Both default to false, so existing usage is unaffected.
Project-wide defaults
To opt every [AutoConfig] class in at once, pass an AutoConfigOptions to AddAutoConfig:
services.AddAutoConfig(configuration, new AutoConfigOptions
{
ValidateDataAnnotations = true,
ValidateOnStart = true,
});
Per-attribute flags are OR'd with the defaults — turning a flag on globally cannot be cancelled at the attribute level.
Retrieving every bound option
Use GetAutoConfigOptions<T> to grab every registered options instance that is assignable to a given type (an interface or base type). Useful for cross-cutting features like diagnostics or plugin discovery:
public interface IFeatureOptions { bool Enabled { get; } }
[AutoConfig("Feature.A")]
public sealed record FeatureAOptions : IFeatureOptions { public bool Enabled { get; init; } }
[AutoConfig("Feature.B")]
public sealed record FeatureBOptions : IFeatureOptions { public bool Enabled { get; init; } }
// Later, given an IServiceProvider:
var enabled = serviceProvider.GetAutoConfigOptions<IFeatureOptions>()
.Where(x => x.Enabled);
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Configuration.Binder (>= 10.0.5)
- Microsoft.Extensions.Configuration.Json (>= 10.0.5)
- Microsoft.Extensions.DependencyInjection (>= 10.0.5)
- Microsoft.Extensions.Options (>= 10.0.5)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.5)
- Microsoft.Extensions.Options.DataAnnotations (>= 10.0.5)
- ToolBX.Reflection4Humans.TypeFetcher (>= 3.0.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on ToolBX.AutoConfig:
| Package | Downloads |
|---|---|
|
ToolBX.AssemblyInitializer
Helps decouple initialization logic by splitting it into AssemblyInitializer classes. |
|
|
ToolBX.MisterTerminal
A high level library to easily and cleanly build smarter console applications. |
|
|
ToolBX.FileGuy
High-level API for handling files. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.0-beta1 | 354 | 5/11/2026 |
| 4.0.0-beta.1 | 53 | 6/13/2026 |
| 3.0.0 | 2,569 | 9/26/2024 |
| 3.0.0-beta1 | 289 | 9/23/2024 |
| 2.2.0 | 1,255 | 1/11/2024 |
| 2.2.0-beta3 | 435 | 1/7/2024 |
| 2.2.0-beta1 | 332 | 7/26/2023 |