25

I've added additional json config files to my project

appsettings.DEV.json
appsettings.QA.json

and loaded them in the Startup function based on the environment:

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, reloadOnChange: true);
    ...

And I understand how to change the environment: modify the value of the ASPNETCORE_ENVIRONMENT environment variable in project properties. However, there does not appear to be the ability to specify different environment variables depending on the configuration, the dropdownlist is labeled "N/A" and disabled.

The only option I see is to manually change the environment variable value, to change which appsettings are used. I'm sure there is a way to do it automatically, or else how would you ever use CI? (other than using a script to change the environment variable, there has to be an easier way).

The goal here is to setup automated builds and continuous integration for three environments: DEV, QA, and PROD. DEV and QA are on the same machine, so setting the environment variable that specifies the environment manually is not an option.

Project Properties : Debug section

James Wierzba
  • 16,176
  • 14
  • 79
  • 120
  • add a new profile by the profile dropdown or add one manually in launchsettings.json – Eric B Mar 28 '17 at 16:18
  • @EricB how do you associate the profile with a build configuration? Or if not a build configuration, how do you specify which profile is used when you build/publish? (My goal here is to set up automatic builds and continuous integration for different environments, therefore manually changing the environment or profile is not an option) – James Wierzba Mar 28 '17 at 16:29
  • then it sounds like you can just access the destination machines and set the environment variable manually, but it should be a one-time thing. – Eric B Mar 28 '17 at 16:40
  • @EricB This occurred to me however we host two different environments on the same machine (DEV and QA), and Production is on it's own machine. Therefore this solution won't work for DEV|QA – James Wierzba Mar 28 '17 at 16:43
  • in that case I think the easiest solution for you might be the [CommandLine](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration#commandline-configuration-provider) config provider – Eric B Mar 28 '17 at 16:51
  • @EricB Ok I'm reading those docs right now. Would you like to post a solution as an answer? if not, if I figure it out I'll post the answer myself for future readers – James Wierzba Mar 28 '17 at 16:57
  • 1
    I've never used that provider before, you're on your own now :P – Eric B Mar 28 '17 at 17:01

6 Answers6

24

Determine EnvironmentName from Build Type

For anybody that would like to set the EnvironmentName based on the build type, there is the handy .UseEnvironment(environmentName) on WebHostBuilder (found in Program Main).

As long as the appropriate compilation symbols are set against the build configurations in your project, you can do something like this to determine the EnvironmentName:

public static void Main(string[] args)
{
    string environmentName;
#if DEBUG
    environmentName = "Development";
#elif STAGING
    environmentName = "Staging";
#elif RELEASE
    environmentName = "Production";
#endif

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseEnvironment(environmentName)
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .Build();

    host.Run();
}
Carl Sharman
  • 4,435
  • 1
  • 30
  • 29
  • 2
    what is `#if DEBUG`? – Toolkit Aug 02 '19 at 11:01
  • 2
    @Toolkit #if is a pre-processor directive: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if. DEBUG is a predefined preprocessor symbol which is set when you check 'Define DEBUG constant' in the project. – Carl Sharman Aug 13 '19 at 13:26
20

If you are using the default code in Program.cs, you don't need to do anything beyond creating the two files in the project.

The default code in Program.cs looks like this:

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();

Here's what that's actually doing:

public static IWebHostBuilder CreateDefaultBuilder(string[] args)
{
    var builder = new WebHostBuilder();

    ...

    builder.ConfigureAppConfiguration((hostingContext, config) =>
    {
        var env = hostingContext.HostingEnvironment;

        config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
              .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
    });

    ...

    return builder;
}

env.EnvironmentName is set to the value of the ASPNETCORE_ENVIRONMENT environment variable, so all you have to do is create the appsettings.{ASPNETCORE_ENVIRONMENT}.json file and it will automatically get merged.

Additional note: to get the two files to actually merge, use this syntax:

var appSettings = Configuration.GetSection("AppSettings").Get<AppSettings>();

not:

var appSettings = new AppSettings();
Configuration.Bind("AppSettings", appSettings);
return appSettings;

The latter will not returned the merged data.

Thanks to Shawn Wildermuth for this.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Greg Gum
  • 33,478
  • 39
  • 162
  • 233
  • Of course, you need to add the environment variable at the destination machine, but I guess it goes without saying. – Mladen B. Oct 03 '19 at 08:28
  • 1
    True, but I typically only use this trick on my dev or test machine to get it to use my dev or test settings. I would not want to depend on an environmental variable on a prod machine. – Greg Gum Oct 03 '19 at 22:19
  • Would you care to explain why? I'm just curious? – Mladen B. Oct 04 '19 at 08:37
  • 5
    It just seems to me an environmental variable is just that - a variable, and there are too many ways it might get changed, or failed to get published etc. That seems a little scary to me in a prod environment. But that is just my own personal preference. – Greg Gum Oct 06 '19 at 22:16
  • Where are you getting the "AppSettings" class reference from? – Prisoner ZERO Aug 13 '22 at 23:11
  • 1
    @PrisonerZERO, It's a custom class that you write to match your appsettings.json file. Check out the example here: https://www.telerik.com/blogs/how-to-get-values-from-appsettings-json-in-net-core – Greg Gum Aug 15 '22 at 00:31
18

I've found a solution from Tsengs answer but wish to describe it here for clarity. The solution is found in the answer to another question however the question is quite different (and I've also expanded upon the answer) so I do not believe this question should be marked as a duplicate.

