appsettings.Json .net5 console app 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#
using System;
using Microsoft.Extensions.Configuration;
namespace DiConsoleApp
{
public class SomeService : ISomeService
{
IConfiguration configuration;
public SomeService(IConfiguration configuration)
{
this.configuration = configuration;
}
public void DoProcess()
{
var value = configuration["SomeKey"];
Console.WriteLine("Value from the Config is: " + value);
}
}
}