8
votes

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.

3

3 Answers

12
votes

"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

3
votes
  1. You can add "ASPNETCORE_ENVIRONMENT": "Development" in the local.settings.json, to change the hosting environment:

enter image description here

  1. As you know, local.settings.json is just for local testing and will not be published to azure portal. For host.json(which will be published to azure), you can configure settings like loglevel(if you want to log) in azure portal. More details, please refer to this article of host.json.
-1
votes

Sometimes, most times, Microsoft has strange ways of doing things... we as the community have to either find a way around them, or conform. Well, if you are one of those who cant survive the idea to conforming to something that doesn't make sense, here is a solution:

  1. Delete local.settings.json
  2. Create two files (or many, each for your environment), and name them {environment}.local.settings.json eg: development.local.settings.json and production.local.settings.json
  3. Create a task to copy your current environment file to local.settings.json

Here we go again: we got to duplicate tasks...

[
  {
    "label": "use development.local.settings.json",
    "command": "cp development.local.settings.json local.settings.json",
    "type": "shell",
    "options": {
      "cwd": "${workspaceFolder}/..."
    }
  },
  {
    "label": "use production.local.settings.json",
    "command": "cp production.local.settings.json local.settings.json",
    "type": "shell",
    "options": {
      "cwd": "${workspaceFolder}/..."
    }
  },
  {
    "label": "azure-func-development",
    "type": "func",
    "dependsOn": [
      "use development.local.settings.json",
      "build"
    ],
    "dependsOrder": "sequence",
    "options": {
      "cwd": "${workspaceFolder}/.../bin/Debug/netcoreapp2.2"
    },
    "command": "host start",
    "isBackground": true,
    "problemMatcher": "$func-watch"
  },
  {
    "label": "azure-func-production",
    "type": "func",
    "dependsOn": [
      "use production.local.settings.json",
      "build"
    ],
    "dependsOrder": "sequence",
    "options": {
      "cwd": "${workspaceFolder}/.../bin/Debug/netcoreapp2.2"
    },
    "command": "host start",
    "isBackground": true,
    "problemMatcher": "$func-watch"
  }
]

Don't get tired, we are not finished duplicating, yet...

Lets duplicate the launch.json configurations:

[
  {
    "name": "[Development] Attach to Azure .NET Functions",
    "type": "coreclr",
    "request": "attach",
    "preLaunchTask": "azure-func-development",
    "processId": "${command:azureFunctions.pickProcess}"
  },
  {
    "name": "[Production] Attach to Azure .NET Functions",
    "type": "coreclr",
    "request": "attach",
    "preLaunchTask": "azure-func-production",
    "processId": "${command:azureFunctions.pickProcess}"
  }
]