-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Generic Host Builder and HostingEnvironment.IsDevelopment #4150
Comments
From @dazinator on July 5, 2018 17:11 This seems like a sensible workaround, but it would be better if this behaviour was automatic to be consistent with asp.net core scenarios?
|
Tentatively putting this in 3.0. pinging @davidfowl @Tratcher for their thoughts. |
ASPNETCORE_ENVIRONMENT isn't expected to work, as you say, this isn't ASP.NET Core land. GenericHost does not register any configuration providers by default, not even environment variables. |
@Tratcher
If this API was not present, I wouldn't have raised an issue. On Dev machines, this always returned true by default until now so that might catch people out. |
To be practical here we might just have to recognize ASPNETCORE_ENVIRONMENT in the generic host. |
I meant that no environment variables are recognized by default, that's opt-in. IsDevelopment doesn't check ASPNETCORE_ENVIRONMENT, it checks config["environment"], and the environment variable config provider deals with prefixes like ASPNETCORE_. That's all under your control setting up the HostBuilder. We'll see what defaults we decide to bring back, or if we mirror the CreateDefaultBuilder pattern to get something bootstrapped for you. |
@Tratcher got ya. Ok thanks. |
I've just been having issues with this and i noticed that the environment is not set from ConfigureAppConfiguration but from ConfigureHostConfiguration on the HostBuilder |
From a user that dont already read the docs i did use some time here to find out that the enviromentname is defaulted to production ( also causing some pain to find out it altered production environment). I think it would have been a good idea to add environment variables as default to hostconfiguration :) I just added to my solution
|
@davidfowl @Tratcher Are we doing something here for 3.0? |
Yes this needs design. |
There's a PR updating the docs around this: In 3.0 we've added Host.CreateDefaultBuilder that adds unprefixed environment variable support, so generic host apps are expected to use ENVIRONMENT=Development. The 3.0 web host integration for generic host adds ASPNETCORE_ prefixed environment variables so web apps can use either the generic ENVIRONMENT or the web specific ASPNETCORE_ENVIRONMENT. The only remaining question here is if Host.CreateDefaultBuilder should use a prefix when adding environment variables? @glennc? |
Proposal: "DOTNET_" |
I think we should support both |
@davidfowl Which one overrides the other, in case both are specified? |
In that odd case DOTNET wins. |
Adding the web hosted service already adds |
Ah yes maybe that's enough then? If you get the IHostingEnvironment from hosting after calling ConfigureWebHost does it report IsDevelopment() true then? |
Yes |
Lets write a test for that case to make sure it's covered. The original plan sounds fine in that case. It does mean in the new worker template the DOTNET_ENVIRONMENT flag should be set instead of ASP.NET Core environment. We should also determine if we want to chose the new web templates to use the DOTNET_ENVIRONMENT in launch settings. cc @glennc |
In a generic host in dotnet core 2.2 you can just use new HostBuilder()
.UseEnvironment(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production") to set the environment the same way as you would with the aspnet core app. |
im my environment Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") always return null and hostContext.HostingEnvironment.IsDevelopment() ; always return false. What are other option to fix the problem? |
@jdang67 some options:
|
From @dazinator on July 5, 2018 17:7
I am using the new generic host in 2.1.0 as documented here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.1
When the hosting environment is development, I'd like to add user secrets:
However
IsDevelopment
always returns false.Yet I have set the
ASPNETCORE_ENVIRONMENT
enironment variable. Here is my launch settings:It would make sense if
ASPNETCORE_ENVIRONMENT
was no longer used to control this for a generic host as we are not in asp.net core land - but in that case is there now a different environment variable name for this, or do we set the Environment name ourselves in code?Copied from original issue: #3298
The text was updated successfully, but these errors were encountered: