Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with environment differences when deploying asp.net core application?

Is there a way to change the environment settings when deploying ASP.NET Core application (like config file transformations using debug/release build)?

What would be the best approach for maintaining multiple environment settings in .NET Core applications (something similar to <appSettings file="local.config"> for local, staging and production)?

like image 451
Soma Yarlagadda Avatar asked Jun 17 '16 05:06

Soma Yarlagadda


People also ask

How do you set an environment variable in the NET Core console app?

Set Your Local Environment Variable In Visual Studio, right-click on your console application project and go to properties. Click on the 'Debug' option in the left navigation. Add a new environment variable called ASPNETCORE_ENVIRONMENT and set the value to Development.

Is ASP.NET Core configuration system can be environment specific?

The configuration system in ASP.NET Core is built up of layers of configuration values, compiled from multiple sources. You can load values from JSON files, XML files, environment variables, or you can create a custom provider to load values from pretty much anywhere.


1 Answers

The central configuration file is the appsettings.json and you can have multiple files, like appsettings.Production.json etc, which will be loaded and override settings from the appsettings.json.

For example

        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .SetBasePath(hostEnv.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{hostEnv.EnvironmentName}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();

All you need to get this working is the environment variable for setting the environment type (see documentation here).

You can also have environment variables that override, if you add AddEnvironmentVariables() to your configuration builder. So if you have a appsettings.json of

{
    "Data"  {
         "Default" {
              "ConnectionString" : "..."
         }
    }
}

and want to override that via environment Variable, you'd set up an environment variable called "Data:Default:ConnectionString" and it's value will override the settings in the appsettings.config and appsettings.Production.config (assuming your .AddEnvironmentalVariables() is called after .AddJsonFile() - Last registration with the matching key wins) with the value from the environment variable.

You can find more in the official documentation here.

Update

Since in the comments some understand this as the only way to set the environment, there are many ways to set environment variable (most of it is documented in Use multiple environments in ASP.NET Core), all ultimately boiling down to being an environment variable, just within a different scope:

  1. Environment Variable (globally, Windows cmd.exe set ASPNETCORE_ENVIRONMENT=Development or $Env:ASPNETCORE_ENVIRONMENT = "Development" on powershell, export ASPNETCORE_ENVIRONMENT = Development on linux)
  2. Per command environment variable (i.e. linux: ASPNETCORE_ENVIRONMENT=Production dotnet MyApp.dll)
  3. Docker container, i.e. via docker-compose.yaml

    web:
        environment:
        - ASPNETCORE_ENVIRONMENT=Debugging
    
  4. Docker container via command line docker run -e ASPNETCORE_ENVIRONMENT=Debugging
  5. in IIS via web.config.

    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="true" >
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
    
  6. On IIS set it per AppPool (see here)
  7. On Linux via service definition files (see docs)
  8. Azure App Service via Environment variables, can be set per slot and having different slots for Staging, Development, Production and i.e. deploying to staging, doing warm up and swapping with Production
  9. Per execution via dotnet run --launch-profile Development

They all change/set the environment variable in a specific scope (globally, locally to a container, inside the app pool, per execution etc.). Choose one which suits your needs.

like image 154
Tseng Avatar answered Sep 19 '22 17:09

Tseng