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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cayd.AspNetCore.Settings" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Cayd.AspNetCore.Settings" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Cayd.AspNetCore.Settings" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Cayd.AspNetCore.Settings --version 1.0.1
                    
#r "nuget: Cayd.AspNetCore.Settings, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Cayd.AspNetCore.Settings&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Cayd.AspNetCore.Settings&version=1.0.1
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 132 6/1/2025
1.0.0 150 5/25/2025

Update package info: source repository is added