Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.92k stars 442 forks source link

AZURE_FUNCTIONS_ENVIRONMENT value not reflect on Windows Plan #6978

Open komayama opened 3 years ago

komayama commented 3 years ago

AZURE_FUNCTIONS_ENVIRONMENT value not reflect in Windows Plan

I create code that call IHostEnvironment interface IsDevelopment method for switching Development and Production environment when use the following the Environment as a application setting in Azure Functions on Windows consumption, AppService, Premium Plan.

https://docs.microsoft.com/en-us/azure/azure-functions/functions-app-settings#azure_functions_environment This environment default value in Azure Functions is `True` when call IsDevelopment method. But even set AZURE_FUNCTIONS_ENVIRONMENT=Production, Azure Functions return to `True`. ~~I change to Windows consumption plan to Premier Plan. Then IsDevelopment method returned to `False`.~~ I created new App Service Plan and Premium Plan. Deploy code to each app service plan and set AZURE_FUNCTIONS_ENVIRONMENT=Production. but It was not displayed `False`. #### Investigative information Please provide the following: - Timestamp: ongoing ~ - Function App version: 3.0.14916.0 - Function App name: ishostenvironment - Function name(s) (as appropriate): Function 1 - Invocation ID: All - Region: Japan East #### Repro steps 1. Deploy to Azure Functions bellow source code. 2. Set the application setting ![スクリーンショット 2020-12-10 225918](https://user-images.githubusercontent.com/24888431/101781603-5def3880-3b3b-11eb-8197-a46adaa3b387.png) 3. Check the result which type return bool variable in Azure Functions monitor. ![image](https://user-images.githubusercontent.com/24888431/101781849-ab6ba580-3b3b-11eb-92c9-77f2eb71bd1d.png) #### Expected behavior - IsDevelopment() value is `False`, when set AZURE_FUNCTIONS_ENVIRONMENT=Production #### Actual behavior - Even set AZURE_FUNCTIONS_ENVIRONMENT=Production but IsDevelopment() value is `True`. ![スクリーンショット 2020-12-10 224500](https://user-images.githubusercontent.com/24888431/101780077-6777a100-3b39-11eb-8f43-2eba28bd20fd.png) #### Related information * Programming language used : C# * Bindings used : Timer ```csharp public class Function1 { private readonly IHostEnvironment _hostEnvironment; private readonly IConfiguration _configuration; public Function1(IHostEnvironment hostEnvironment, IConfiguration configuration) { _hostEnvironment = hostEnvironment ?? throw new ArgumentNullException(nameof(hostEnvironment)); _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); } [FunctionName("Function1")] public void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log) { log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); log.LogInformation($"_hostEnvironment.IsDevelopment(): {_hostEnvironment.IsDevelopment()}"); } } ```
kashimiz commented 3 years ago

I was able to reproduce this bug on two separate Function apps running host version 3.0.15185.0 in Azure (Central US.) See below screenshot. Repro code was modified slightly for clarity.

Interestingly, I was not able to reproduce the bug locally; supplying different AZURE_FUNCTIONS_ENVIRONMENT values on my local machine resulted in expected behavior. Possibly a platform caching issue?

Results by setting value Between each change, the host restarted with a new PID.

  1. No AZURE_FUNCTIONS_ENVIRONMENT setting: IsDevelopment = False [tested on a second app, this was True]
  2. AZURE_FUNCTIONS_ENVIRONMENT = "Development": IsDevelopment = True
  3. AZURE_FUNCTIONS_ENVIRONMENT = "Production": IsDevelopment = True
  4. Deleted AZURE_FUNCTIONS_ENVIRONMENT setting: IsDevelopment = True

Direct setting read vs IHostingEnvironment.IsDevelopment() return value

image

Repro Code

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace _6978repro
{
    public class Function1
    {
        private readonly IHostEnvironment _hostEnvironment;
        private readonly IConfiguration _configuration;

        public Function1(IHostEnvironment hostEnvironment, IConfiguration configuration)
        {
            _hostEnvironment = hostEnvironment ?? throw new ArgumentNullException(nameof(hostEnvironment));
            _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
        }

        [FunctionName("Function1")]
        public void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            log.LogInformation($"_hostEnvironment.IsDevelopment(): {_hostEnvironment.IsDevelopment()}");
            log.LogInformation($"_hostEnvironment.IsProduction(): {_hostEnvironment.IsProduction()}");
            log.LogInformation($"AZURE_FUNCTIONS_ENVIRONMENT: {Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT")}");
        }
    }
}