Read all configurations from environment and IConfiguration to a class.
- Simple configuration loader
- Auto load named variable to property
- Automatic type conversion
dotnet add package Ngb.Configuration
Using package
using Ngb.Configuration;Example appsettings.json
{
"ConnectionStrings": {
"Default": "",
"Sqlite": ""
},
"ApiUrl": "https://example.com",
"ServerPort": "8080"
}Define object class
// example config class
public class GlobalConfig {
// this will load environment variable API_URL or use default value if not exists
[FromConfig("API_URL")]
public string ApiUrl { get; set; } "http://localhost";
[FromConfig("SERVER_PORT")
public int ServerPort { get; set; }
}Call from main
var builder = WebApplication.CreateBuilder(args);
IConfiguration configuration = builder.Configuration;
var globalConfig = ConfigReader.ReadAll<GlobalConfig>(configuration);Prepare class
public class GlobalConfig {
// this will load environment variable API_URL
[FromConfig("API_URL")]
public string ApiUrl { get; set; }
[FromConfig("SERVER_PORT")
public int ServerPort { get; set; }
public GlobalConfig(IConfiguration? configuration) {
// set configuration to null to ignore IConfiguration
ConfigReader.ReadAll(this, configuration);
}
}Call from main
var builder = WebApplication.CreateBuilder;
var globalConfig = new GlobalConfig(builder.Configuration);// example config class
public class GlobalConfig {
// this will load environment variable API_URL
[FromConfig("DEFAULT_CONNECTION", Section = "ConnectionStrings", Key = "Default")]
public string DatabaseServer { get; set; }
[FromConfig("SQLITE_CONNECTION", Section = "ConnectionStrings")
public string Sqlite { get; set; }
public GlobalConfig(IConfiguration? configuration) {
// set configuration to null to ignore IConfiguration
ConfigReader.ReadAll(this, configuration);
}
}public class GlobalConfig {
// this will load environment variable API_URL
[FromConfig("API_URL")]
public string ApiUrl { get; set; }
[FromConfig("SERVER_PORT")
public int ServerPort { get; set; }
DatabaseConfig DatabaseConnection { get; }
public GlobalConfig(IConfiguration? configuration) {
// set configuration to null to ignore IConfiguration
ConfigReader.ReadAll(this, configuration);
var section = configuration?.GetSection("ConnectionStrings");
DatabaseConnection = new DatabaseConfig(section);
}
}
public class DatabaseConfig {
// this will load environment variable API_URL
[FromConfig("DEFAULT_CONNECTION", Key = "Default")]
public string DatabaseServer { get; set; }
[FromConfig("SQLITE_CONNECTION")
public string Sqlite { get; set; }
public GlobalConfig(IConfiguration? configuration) {
// set configuration to null to ignore IConfiguration
ConfigReader.ReadAll(this, configuration);
}
}- No hot reload