_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:
.ConfigureAppConfiguration((hostContext, configApp) =>
{
configApp.AddJsonFile("appsettings.json", optional: true);
if (hostContext.HostingEnvironment.IsDevelopment())
{
configApp.AddUserSecrets<Program>();
}
configApp.AddEnvironmentVariables(prefix: "FOO_");
configApp.AddCommandLine(args);
})
However IsDevelopment
always returns false.
Yet I have set the ASPNETCORE_ENVIRONMENT
enironment variable. Here is my launch settings:
{
"profiles": {
"Foo": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
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: aspnet/Home#3298_
_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?
.ConfigureAppConfiguration((hostContext, configApp) =>
{
hostContext.HostingEnvironment.EnvironmentName = System.Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT");
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.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.1#host-configuration
https://github.com/aspnet/Hosting/blob/c9a4ee8b719ffb01eb44705d68efd158eb80c221/src/Microsoft.Extensions.Hosting/HostBuilder.cs#L121
https://github.com/aspnet/Hosting/blob/f9d145887773e0c650e66165e0c61886153bcc0b/src/Microsoft.Extensions.Hosting/HostBuilder.cs#L134
@Tratcher
If its not expected to work the same, having the IsDevelopment()
API exposed still is a little misleading. Especially for those with asp.net core experience, they will see this API (like I did) and expect to be able to use it in the same way.
if (hostContext.HostingEnvironment.IsDevelopment())
{
}
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 :)
and use a fallback on aspnetcore_environment.
I just added to my solution
.ConfigureHostConfiguration((configurationBuilder)=>{
configurationBuilder
.AddInMemoryCollection(new[] { new KeyValuePair<string, string>(HostDefaults.EnvironmentKey, System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")) })
.AddEnvironmentVariables();
})
@davidfowl @Tratcher Are we doing something here for 3.0?
Yes this needs design.
There's a PR updating the docs around this:
https://github.com/aspnet/Docs/pull/10531
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 DOTNET_
and ASPNETCORE_
@davidfowl Which one overrides the other, in case both are specified?
In that odd case DOTNET wins.
Adding the web hosted service already adds ASPNETCORE_
and overwrites the prior one.
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
```c#
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
always return null and
``` c#
hostContext.HostingEnvironment.IsDevelopment() ;
always return false. What are other option to fix the problem?
@jdang67 some options:
Most helpful comment
In a generic host in dotnet core 2.2 you can just use
to set the environment the same way as you would with the aspnet core app.