I have an ASP Core 2.2 application which runs fine.
- I have an appsettings.json
- I have an appsettings.QA.json
- I have an Azure App Service which I have added ASPNETCORE_ENVIRONMENT variable to in the Configuration > App Settings section of the portal and set the value to "QA".
- I have also added a connection string directly to the Configuration > Connection Strings section of the portal.
When I publish to my Azure App Service, I want to override the settings in appsettings.json with values from appsettings.QA.json. I am struggling to make sense of how this should be configured.
I am reading out the values from the appsettings via the Microsoft.Extensions.Configuration.IConfiguration implementation which is injected into classes where I need such configuration.
So far I have the following:
Program.cs:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
})
.UseStartup<Startup>();
When the app runs in Azure, it is picking up the connection string from the portal and using it. However, when I read out a value from the appsettings, I always get the value from appsettings.json not appsettings.QA.json.
- Can anyone point out how to configure the transforms correctly for an ASP Core 2.2 application?
- Am I going about this in the wrong way entirely?
Update
- I have removed my custom code above as suggested as it is not needed.
- If I output
@Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")in my view, I get: "QA;Development". It seems that the "Development" is coming from the web.config which also has ASPNETCORE_ENVIRONMENT set.
appsettings.QA.jsonexists in the App Service? If it's easy enough, it might be worth changing tooptional: falsejust to make sure the file is being picked up. Also, theConfigureAppConfigurationisn't really needed in your example, as this is the default setup when usingWebHost.CreateDefaultBuilder. Lastly, as @poke asks, how exactly do you read this configuration value?