ByteDev.Configuration.Core
Collection of classes to help when dealing with configuration settings in .NET Core.
Installation
ByteDev.Configuration.Core is hosted as a package on nuget.org. To install from the Package Manager Console in Visual Studio run:
Install-Package ByteDev.Configuration.Core
Further details can be found on the nuget page.
Release Notes
Releases follow semantic versioning.
Full details of the release notes can be viewed on GitHub.
Usage
Functionality includes:
IConfigurationBuilder
extensionsIConfiguration
extensionsInMemoryConfigurationBuilder
class
IConfigurationBuilder extension methods
Build a configuration from JSON settings files:
// Add a default settings file (appsettings.json)
var config = new ConfigurationBuilder()
.AddAppSettingsJsonFile()
.Build();
// Add a UAT settings file (appsettings.uat.json)
var config = new ConfigurationBuilder()
.AddAppSettingsJsonFile(new ConfigurationFileOptions
{
Environment = "uat",
IsOptional = false,
ReloadOnChange = true
})
.Build();
IConfiguration extension methods
Retrieve settings from the configuration:
// Get "MySettings" section bound to a type
MySettings settings = config.GetSectionSettings<MySettings>("MySettings");
// Get "ApplicationSettings" section bound to a type
MyAppSettings settings = config.GetApplicationSettings<MyAppSettings>();
// Get a particular "ApplicationSettings" section value
string name = config.GetApplicationSettingsValue<string>("Name");
InMemoryConfigurationBuilder
The InMemoryConfigurationBuilder
class allows a configuration to be built quickly in memory (rather than from a settings JSON file).
var builder = new InMemoryConfigurationBuilder();
// Add a setting
builder.WithSetting("KeyVaultUri", "https://mykeyvault/")
// Add a setting in the "ApplicationSettings" section
builder.WithApplicationSetting("Name", "John");
// Build the configuration
var config = builder.Build();