I have a .net core CONSOLE app. In the code I am reading in the appropriate environment specific appsettings.dev.json, appsettings.uat.json etc.
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
.AddJsonFile("appsettings.json", false)
.AddJsonFile($"appsettings.{environmentName}.json", optional: true)
.AddEnvironmentVariables()
.Build();
When this is run directly in Visual Studio the ASPNETCORE_ENVIRONMENT variable is set from the launchSettings.json file or from amending the project properties, debug tab and the correct appsettings.environment.json file is read.
When i want to publish the project via Visual Studio i.e. right click project and publish (i.e. I do not want to do this via the command line) I select the appropriate .pubxml file held within PublishProfile folder. In this .pubxml file I have set the environmentName.
<EnvironmentName>dev</EnvironmentName>
However, when running the exe that is produced by the publish process - the environmentName variable specified above is always blank and consequently it doesn't pick up the correct environmentName setting and hence the appropriate appsettings.environment.json file is not read in.