The moment I came to play with .NET Standard 2.0 – Azure Functions
A lot has changed ever since I played with Azure Functions and .NET Standard 2.0. If you remember from my previous post, I was talking about how you had to set the FUNCTIONS_EXTENSION_VERSION to beta in order to benefit from the new runtime. I also talked about how I had a problem with the connection manager to access the app settings.
Well the good news is that the team now fully migrated to the new Configuration modeling from ASP.NET core. You can now easily refer to your configurations by importing the following packages and using the following code to have access to your configuration:
Nuget package | Description |
Microsoft.Extensions.Logging | Main logging package |
Microsoft.Extensions.Logging.Abstractions | Makes SetBasePath() available |
Microsoft.Extensions.Logging.Json | Makes AddJsonFile() method available |
Microsoft.Extensions.Configuration.EnvironmentVariables | Makes AddEnvironmentVariables() method available |
1 2 3 4 5 |
var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); |
Visual Studio
The template in Visual Studio 2017 for Azure Function is targeted for the .NET framework. If you want to play with .NET Standard 2.0, you will need to target Azure Functions Version 2.
This is especially important if you want to use EntityFramework Core 2.1 and over in your function.
To change the target framework and Azure Function version of your Azure Function app, you will need to edit the csproj. In the PropertyGroup
tag, replace the TargetFramework
and AzureFunctionsVersion
tags to the following:
1 2 |
<TargetFramework>netstandard2.0</TargetFramework> <AzureFunctionsVersion>v2</AzureFunctionsVersion> |
You also will need to install the latest Azure Functions Core Tools through npm. Debugging is also an important step when we program. In order to be able to debug, you will need to add a debugging profile. Azure Function staffs provides some information about this here.
In case you care unable to access the page, here’s a quick summary:
- Install the Azure Functions Core Tools from NPM. The core tag will have the latest version:
npm i -g azure-functions-core-tools@core --unsafe-perm true
- In your function project properties, under Debug create a new profile and update the following values:
- Launch: Executable
- Executable:
dotnet.exe
- Application arguments:
%userprofile%\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\func.dll host start
(NOTE: this path may differ if you’re using NVM. You can usenpm root -g
to find the exact path to thenode_modules
folder)
If you wish you can directly modify your launchSettings.json and add another profile (under the “profiles” section) with the following:
1 2 3 4 5 |
"FunctionsCLI": { "commandName": "Executable", "executablePath": "dotnet.exe", "commandLineArgs": "%userprofile%\\AppData\\Roaming\\npm\\node_modules\\azure-functions-core-tools\\bin\\func.dll host start" } |
Don’t forget to update your Azure Functions and Web Jobs Tools extension to the latest version.
Happy coding!