Hosting environment why is it set to Production, Difference between host.json and local.settings.json - azure

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://learn.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

You can add "ASPNETCORE_ENVIRONMENT": "Development" in the local.settings.json, to change the hosting environment:
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.

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:
Delete local.settings.json
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
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}"
}
]

Related

Refer environment variable in Run.json for Fleet IDE

I am working on setting up the JetBrains Fleet for JavaScript development.
Here is my hard coded run.json look like.
"configurations": [
{
"type": "command",
"name": "local test",
"program": "npm",
"args": [
"run",
"test",
"ssoni_local"
],
}
]
Instead of using ssoni (my username), I would like to use ${USERNAME} environment variable so the same run.json file can be used my other team members as well.
How can I achieve the same?
Also same needs for the hostname variable as well. Instead of hardcoding machine name want to use an environment variable.

launch.json for azure core function version 3

I was working in the azure core tools version 2 and recently updated to version 3. Now I am not able to run any functions as it is showing error message "connect Econnrefused 127.0.0.1:9091".
Here is my launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}
I am working on windows machine and using 1.6.1 azure functions versions.
Do I have to change something? I checked and this is common error and reinstalling the extensions should work?I tried all those. Do you think, I am missing anything ?
Please check if the below workarounds help to:
Approach 1:
Use Integrated terminal in the VS Code and then run the Azure Functions Python:
I will also get this issue most of the time and My working launch.json configuration will be like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current file",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Azure Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}
Approach 2:
If the above approach gets the issue still, then try changing the port number and then run the Azure Functions Python.
Approach 3:
Instead of running the Azure Functions Python from the VS Code built-in Run Menu, try with the commands func host start in the integrated terminal like:
If that does not help you, please refer to this workaround given here which solves the specific error of Azure Function Python - Error Connection Refused.

Set env var for Node JS when launching through VS Code

I have Node JS Azure function that when I run locally needs the NODE_TLS_REJECT_UNAUTHORIZED=0 environment variable needs setting for my code to work. This is because I am connecting to an emulated Cosmos DB running locally on my machine which require Node to allow for self signed certificates.
I only want this environment variable setting when I run locally as in production I will be using a real (i.e. non emulated) Cosmos DB instance. Rather than putting and #if debug in my code (or the equivalent for a Node Azure Function) I'd like my VS Code project to set the env var when it is launched.
I've trying following this answers advice but when VS Code launches the program the environment variable is not set as I get a runtime error in my code about self signed certificates not being authorised. My launch.json looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Node Functions",
"type": "node",
"request": "attach",
"port": 9229,
"preLaunchTask": "func: ",
},
],
"environment": [{
"name": "NODE_TLS_REJECT_UNAUTHORIZED",
"value": "0"
}],
}
If I set the env var directly in code using process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 everything is fine. I don't know how I can get VS Code to set this, or if it is even possible.
Please update
"environment": [{
"name": "NODE_TLS_REJECT_UNAUTHORIZED",
"value": "0"
}],
To Be
"env": {
"name": "NODE_TLS_REJECT_UNAUTHORIZED",
"value": "0"
},
Also please check the alternatives.
You can Update package.json scripts to add all environment variables on start like "runLocal": "NODE_TLS_REJECT_UNAUTHORIZED=0 ... your start code " or You can use third party lib called dotenv
this is how it works
01- Create .env file
02- Write all your environment variables key=val NODE_TLS_REJECT_UNAUTHORIZED=0
03- Update package.json scripts to add "runLocal": "NODE_ENV=dev ... your start code "
04- Check if node NODE_ENV is equal dev then load the dotenv
05- Import module in case of node env is local and call config function require('dotenv').config()

Unable to debug Azure Functions Core Tools in VSCode

