Deploy different connection string for each environment

8.6k views Asked by At

I'm trying to deploy an asp.net core 2.0 api project on IIS from a mac.

What I'd like to do is set up different appsettings.json files for my development, staging and production environments, and then call the different environment with dotnet -build as part of my deployment scripts.

I've looked at https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments which is for an older version of .net core and I can't get my head around what I need to do. Rather than setting the environment for the OS, I'd like to set it programatically (as my staging and production environments are for the same server)

I have an appsettings.Development.json file which is used when I run my app, but I can't seem to get my appsettings.Production.json file loaded when simply setting the environment variable as part of the build command.

bash$ ASPNETCORE_ENVIRONMENT=Production dotnet run
Using launch settings from /Properties/launchSettings.json...
Hosting environment: Development
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

Ultimately I'm trying to deploy a specific connection string depending on the environment that I build for. Is there a better way to do this?

Update

@Chris's answer below helped, in addition I found the following:

Per IIS Application Pool If you need to set environment variables for individual apps running in isolated Application Pools (supported on IIS 10.0+), see the AppCmd.exe command section of the Environment Variables topic in the IIS reference documentation.

Which let me set up different environments per app pool

1

There are 1 answers

11
Chris Pratt On BEST ANSWER

The documentation is not for an older version of ASP.NET Core. Everything there still applies, and it's all laid out, so I'm not sure exactly where the confusion is coming from here.

In the simplest form, you just create one or more appsettings.{environment}.json files. By default, appsettings.json is loaded, first, and then, if it exists, the appsettings.{environment}.json file which matches the current environment is loaded. This allows you to override settings from the main appsettings.json file, specifically for the environment. However, the better approach is to only put global settings that aren't affected by environment in appsettings.json and then leave all your environment-specific settings to the environment-specific files.

Regardless, in either case, when you deploy your application all of the settings files are copied. ASP.NET Core projects are not published based on a particular configuration like older ASP.NET projects were. This means the same published files can be deployed in multiple different environments without having to republish.

The environment to use at runtime is determined by the value of the ASPNETCORE_ENVIRONMENT environment variable. Just set this on the server to what it should be, and you're good to go.

If you're running behind IIS, you need to take one more step. By default, the App Pool doesn't load in environment variables. You can change this by editing the advanced properties of the App Pool in IIS and changing the setting "Load User Profile" to true.