In azure functions (v2, c#), there are two environment variables that can be potentially used to identify the name of the current environment.
AZURE_FUNCTIONS_ENVIRONMENT
ASPNETCORE_ENVIRONMENT
I am planning to use AZURE_FUNCTIONS_ENVIRONMENT
, and I am wondering if there are reasons to choose one over another?
In terms of behavior of the two, this is what I discovered:
AZURE_FUNCTIONS_ENVIRONMENT
is set to Development
locally by the functions host/runtime. It is not automatically set in azure to Production
. One can set this in App Settings in azure.ASPNETCORE_ENVIRONMENT
is not set by the functions host/runtime either locally or in Azure.I have also raised a github issue about this a couple of weeks ago, but got no response. I am hoping I might get an answer here.
AZURE_FUNCTIONS_ENVIRONMENT is set to Development locally by the functions host/runtime. It is not automatically set in azure to Production. One can set this in App Settings in azure. ASPNETCORE_ENVIRONMENT is not set by the functions host/runtime either locally or in Azure.
When the ASPNETCORE_ENVIRONMENT environment variable is set for an app pool, its value overrides a setting at the system level. Execute net stop was /y followed by net start w3svc from a command prompt.
This variable is popular in dotnet core web apps, but it is not mentioned in official docs in Azure functions (I am not sure why). If you write a for loop and output all the environment variables to console from within a function, you will find that this variable is not set by default - neither in production, nor when running in Visual Studio.
ASP.NET Core configures app behavior based on the runtime environment using an environment variable. To determine the runtime environment, ASP.NET Core reads from the following environment variables: ASPNETCORE_ENVIRONMENT when the WebApplication.CreateBuilder method is called.
Adding the official answer from github issue here for everyone's benefit:
You'll want to use
AZURE_FUNCTIONS_ENVIRONMENT
. The Functions runtime that powers a Function app on Azure is the WebHost project in this repo. As the host is initializing, it looks for theAZURE_FUNCTIONS_ENVIRONMENT
app setting (asEnvironmentSettingNames.EnvironmentNameKey
) and passes it to the IWebHostBuilder. Using onlyASPNETCORE_ENVIRONMENT
can lead to desired behavior changes and telemetry being missed.
ASPNETCORE_ENVIRONMENT
is the default environment-variable to determine the environment for IHostingEnvironment
. IHostingEnvironment
has current two implementations. One can be found here and is only supposed to be used internally. The other can be found here.
The exact idea on why there is AZURE_FUNCTIONS_ENVIRONMENT
I cannot tell. I would suggest you to stick to IHostingEnvironment
for ASP.NET Core applications. The version of IHostingEnvironment
will be replaced with IWebHostEnvironment
in the future. With the release of 3.0 they will continue to support both until the remove it. It will be marked as obsolete.
In your functions you can always set custom variables and just access them via Environment.GetEnvironmentVariable("MY-VAR")
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With