2

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.

  1. Can anyone point out how to configure the transforms correctly for an ASP Core 2.2 application?
  2. 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.
3
  • “However, when I read out a value from the appsettings” – How are you doing that exactly? Can you show how you consume the configuration? Commented Jul 31, 2019 at 9:02
  • Just to rule out the simplest of possibilities - Have you confirmed that appsettings.QA.json exists in the App Service? If it's easy enough, it might be worth changing to optional: false just to make sure the file is being picked up. Also, the ConfigureAppConfiguration isn't really needed in your example, as this is the default setup when using WebHost.CreateDefaultBuilder. Lastly, as @poke asks, how exactly do you read this configuration value? Commented Jul 31, 2019 at 9:29
  • @KirkLarkin The appsettings.QA.json is definitely published to the Azure App Service. I am reading out the values via the Microsoft.Extensions.Configuration.IConfiguration implementation which is injected into the classes I need. Commented Jul 31, 2019 at 9:37

2 Answers 2

4

you shouldn’t read environment from appsettings. environment is set on machine level and is read at startup.

you have 2 options:

  1. for local machine, set environment variable in launch.settings

  2. in azure, set it in app service configuration as environment variable

ref: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.2

Sign up to request clarification or add additional context in comments.

4 Comments

“you shouldn’t read environment from appsettings” – What do you mean with that?
I have set ASPNETCORE_ENVIRONMENT in the Azure portal directly, under Configuration > App Settings.
sorry, i’ve missread your question. this should help: stackoverflow.com/questions/53161103/…
also, using default web builder should be enough since it applies correct appsettings file based on environment set.
0

I now have this working correctly:

  • Removed the custom code from the Program.cs as suggested by @KirkLarkin and @dee-zg.
  • Removed the ASPNETCORE_ENVIRONMENT setting from the Azure portal. The reason for this is that there is also a web.config in the app which has this setting. When reading from the config, it seems that this version is used, not the Azure portal version.

Why is the ASPNETCORE_ENVIRONMENT in the web.config? There is a discussion about this here. Ultimately, what I have done to work around this is to add transforms for the web.config.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.