1

I look at several topics e.g. Automatically set appsettings.json for dev and release environments in asp.net core? and see that I can create appsettings.{Environment}.json in .NET Core apps and set the environment in launchSettings.json.

However, I am wondering what should I do to set this environment properly while publishing my app to IIS or Docker container. Before each publishing, should I change the environmentVariables parameter to Prod and then update it to Development after publish? I do not think so, but I could not find a suitable page explaining this. Could you please clarify me about this issue?

  • 1
    You need to set the environment variable on the respective machine and asp.net will read from the respective json based on environment variable – Vivek Nuna Feb 27 '21 at 18:11
  • 1
    `launchSettings.json` is [only used](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-5.0#development-and-launchsettingsjson) on the local development machine. – Guru Stron Feb 27 '21 at 18:24
  • Thanks a lot both of you. Then as far as I understood, in the development machine, I can easily switch between PROD and DEV using `launchSettings.json`. But on the server machine, I should set the environment to PROD and then it automatically use the `appsettings.Production.json` file (respective json file). Is that all true? –  Feb 27 '21 at 18:29
  • 1
    @Maria yes, on the local machine you can switch environments using this file. – Guru Stron Feb 27 '21 at 18:34
  • @GuruStron What about the other question related to the server machine? –  Feb 27 '21 at 18:37

1 Answers1

2

First of all as docs state: launchSettings.json is only used on the local development machine.

As for using configuration file - by default next files are used:

  • appsettings.json using the JSON configuration provider.
  • appsettings.Environment.json using the JSON configuration provider. For example, appsettings.Production.json and appsettings.Development.json.

The environment for ASP.NET Core app determined next way:

To determine the runtime environment, ASP.NET Core reads from the following environment variables:

  • DOTNET_ENVIRONMENT
  • ASPNETCORE_ENVIRONMENT when ConfigureWebHostDefaults is called. The default ASP.NET Core web app templates call ConfigureWebHostDefaults. The ASPNETCORE_ENVIRONMENT value overrides DOTNET_ENVIRONMENT.

So the usual approach is to set ASPNETCORE_ENVIRONMENT environment variable to needed value on target host (machine, docker container, etc...)

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    **1.** I think I can set the `ASPNETCORE_ENVIRONMENT` via cmd `set ASPNETCORE_ENVIRONMENT=Production`, etc. Is that true? –  Feb 27 '21 at 18:42
  • **2.** I have read using `ASPNETCORE_ENVIRONMENT` is better than `DOTNET_ENVIRONMENT` as it also supports Console apps. I am confused, which one should I prefer and why? –  Feb 27 '21 at 18:43
  • @Maria as written in the answer `ASPNETCORE_ENVIRONMENT` if present overrides `DOTNET_ENVIRONMENT`, so I use the former one in my projects. As for setting environment variable on windows see [this](https://superuser.com/a/79614) answer. – Guru Stron Feb 27 '21 at 18:57
  • @Maria also it seems that it can be set in [IIS](https://stackoverflow.com/a/36836533/2501279) – Guru Stron Feb 27 '21 at 18:59
  • Thanks a lot again for all your useful helps. I think I should use `ASPNETCORE_ENVIRONMENT` but not fully understand the difference exactly with `DOTNET_ENVIRONMENT` :) –  Feb 27 '21 at 19:05
  • Just a comment on the comments left by @Maria and the OP - it is my understanding that the DOTNET_ENVIRONMENT is overridden by ASPNETCORE_ENVIRONMENT as Maria stated, but if you have an .NET Core application that is not an ASP.NET Core application (e.g., a .NET Console application) you would need the DOTNET_ENVIRONMENT value to be explicitly set or the application will look instead for a commandline argument (if present), an appsettings.production.json file (if present), or it would just use the appsettings.json file by default. – jlavallet Jul 08 '22 at 20:32
  • https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-6.0 – jlavallet Jul 08 '22 at 21:00
  • Also see https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0 – jlavallet Jul 08 '22 at 21:47