I am currently having trouble to debug my Azure Functions Core Tools in VS Code.
I am using the npm package azure-functions-core-tools#2.
As i read on many resources func host start / func start should always start the node process in with --inspect=9229. As you can see this is not the case in my setup:
[4/30/19 4:51:25 AM] Starting language worker process:node "/usr/lib/node_modules/azure-functions-core-tools/bin/workers/node/dist/src/nodejsWorker.js" --host 127.0.0.1 --port 50426 --workerId 3e909143-72a3-4779-99c7-376ab3aba92b --requestId 656a9413-e705-4db8-b09f-da44fb1f991d --grpcMaxMessageLength 134217728
[4/30/19 4:51:25 AM] node process with Id=92 started
[4/30/19 4:51:25 AM] Generating 1 job function(s)
[...]
[4/30/19 4:51:25 AM] Job host started
Hosting environment: Production
Also all attempts of changing the hosting environment failed. I tried to add FUNCTIONS_CORETOOLS_ENVIRONMENT to my local configuration, resulting in an error:
An item with the same key has already been added. Key: FUNCTIONS_CORETOOLS_ENVIRONMENT
I tried to add several environment variables in my launch and task settings, using --debug, changing project settings. Nothing works.
I am currently running this on the Windows Subsystem for Linux (WSL) and except this it works really well.
Anyone has a clue on what i am doing wrong here?
I don't think debug is enabled by default. You will have to set the language worker arguments for this to work as documented.
In local.settings.json
To debug locally, add "languageWorkers:node:arguments": "--inspect=5858" under Values in your local.settings.json file and attach a debugger to port 5858.
With func CLI
You could set this by using the --language-worker argument
func host start --language-worker -- --inspect=5858
In VS Code
If you are developing using VS Code and the Azure Functions extension, the --inspect is added automatically as the corresponding environment variable is set in .vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "runFunctionsHost",
"type": "shell",
"command": "func host start",
"isBackground": true,
"presentation": {
"reveal": "always"
},
"problemMatcher": "$func-watch",
"options": {
"env": {
"languageWorkers__node__arguments": "--inspect=5858"
}
},
"dependsOn": "installExtensions"
},
{
"label": "installExtensions",
"command": "func extensions install",
"type": "shell",
"presentation": {
"reveal": "always"
}
}
]
}
You could also set this environment variable directly if you'd like instead of adding it in local.settings.json too.

How to start developping with TypeScript and NodeJS in VSCode

I used to develop web sites with C# or Javascript with Visual Studio and IIS.
I've decided to upgrade to newer tools and try to create a simple web site with VSCode, NodeJS and TypeScript that I'll deploy to Azure later but each time I try a new sample, I get lost as I have the feeling it doesn't do what I want.
I created a TSConfig.json file with this minimum, I understood it creates a "project" in TypeScript:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true
}
}
A simple main.ts file:
class Startup {
public static main(): number {
console.log('Hello World');
return 0;
}
}
Startup.main();
A simpliest index.html file that references the generated main.js file
I wanted to
- compile my web site using "$tsc-watch" to benefit from that automatic recompile
- launch the web site in NodeJS
- Open the web page in Chrome and being able to debug
But I am wondering, is it the right approach ? Should it be a tasks.json file that each time runs "$tsc-watch", launch the web site in Node and opens Chrome ?
I started with this tasks.json file :
{
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc-watch"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Chrome",
"type": "process",
"command": "chrome.exe",
"windows": {
"command": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
},
"args": ["./index.htm"],
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Then, VSCode created a launch.json file but I'm not sure why and where it fits in the picture:
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
Would you be able to help me understand what I am doing wrong here and what I need to simply debug my application in VSCode as I would pressing F5 in VSStudio.
Thank you for any help,
Claude
It looks like you're trying to create the entire build-chain and configuration yourself. I recommend starting with a tool that handles the initial bootstrapping for you. For exampe, use the vue-cli tool to bootstrap a vue.js project with TypeScript. While it may include a little bit of spin-up in understanding vue.js, the vue-cli tool lets you select options (e.g. TypeScript) and auto-generates a project for you. Then, just open the newly created folder in Visual Studio Code and start playing around.
Once you get a feel for how it all ties together, you can add VSCode specific tasks, start modifying configurations, etc.
There are a number of good tutorials on vue.js and, in practice, you'll probably want to leverage a front-end framework when building any real application anyway.
See the following links for tutorials and more information:
vue-cli
vue.js

Resources