In my asp.net core 3.1 console app. In main class I have code like this:
class Program
{
static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
BuildConfig(builder);
var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddTransient<StartService>();
})
.Build();
var svc = ActivatorUtilities.CreateInstance<StartService>(host.Services);
svc.Run();
}
static void BuildConfig(IConfigurationBuilder builder)
{
builder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json", optional: true)
.AddEnvironmentVariables();
}
}
Environment set to Development
and config files like this (only values there differ):
My app keeps taking values from appsettings.json. What to change in order to take values from appsettings.Developement.json?
I also tried like this, but it didn`t work either:
static void BuildConfig(IConfigurationBuilder builder)
{
builder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.Development.json", optional: true)
.AddEnvironmentVariables();
}
Does anyone can help with that? Files are properly copied to bin
builder.SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.Development.json", optional: false, reloadOnChange: true) .AddEnvironmentVariables();
– Pat