I have two questions on .net core applications for functions. I am using blobtrigger.
1) When I run my project locally I get this 'Hosting environment' on the command prompt console, I want to understand where is this variable set and how can I change it to development. Its misleading since I am only developing locally.
[5/23/2019 7:00:20 PM] Host started (773ms) [5/23/2019 7:00:20 PM] Job host started Hosting environment: Production Content root path: C:Myproject\bin\Debug\netcoreapp2.1 Now listening on: http://0.0.0.0:7071
2) What is the difference between host.json and local.settings.json. When can host.json be used? So far I have only used local.settings.json and when I publish to azure I am creating configurations mentioned in local.settings.json but Host.json is not used it looks like. Whats the purpose of host.json file is.
"Hosting environment" on the console comes from the environment variable ASPNETCORE_ENVIRONMENT. When this variable is not set, it defaults to "Production".
It's set here: HostingEnvironment.cs
The reason behind this default is described in this github issue.
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.
If you wish to define this variable locally, you have a few different ways.
Setting the environment variable via command line:
setx ASPNETCORE_ENVIRONMENT "Development"
Defining this in Properties\launchSettings.json:
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Defining this in local.settings.json:
"Values": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Note that this variable is not automatically defined to production when you publish your app to azure. You will have to define this variable in "Configuration" -> "Application Settings" in Azure portal.
In azure functions there appears to be another similar environment variable called AZURE_FUNCTIONS_ENVIRONMENT. This one is defined by default locally.
AZURE_FUNCTIONS_ENVIRONMENT = Development
This is not defined in production by default, and can be defined in the azure portal.
Difference between host.json and local.settings.json:
host.json is to configure pre-defined settings that function app infrastructure understands. It applies to both local and production environments. It doesn't allow custom settings though. local.settings.json on the other hand is useful for defining custom settings. host.json is committed into source control, but local.settings.json is usually left out of source control, and is considered to be a good location to store secrets and connection strings for development.
More details here about the differences: https://docs.microsoft.com/en-us/azure/azure-functions/functions-develop-vs#create-an-azure-functions-project (scroll to the end of that section)
host.json reference
local.settings.json reference
"ASPNETCORE_ENVIRONMENT": "Development"
in the local.settings.json, to change the hosting environment: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