Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

Configuring different environments #192

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

efleming18
Copy link
Contributor

Still working on the last bullet, but wouldn't hurt to get eyes on this sooner rather than later.

@ardalis
Copy link
Contributor

ardalis commented Feb 27, 2017

How's this coming? Waiting on me for anything?

@efleming18
Copy link
Contributor Author

@ardalis sorry, last week got a little crazy. I'm not sure what you want for the last bullet point if you want to tackle that? Or Slack me some more info and I can try to knock it out today.

The rest of it is ready for review though.

@@ -0,0 +1,153 @@
# Configuring Different Environments
by [Eric Fleming](https://ericflemingblog.wordpress.com)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point this at http://deviq.com/me/eric-fleming/ which we can update however you want.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops! Updated

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear what impact the order in which they're loaded has. Update to make clear that the "last setting loaded" wins if a setting exists in multiple places. You can look at the docs for an example of how to phrase this, or come up with something that works for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
```

where `{env.EnvironmentName}` corresponds to 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"corresponds to the value of the environment variable"


and should be set to `Development` by default.

Environment variables will be updated throughout the environments; for example, on Staging the environment variable will likely be set to `Staging`, and for production it will likely be set to `Production`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this is saying. "will be updated" by whom?
Is this meant to be saying they can be configured for each environment in which the application may be deployed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Setting the current environment for macOS can be done in-line when running the application;

```bash
ASPNETCORE_ENVIRONMENT=Development dotnet run
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is neat; didn't realize you could do this.


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.

```powershell
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you formatting this as powershell?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure what else to format it as :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm, I see ....


#### Command Line
```powershell
setx ASPNETCORE_ENVIRONMENT "Development"
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@ardalis
Copy link
Contributor

ardalis commented Apr 6, 2017

LMK when you think you can get to the changes requested. I'd like to merge this PR and then make a separate one to add the last bullet's info.

@efleming18
Copy link
Contributor Author

@ardalis I've addressed all but 1 comment. Just let me know which you would rather me use.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants