NexumNovus.AppSettings.Json 0.2.1

This package has a SemVer 2.0.0 package version: 0.2.1+build.31.
dotnet add package NexumNovus.AppSettings.Json --version 0.2.1
NuGet\Install-Package NexumNovus.AppSettings.Json -Version 0.2.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="NexumNovus.AppSettings.Json" Version="0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NexumNovus.AppSettings.Json --version 0.2.1
#r "nuget: NexumNovus.AppSettings.Json, 0.2.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.
// Install NexumNovus.AppSettings.Json as a Cake Addin
#addin nuget:?package=NexumNovus.AppSettings.Json&version=0.2.1

// Install NexumNovus.AppSettings.Json as a Cake Tool
#tool nuget:?package=NexumNovus.AppSettings.Json&version=0.2.1

Banner

NexumNovus.AppSettings.Json

NexumNovus.AppSettings.Json NuGet Package NexumNovus.AppSettings.Json NuGet Package Downloads GitHub Actions Status

GitHub Actions Build History

About

JSON configuration provider implementation for Microsoft.Extensions.Configuration. This package enables you to

  • read application's settings from a JSON file.
  • update application's settings and save changes to a JSON file.
  • cryptographically protect settings

Use JsonConfigurationExtensions.AddJsonConfig extension method on IHostBuilder to add the JSON configuration provider to the configuration builder and register JsonSettingsRepository with service collection.

Use ISettingsRepository.UpdateSettingsAsync to update JSON settings file.

Mark properties with SecretSetting attribute to cryptographically protect them.

Example

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using NexumNovus.AppSettings.Common;
using NexumNovus.AppSettings.Common.Secure;
using NexumNovus.AppSettings.Json;

var builder = WebApplication.CreateBuilder(args);

// Load settings from json file and register settings repository with service collection
builder.Host.AddJsonConfig("appsettings.json");

// Use of options pattern to register configuration elements is optional.
builder.Services.AddOptions<EmailSettings>().BindConfiguration(EmailSettings.ConfigElement);

var app = builder.Build();

// Api's to get and update EmailSettings
app.MapGet("/emailSettings", (IOptionsMonitor<EmailSettings> emailSettings) => emailSettings.CurrentValue);
app.MapPost("/emailSettings", async (EmailSettings emailSettings, ISettingsRepository settingsRepo)
  => await settingsRepo.UpdateSettingsAsync(EmailSettings.ConfigElement, emailSettings)
);

// Api's to get and update setting
app.MapGet("/settings", (string section, IConfiguration settings) => settings.GetSection(section));
app.MapPost("/settings", async (string section, string value, ISettingsRepository settingsRepo)
  => await settingsRepo.UpdateSettingsAsync(section, value)
);

app.Run();

public record EmailSettings
{
  public static string ConfigElement = "Email";
  public string Host { get; set; }
  public string Username { get; set; }
  [SecretSetting]
  public string Password { get; set; } //this setting will be cryptographically protected
} 

To run this example, include an appsettings.json file with the following content in your project:

{
  "Region": "demo",
  "Email": {
    "Host": "example.com",
    "Username": "",
    "Password": ""
  }
}

If a setting is not found in the configuration file, then it's created.

Note that password is marked with [SecretSetting] and it will be protected. After update appsettings.json will look like:

{
  "Region": "demo",
  "Email": {
    "Host": "example.dom",
    "Username": "my_username",
    "Password*": "CfDJ8IBGRtcA2S1Ji7VPVwaKmLYnTN6skE_2RQqvNZ8_CN5y3Xvk3LkFC6GXCe8EY7AicxH5...."
  }
}

Default implementation for ISecretProtector uses Microsoft.AspNetCore.DataProtection. You can also provide your own implementation:

builder.Host.AddJsonConfig(x =>
{
  x.Path = "appsettings.json",
  x.Protector = <your implementation of ISecretProtector>
});
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
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
0.2.1 76 6/4/2023
0.2.0 63 5/26/2023
0.1.2 67 5/11/2023
0.1.1 67 5/11/2023
0.1.0 65 5/8/2023