When creating a .NET Core console application, it's handy to have Web API style appsettings.json to configure the application.
{
"AppConfig": {
"DatabaseConnectionString":
"Data source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=Dev;Integrated Security=True"
}
}
It would be nice to bind this to a concrete class.
class AppConfig
{
public string DatabaseConnectionString { get; set; }
}
Install NuGet packages
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.Binder
- Microsoft.Extensions.Configuration.Json
Add an extension method to IConfiguration
public static class ConfigurationExtensions { public static T ParseAs<T>(this IConfiguration configuration, string path) where T : new() { var result = new T(); configuration.GetSection(path).Bind(result); return result; } }
Use the configuration in your program
class Program { static void Main(string[] args) { var configuration = GetStandardConfigurationBuilder(); var appConfig = configuration.ParseAs<AppConfig>("AppConfig"); Console.WriteLine(appConfig.DatabaseConnectionString); } internal static IConfiguration GetStandardConfigurationBuilder() { return new ConfigurationBuilder() .AddJsonFile("appsettings.json").Build(); } }