0

I have a service which is called within an Azure Function, and the service accesses IHostEnvironment via DI. Very standard, works as expected:

public TestService(IHostEnvironment hostEnvironment, IHttpClientFactory httpClientFactory, ILogger<TestService> logger)
{
    this.hostEnvironment = hostEnvironment;
    this.httpClientFactory = httpClientFactory;
    this.logger = logger;
}

When the function is deployed, the IsDevelopment method always returns true, and logging shows that this.hostEnvironment.EnvironmentName is always Development. I've tried adding settings for DOTNET_CORE_ENVIRONMENT, ASPNET_CORE_ENVIRONMENT, and AZURE_FUNCTIONS_ENVIRONMENT. No matter what these settings are, IsDevelopment is always returning true.

What environment variable do I need to set so that IHostEnvironment reflects the correct environment?

/// <inheritdoc/>
public async Task<MemoryStream> GetAsync(Uri requestUri, CancellationToken cancellationToken)
{
    if (this.hostEnvironment.IsDevelopment())
    {
        return new MemoryStream(Encoding.Default.GetBytes("Test content"));
    }
    else
    {
        //etc....
    }
}
Mike
  • 1,010
  • 1
  • 14
  • 33

3 Answers3

0

If you do look at Function App Settings you'll find:

The Azure Functions Core Tools set AZURE_FUNCTIONS_ENVIRONMENT to Development when running on a local computer, and this can't be overridden in the local.settings.json file.

The only way I was able to get around this is to add some custom code that replaces the IHostEnvironment instance with my own. In the below snippet, I use an "Environment" configuration key in a custom appsettings.local.json file.

Load the appsettings file:

public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        builder.ConfigurationBuilder
            // This is only for local execution.  It should not be available in Azure.
            .AddJsonFile($"appsettings.local.json", optional: true, reloadOnChange: true);

        Configuration = builder.ConfigurationBuilder.Build();
    }

Replace the IHostEnvironment:

public override void Configure(IFunctionsHostBuilder builder)
    {
        var environment = Configuration.GetValue<string>("Environment");
        if (!string.IsNullOrWhiteSpace(environment))
        {
            // Get the original IHostEnvirment instance so we can copy some values.
            var defaultHost = builder.Services.BuildServiceProvider().GetService<IHostEnvironment>();
            builder.Services.Replace(new ServiceDescriptor(
                typeof(IHostEnvironment),
                new CustomHostEnvironment
                {
                    ApplicationName = defaultHost.ApplicationName,
                    ContentRootFileProvider = defaultHost.ContentRootFileProvider,
                    ContentRootPath = defaultHost.ContentRootPath,
                    EnvironmentName = environment
                }));
        }
    }

Now, wherever you inject IHostEnvironment, you'll get whatever EnvironmentName value you set.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ed Downs
  • 376
  • 2
  • 5
  • I just wanted to add that I was using this configuration to run as "Localhost" on my development machine, because we had a deployed "Development", "Staging" and "Production" environments. – Ed Downs Dec 03 '21 at 21:31
0

Locally, you can set your launchSettings.json file to override the AZURE_FUNCTIONS_ENVIRONMENT variable.

{
  "profiles": {
    "MyFunctionApp": {
      "commandName": "Project",
      "environmentVariables": {
        "AZURE_FUNCTIONS_ENVIRONMENT": "Local"
      }
    }
  }
}
Ben Sampica
  • 2,912
  • 1
  • 20
  • 25
-1

Is hard tell you a specific solution without know witch Functions runtime is used or if your functions run in-process or out-of-process.

Meanwhile you take a look to Functions app settings.

binick
  • 44
  • 3