Cayd.AspNetCore.Settings
1.0.1
dotnet add package Cayd.AspNetCore.Settings --version 1.0.1
NuGet\Install-Package Cayd.AspNetCore.Settings -Version 1.0.1
<PackageReference Include="Cayd.AspNetCore.Settings" Version="1.0.1" />
<PackageVersion Include="Cayd.AspNetCore.Settings" Version="1.0.1" />
<PackageReference Include="Cayd.AspNetCore.Settings" />
paket add Cayd.AspNetCore.Settings --version 1.0.1
#r "nuget: Cayd.AspNetCore.Settings, 1.0.1"
#addin nuget:?package=Cayd.AspNetCore.Settings&version=1.0.1
#tool nuget:?package=Cayd.AspNetCore.Settings&version=1.0.1
About
This package helps to register configurations automatically from appsettings, user secrets and environment variables into the ASP.NET Core dependency injection system using IOptions<T>
.
How to Use
After installing the package, you can use ISettings
interface in the Cayd.AspNetCore.Settings
namespace to mark your settings classes for the automatic registering.
For example, suppose you have JWT configuration like below and want to use it in your code via IOptions<T>
.
{
"Jwt": {
"SecretKey": "...",
"Issuer": "api.example.com",
"Audience": "example.com",
"AccessTokenLifeSpanInMinutes": 5,
"RefreshTokenLifeSpanInDays": 2
},
// ... other configurations
}
For that, you need to create a class representing the configuration and implementing ISettings
.
using Cayd.AspNetCore.Settings;
public class JwtSettings : ISettings
{
public static string SettingsKey => "Jwt"; // -> Top-level section name of your configuration
public required string SecretKey { get; set; }
public required string Issuer { get; set; }
public required string Audience { get; set; }
public required int AccessTokenLifeSpanInMinutes { get; set; }
public required int RefreshTokenLifeSpanInDays { get; set; }
}
Afterwards, you need to use one of the extension methods of the library (extending IHostApplicationBuilder
) to register all configurations you define in an assembly. For this example, the AddSettingsFromAssembly
is used.
using Cayd.AspNetCore.Settings.DependencyInjection;
var builder = WebApplication.CreateBuilder();
var assembly = Assembly.GetAssembly(typeof(Program));
builder.AddSettingsFromAssembly(assembly);
You can then use the class you created in your code via the dependency injection system.
public class MyService : IMyService
{
private readonly JwtSettings _jwtSettings;
public MyService(IOptions<JwtSettings> jwtSettings)
{
_jwtSettings = jwtSettings.Value;
}
}
Extras
- If you want a subsection to be a settings class, you can change the
SettingsKey
value as follows:
{
"Section": {
"Subsection1": {
// ...
},
"Subsection2": {
// ...
}
}
}
public class Subsection1 : ISettings
{
public static string SettingsKey => "Section:Subsection1";
}
public class Subsection2 : ISettings
{
public static string SettingsKey => "Section:Subsection2";
}
- If you have settings classes in different assemblies, you can use the
AddSettingsFromAssemblies
extension method.
var applicationLayerAssembly = Assembly.GetAssembly(typeof(MyClassInApplicationLayer));
var infrastructureLayerAssembly = Assembly.GetAssembly(typeof(MyClassInInfrastructureLayer));
var persistenceLayerAssembly = Assembly.GetAssembly(typeof(MyClassInPersistenceLayer));
var presentationLayerAssembly = Assembly.GetAssembly(typeof(MyClassInPresentationLayer));
builder.AddSettingsFromAssemblies(applicationLayerAssembly,
infrastructureLayerAssembly,
persistenceLayerAssembly,
presentationLayerAssembly);
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
- Microsoft.Extensions.Hosting (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Update package info: source repository is added