3
votes

I've deployed an Azure Function to Azure. I have not specified AZURE_FUNCTIONS_ENVIRONMENT to be "Development" anywhere in the Application Settings, or anywhere else in the environment.

However, when the following code executes

var configurationBuilder = new ConfigurationBuilder()
    .SetBasePath(basePath)
    .AddJsonFile("appsettings.json", true, true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, reloadOnChange: true);

$"appsettings.{env.EnvironmentName}.json"resolves to "appsettings.Development.json", and as such the Development configuration is loaded instead of the Production config.

When I view the Environment variables of that environment via Kudu, I don't see any references to Development at all, and in fact see WEBSITE_SLOT_NAME = Production configured (though that's not necessarily relevant to a Function.

My understanding is that if no Environment is specified then the runtime defaults to Production. As such, this implies to me that Development is specified somewhere. I have no idea where to look for that though.

Question: Is it possible that Development could be specified somewhere that isn't in App Settings or Environment variables? If so, where? Is there anywhere I could look for it, or is there any other explanation for why the Development configuration is being loaded instead of Production? What should I do to explicitly ensure the Production configuration is loaded in the Production environment?

3

3 Answers

1
votes

Unfortunately I couldn't get this to work until I manually specified that I wanted the data from Environment Variables:

var environment = Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT");

var configurationBuilder = new ConfigurationBuilder()
    .SetBasePath(basePath)
    .AddJsonFile("appsettings.json", true, true)
    .AddJsonFile($"appsettings.{environment}.json", true, reloadOnChange: true);

Which required that I specify the value in Application Settings in the Azure environment:

enter image description here

And in the launchSettings.json file as

"environmentVariables": {
  "AZURE_FUNCTIONS_ENVIRONMENT": "Development"
}
0
votes

.NET Core uses the ASPNETCORE_ENVIRONMENT and Hosting:Environment environment variables for env.EnvironmentName. WEBSITE_SLOT_NAME is set by Azure and indicates the name of the Deployment Slot hosting the app.

0
votes

You'd better set it in App settings or Environment variables if you want to have a better control.

Just for your case, you should remember that when you use the code:

.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, reloadOnChange: true);

the 2nd .AddJsonFile() method should always override the first .AddJsonFile() method, if both appsettings.json and appsettings.development.json exist.

So if you just want to make sure it uses production config without using App setting or environment variable, just specify your production config( it is appsettings.json file) in the 2nd .AddJsonFile() method.