Skip to main content

Curiosus.Configuration.YML

NuGet Downloads Coverage

YAML configuration provider: YamlConfigurationProvider<T> reads layered config*.yml files, environment variables and command line arguments and binds them to a POCO. Use it when the application is configured with YAML files instead of appsettings.json.

Installation​

dotnet add package Curiosus.Configuration.YML

Usage​

Files are looked up in the given directory (the current directory if null), in this order:

FileRequiredLoaded when
config.ymlyes (unless isConfigOptional: true)always
config.secret.ymlnoalways
config.{env}.yml, config.{env}.secret.ymlnoASPNETCORE_ENVIRONMENT is set and is not Production
config.{env}.{USER}.yml, config.{env}.{USER}.secret.ymlnoas above and USER is set

Environment variables and command line arguments are applied on top. Keep *.secret.yml out of source control.

# config.yml
ServiceUrl: https://example.com
Smtp:
Host: smtp.example.com
Port: 587
using Curiosus.Configuration;

var provider = new YamlConfigurationProvider<AppConfiguration>(configurationBasePath: null, cliArgs: args);
var configuration = provider.GetConfiguration(); // strongly typed, cached
var raw = provider.GetRawConfiguration(); // IConfiguration

// or plug the same sources into a host
var builder = WebApplication.CreateBuilder(args);
provider.ConfigureAppConfiguration(builder.Configuration);

public class AppConfiguration
{
public string ServiceUrl { get; set; } = null!;
public SmtpSection Smtp { get; set; } = new();
}

public class SmtpSection
{
public string Host { get; set; } = null!;
public int Port { get; set; }
}

Note: the provider lives in the Curiosus.Configuration namespace.

See also​