12

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

6
  • 1
    show how you deal with appsettings.{environment}.json files in code Commented Dec 1, 2017 at 11:48
  • @Set is completely correct. You want an appsettings.production.json for production, an appsettings.staging.json for staging and appsettings.development.json for development. One thing to remember is that if no other appsettings files are found, then appsettings.json is used. Commented Dec 1, 2017 at 11:50
  • 1
    @JamieTaylor: appsettings.json is always used. The environment-specific files simply override. Commented Dec 1, 2017 at 13:38
  • @ChrisPratt Indeed. Commented Dec 1, 2017 at 13:43
  • I understand now about the enviornment specific appsettings files, but need to find a way to set the environment. I can't set the environment vairable on the server as the server runs both staging and production Commented Dec 1, 2017 at 20:21

1 Answer 1

17

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.

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

11 Comments

Yes, sorry. I've updated it in my answer. As for the second question, I'm not sure what you're referring to exactly. However, in one sense, it doesn't matter. You can use either. The App Pool setting is just oddly named. It will in fact pull in system environment variables just as well.
That's interesting. I, too, always set the environment in the system variables, but I've always needed to change that setting on the App Pool. It's possible it might be a version-specific inconsistency with either IIS, Windows Server, or both. My org is currently using 2012 R2 Datacenter servers, which of course come with IIS 8.5.
Why in the name of all things good and right in the world would you do that? The same server should never house multiple environments. You're just asking for trouble.
Because I have a single server that runs the API's for a number of different sites, some staging, some production. I'd rather the answer not be change 1 server to 7!
@ChrisPratt Trouble is asking my boss for another $200 a month to run a new AzureVM to set one environmental variable differently ;-) Not all developers have infinite resources (or time for stringent staging releases).
|

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.