This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
Configuring different environments #192
Open
efleming18
wants to merge
5
commits into
master
Choose a base branch
from
efleming18/configuring-different-environments
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
94b64ef
Creating the article
efleming18 2ab21c7
WIP - switching computers
efleming18 1a6ce13
Content for major bullet points
efleming18 03d7969
Adding images, adjusting formatting, and wording
efleming18 55ca05f
Reacting to feedback
efleming18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
content/asp.net/getting-started/configuring-different-environments.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# Configuring Different Environments | ||
by [Eric Fleming](http://deviq.com/me/eric-fleming/) | ||
|
||
In ASP.NET Core controlling application behavior across multiple environments, such as developement, staging, and production, has been improved through the expanded use of environment variables. Environment variables are used to indicate which environment the application is running in, and can be detected programmatically allowing the application to be configured appropriately. | ||
|
||
## Environment Based Settings Files | ||
The constructor of the Startup class, found below, provides the ability to use more than one appsettings.json file by leveraging these environment variables. Since the `appsettings` configurations are [read in the order they are specified](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration), the general `appsettings` configuration will be loaded first, followed by an environment specific `appsettings` configuration. Keep in mind, if a setting exists in multiple settings files the **last settings file loaded will win**. | ||
|
||
```c# | ||
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) | ||
.AddEnvironmentVariables(); | ||
Configuration = builder.Build(); | ||
} | ||
``` | ||
|
||
The following loads the environment specific `appsettings.json` configuration; | ||
|
||
```c# | ||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) | ||
``` | ||
|
||
where `{env.EnvironmentName}` corresponds to the value of the environment variable `ASPNETCORE_ENVIRONMENT`. When using Visual Studio, the value for the `ASPNETCORE_ENVIRONMENT` variable can be found in your project's debug profiles show below: | ||
|
||
![Project properties debug](images/project-properties-debug.png) | ||
|
||
and should be set to `Development` by default. | ||
|
||
Environment variables can be configured for each environment in which the application may be deployed; for example, on Staging the environment variable will likely be set to `Staging`, and for production it will likely be set to `Production`. | ||
|
||
> Note: On Windows or macOS, environment names are not case sensitive meaning `DEVELOPMENT`, `Development` and `development` will all provide you the same result. On Linux, since it is a case sensitive OS by default, assuming case sensitivity for environment variables is encouraged. | ||
|
||
## Developer Error Page | ||
In order to easily interact with specific environments, ASP.NET Core has provided the `IHostingEnvironment` abstraction which can be injected into the startup logic via [Dependency Injection](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection). For example, in the constructor above, the `IHostingEnvironment` is used to access the `EnvironmentName` when loading configuration files. | ||
|
||
In order to use this `IHostingEnvironment` for displaying the developer error page only in the `Development` environment, add the following to the `Configure` method of the `Startup` class. | ||
|
||
```c# | ||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
app.UseBrowserLink(); | ||
} | ||
else | ||
{ | ||
app.UseExceptionHandler("/Home/Error"); | ||
} | ||
``` | ||
|
||
In the above, the `IHostingEnvironment` provides a method `IsDevelopment` which checks the `ASPNETCORE_ENVIRONMENT` variable for the value `Development`. If this is true the developer error page, which should typically not be run in a production environment, will be used to display errors. If it is any other environment, the standard error handling page will be used. | ||
|
||
## Use launchSettings to Set Environment in Visual Studio | ||
The `Properties` folder in Visual Studio contains the `launchSettings.json` file. The `launchSettings.json` holds settings specific to each profile Visual Studio is configured to use to launch the application, including any environment variables that should be used. For the development environment in Visual Studio, the `IIS Express` profile will be used. This profile also specifies to use the `ASPNETCORE_ENVIRONMENT` variable with a value of `Development`. | ||
|
||
```c# | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:62101/", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Adding profiles can be done by adding them to the `launchSettings.json` file. For example, adding a new profile for the Staging environment with a different value for the `ASPNETCORE_ENVIRONMENT` variable will look like the following: | ||
|
||
```c# | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:62101/", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express (Staging)": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Staging" | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
There are now two profiles, `IIS Express` and `IIS Express (Staging)`, which can be used to launch the application with their respective environment variables. | ||
|
||
> Note: Changes made to project profiles or to launchSettings.json directly may not take effect until the web server used is restarted (in particular, Kestrel must be restarted before it will detect changes made to its environment). | ||
Warning: Environment variables stored in launchSettings.json are not secured in any way and will be part of the source code repository for your project, if you use one. Never store credentials or other secret data in this file. If you need a place to store such data, use the Secret Manager tool described in [Safe storage of app secrets during development](https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets#security-app-secrets). | ||
|
||
## Manually Set Environment Variables in Windows | ||
To set the `ASPNETCORE_ENVIRONMENT` for the current session, if the app is started using `dotnet run`, the following commands are used | ||
|
||
#### Command Line | ||
```powershell | ||
setx ASPNETCORE_ENVIRONMENT "Development" | ||
``` | ||
|
||
```powershell | ||
$Env:ASPNETCORE_ENVIRONMENT = "Development" | ||
``` | ||
|
||
These commands take effect only for the current window. When the window is closed, the `ASPNETCORE_ENVIRONMENT` setting reverts to the default setting or machine value. In order to set the value globally on Windows open the Control Panel > System > Advanced system settings and add or edit the `ASPNETCORE_ENVIRONMENT` value. | ||
|
||
![System Setting Environemtn](images/systemsetting_environment.png) | ||
|
||
![Windows AspNet Core Environment](windows_aspnetcore_environment.png) | ||
|
||
## Manually Set Environment Variables in macOS | ||
Setting the current environment for macOS can be done in-line when running the application; | ||
|
||
```bash | ||
ASPNETCORE_ENVIRONMENT=Development dotnet run | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is neat; didn't realize you could do this. |
||
``` | ||
or using export to set it prior to running the app. | ||
|
||
```bash | ||
export ASPNETCORE_ENVIRONMENT=Development | ||
``` | ||
|
||
Machine level environment variables are set in the .bashrc or .bash_profile file. Edit the file using any text editor and add the following statment. | ||
|
||
```bash | ||
export ASPNETCORE_ENVIRONMENT=Development | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.8 KB
content/asp.net/getting-started/images/windows_aspnetcore_environment.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setx? what about set? Is setx different? I've always used just set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've always thought set took effect in the local shell you have but you would lose the environment variable once you close the prompt.
Setx on the other hand makes the change permanent but you'll have to open a new shell to see the change take place.