Ares.Extensions.Configuration.Annotations
1.0.2
dotnet add package Ares.Extensions.Configuration.Annotations --version 1.0.2
NuGet\Install-Package Ares.Extensions.Configuration.Annotations -Version 1.0.2
<PackageReference Include="Ares.Extensions.Configuration.Annotations" Version="1.0.2" />
<PackageVersion Include="Ares.Extensions.Configuration.Annotations" Version="1.0.2" />
<PackageReference Include="Ares.Extensions.Configuration.Annotations" />
paket add Ares.Extensions.Configuration.Annotations --version 1.0.2
#r "nuget: Ares.Extensions.Configuration.Annotations, 1.0.2"
#:package Ares.Extensions.Configuration.Annotations@1.0.2
#addin nuget:?package=Ares.Extensions.Configuration.Annotations&version=1.0.2
#tool nuget:?package=Ares.Extensions.Configuration.Annotations&version=1.0.2
Binder Options with Microsoft.Extensions.Configuration.Annotations
English | 简体中文
Microsoft.Extensions.Configuration.Annotations is a library that extends the Microsoft.Extensions.Configuration system by providing attribute-based support, allowing more flexible and structured control over configuration items during the binding process via AOP.
Features
- Attribute Support: Automatically bind and verify configuration items through attributes
- Automatic registration options
- Compatibility with Existing Configuration System: Fully compatible with Microsoft.Extensions.Configuration and can be seamlessly integrated into existing projects
Requirements
This library requires .NET 8.0+.
How to Use
Install the Nuget package:
Install-Package Ares.Extensions.Configuration.Annotations
Or via .NET CLI:
dotnet add package Ares.Extensions.Configuration.Annotations
Configuration
First, configure it in the Program.cs file as follows:
var builder = WebApplication.CreateBuilder(args);
// Add Ares.Extensions.Configuration.Annotations
// Add all `OptionsAttribute` defined in the `Program` assembly to the IServiceCollection
builder.Services.AddAttributeConfigurationOptions(builder.Configuration,true,typeof(Program).Assembly);
How to Define?
The examples directory contains a couple of clear examples
Example: Standard Options Class
[Options("app")]
public class MyAppOptions
{
public int Id { get; set; }
public string Name {get; set; }
...
}
Example: Bind Non-Public Properties
[Options(SessionKey = "app", BindNonPublic = true)]
public class AppOptions
{
private int Id { get; set; }
public string Name { get;set; }
...
}
Example: Throw Exception if Missing Properties
// appsettings.json Configuration
//
// "App":{
// "Id":1,
// "Name":"my app",
// "Version": "1.0.0"
// }
[Options(SessionKey = "app", BindNonPublic = true,ThrowOnUnknownConfig = true)]
public class AppOptions
{
private int Id { get; set; }
public string Name { get;set; }
...
}
Validators
Example: Options Class with Validator
[Validate]
[Options("app")]
public class MyAppOptions
{
[Range(1,20)]
public int Id { get; set; }
[Required]
public string Name {get; set; }
...
}
Example: Options Class with Custom Validator
[Validate(typeof(MyAppValidateOptions))]
[Options("app")]
public class MyAppOptions
{
public int Id { get; set; }
public string Name {get; set; }
...
}
public class MyAppValidateOptions : IValidateOptions<MyAppOptions>
{
public ValidateOptionsResult Validate(string name, MyAppOptions options)
{
// To do validate for MyAppOptions
return ValidateOptionsResult.Success;
}
}
Accessing Options
Example: Accessing in Constructor
public class MyService
{
private readonly MyAppOptions _myAppOptions;
public MyService(IOptions<MyAppOptions> myAppOptions)
{
_myAppOptions = myAppOptions.Value;
}
public void PrintSettings()
{
Console.WriteLine(_myAppOptions.Id);
Console.WriteLine(_myAppOptions.Name);
}
}
Example: Accessing via IServiceProvider
IServiceCollection services = new ServiceCollection();
IConfigurationBuilder builder = new ConfigurationBuilder();
// Add option data
builder.AddInMemoryCollection(new Dictionary<string, string?>()
{
{ "app:id", "1" },
{ "app:name", "test app" },
{ "app:version", "1.0.0" },
{ "app:description", "test options bind" },
});
IConfigurationRoot configurationRoot = builder.Build();
services.AddAttributeConfigurationOptions(configurationRoot,true,typeof(Program).Assembly);
var provider = services.BuildServiceProvider();
// Gets Options with Options Attribute
var options = provider.GetService<IOptions<MyAppOptions>>();
Console.WriteLine(options.Value.Id);
Console.WriteLine(options.Value.Name);
Contribute
One of the easiest ways to contribute is to participate in discussions and discuss issues. You can also contribute by submitting pull requests with code changes.
License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
-
net8.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Use Ares.Extensions.Configuration.Annotations attributes binding get Microsoft.Extensions.Configuration options.
Release notes:
1.0.0: Upgrade Microsoft.NET.Sdk to net8.0.