The answer is here

The solution is to setup different environment variable values on each IIS site for the key ASPNETCORE_ENVIRONMENT

The steps to do so are:

  1. Go to your application in IIS and choose Configuration Editor.
  2. Select Configuration Editor
  3. Choose system.webServer/aspNetCore (RC2 and RTM) or system.webServer/httpPlatform (RC1) in Section combobox
  4. Choose Applicationhost.config ... in From combobox.
  5. Click on enviromentVariables element and open edit window.
  6. Set your environment variables.
  7. Close the window and click Apply.
  8. Done

Alternatively, you can modify your applicationHost.config file (normally located at C:\Windows\System32\inetsrv\config\applicationHost.config

And add the following entry under the root <Configuration> tag, where "my-iis-site" is the name of your IIS site.

<location path="my-iis-site">
    <system.webServer>
        <aspNetCore>
            <environmentVariables>
                <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="DEV" />
            </environmentVariables>
        </aspNetCore>
    </system.webServer>
</location>
David Gardiner
  • 16,892
  • 20
  • 80
  • 117
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
2

The dialog you linked in the picture is only to configure "launchSettings.json". This file is not used by your application.

It is only used by Visual Studio to set the environment and open an URL in the browser when you hit F5 and nothing else.

When you want to switch environments, you need to setup an environment variable before launching. How to do this, depends on your environment.

Windows (Commandline, cmd.exe)

setx ASPNETCORE_ENVIRONMENT "Development"

Windows (Powershell)

$Env:ASPNETCORE_ENVIRONMENT = "Development"

Linux

export ASPNETCORE_ENVIRONMENT="Development"

Linux (Set it for a single command)

ASPNETCORE_ENVIRONMENT="Development" dotnet run

Update in regards to the comment

Yes it is machine specific (except for Linux, which you can do per command). However, in IIS you can do that too either via different app pools or by following this answers instructions to add it to IIS

Community
  • 1
  • 1
Tseng
  • 61,549
  • 15
  • 193
  • 205
  • When you say "setup an environment variable before launching", do you mean as a startup script of some sort? And this variable will be machine specific of course, so how would this solve my problem of having two environments hosted on the same physical machine, in two different IIS sites? – James Wierzba Mar 28 '17 at 17:13
  • Well, it's called "Environment" for a reason. Yes it is machine specific (except for Linux, which you can do per command). However, in IIS you can do that too iirc, but you need different app pools, but I **believe** it involves assigning a profile to the app pool and set the environment variable in that users profile – Tseng Mar 28 '17 at 17:16
  • 1
    Or check out this answer http://stackoverflow.com/a/36836533/455493. Your admin guys should know how to do that anyways, it's their job ;) – Tseng Mar 28 '17 at 17:17
  • 1
    What if we don't have access to the environment variables on the web servers? – pabrams Feb 07 '20 at 20:01
0

If you are using azure add ASPNETCORE_ENVIRONMENT with corresponding value to Application Settings in side of webapp, which will then pick up correct appsettings.{value}.json file

sensei
  • 7,044
  • 10
  • 57
  • 125
  • Why would we want to do that? Then it's no longer a configuration value, but has to be checked in as code. – pabrams Feb 07 '20 at 20:00
0

We are using the following appsettings.json files:

appsettings.json
appsettings.Community.json

Since we are using Blazor WebAssembly we have these files both in the Server project and Client wwwroot. In order for the transformation to take place in Azure App Service we have a Application Setting under Configuration that sets ASPNETCORE_ENVIRONMENT to Community.

enter image description here

To get appsettings.json in a Class that can be used via dependency injection code looks like this:

appsettings.json:

{
  "UiBaseUrl": "https://example.com",
  "JiraIntegrationSettings": {
    "UseJira": false,
    }
}

AppSettings.cs:

public class AppSettings
{
    public string UiBaseUrl { get; set; }

    public JiraIntegrationSettings JiraIntegrationSettings { get; set; }
}

public class JiraIntegrationSettings
{
    public bool UseJira { get; set; }
    
}

Startup.cs:

var settings = new AppSettings();
Configuration.Bind(settings);
services.AddSingleton(settings);

Program.cs:

var settings = new ClientAppSettings();
builder.Configuration.Bind(settings);
builder.Services.AddSingleton(settings);

This can then be injected into any class or Controller like this example:

public ProductsController(ExtendedApplicationDbContext extendedApplicationDbContext, AppSettings settings, ILogger<ProductsController> logger)
{
    _extendedApplicationDbContext = extendedApplicationDbContext;
    _settings = settings;
    _logger = logger;
}

Blazor front end injection looks like this:

@inject ClientAppSettings ClientAppSettings
Ogglas
  • 62,132
  • 37
  • 328
  • 418