Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AZURE_FUNCTIONS_ENVIRONMENT vs ASPNETCORE_ENVIRONMENT

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.

like image 637
Turbo Avatar asked Jun 06 '19 19:06

Turbo


People also ask

What is the difference between aspnetcore_environment and @Azure_Functions_environment?

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.

What is the aspnetcore_environment environment variable?

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.

Is it possible to set environment variables in Azure Functions?

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.

What is runtime environment in ASP NET Core?

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.


2 Answers

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 the AZURE_FUNCTIONS_ENVIRONMENT app setting (as EnvironmentSettingNames.EnvironmentNameKey) and passes it to the IWebHostBuilder. Using only ASPNETCORE_ENVIRONMENT can lead to desired behavior changes and telemetry being missed.

like image 157
Turbo Avatar answered Nov 14 '22 22:11

Turbo


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").

like image 24
alsami Avatar answered Nov 14 '22 21:11

alsami