6
votes

I am trying to add appsettings transformation to a .net core 2 console application e.g.

  • appSettings.json
  • appSettings.Test.json
  • appSettings.Prod.json

I have found the following code works for asp.net core:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: 
true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: 
true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

However, I don't know how to get env.EnvironmentName because there is no IHostingEnvironment in a console app.

Any help will be appreciated

1
This is not a duplicate because IApplicationEnvironment has now been removed github.com/aspnet/PlatformAbstractions/issues/37 - Aman B
And Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application doesn't contain any environment information - Aman B
Good point well made. - SpruceMoose

1 Answers

2
votes

Couldn't find anything else so using preprocessor directives for now

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if

public Startup()
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: 
true)
#if RELEASE
        .AddJsonFile($"appsettings.Release.json", optional: 
true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}