Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net core 2 console appsettings transform

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

like image 307
Aman B Avatar asked Oct 30 '17 11:10

Aman B


1 Answers

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

https://learn.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();
}
like image 150
Aman B Avatar answered Nov 04 '22 07:11

Aman B