Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unprefixed Hosting Environment Variable is ignored #55379

Closed
1 task done
astorDev opened this issue Apr 26, 2024 · 4 comments
Closed
1 task done

Unprefixed Hosting Environment Variable is ignored #55379

astorDev opened this issue Apr 26, 2024 · 4 comments
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Comments

@astorDev
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Unprefixed hosting environment variable (ENVIRONMENT) does not affect the value of IHostEnvironment.EnvironmentName.

Expected Behavior

Setting ENVIRONMENT=X should set IHostEnvironment.EnvironmentName with precedence over ASPNETCORE_ENVIRONMENT and DOTNET_ENVIRONMENT. In the same fashion in which it's applied to any other configuration value.

Steps To Reproduce

repo: https://github.com/astorDev/hosting-env-unprefixed-variable-repro

Simple steps:

  1. Create a new web project
dotnet new web
  1. Run it setting "ENVIRONMENT" environment variable
export ENVIRONMENT=Repro && dotnet run
  1. Search the logs for Hosting environment:
Expected Actual
Hosting environment: Repro Hosting environment: Development

Exceptions (if any)

No response

.NET Version

8.0.101

Anything else?

I was leaning toward assuming that the variable is ignored since it's overridden by launchSettings. But first, deleting launchSettings doesn't make a difference in the problem (only changing Development to Production). And for other variables unprefixed variables take precedence over variables prefixed with ASPNETCORE_

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Apr 26, 2024
@davidfowl
Copy link
Member

davidfowl commented Apr 26, 2024

Why should this work? Is there some reason you can’t use the ASPNETCORE or DOTNET prefix?

@astorDev
Copy link
Author

I expected it to work because it would be consistent with the way all other environment variables are read.

This variable seemed like a logical choice for experimenting with environment variables and ASP.NET Core and (at least for me) experimenting brought confusion as to whether unprefixed variables are used in general. While I can certainly use a prefixed variable, I wanted to at least figure out if this decision about the ENVIRONMENT variable was intentional.

@davidfowl
Copy link
Member

It was intentional. You can set and read any environment variable via IConfiguration, but IHostingEnvironment is only impacted by ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT intentionally.

@astorDev
Copy link
Author

@davidfowl thank you for the clarification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

No branches or pull requests

2 participants