C# CONSOLE inject appsettings code example

Example 1: appsettings in console application c#

public Startup()
{
    var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

        configuration = new ConfigurationBuilder()
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                        .AddJsonFile($"appsettings.{environment}.json", optional: true)
                        .AddEnvironmentVariables()
                        .Build();
}

Example 2: appsettings in console application c#

{
    "SomeKey": "This is from Config!"
}