Build Azure Functions V4 with .NET 6
This article shows steps to create HTTP triggered Azure Functions with .NET 6 with Visual Studio 2022.
Prerequisites
Download and install Visual Studio 2022 preview edition from here: https://visualstudio.microsoft.com/vs/preview/.
You could also use .NET 6 SDK without Visual Studio.
Create the project
In Visual Studio 2022, create a project using Azure Functions template:
Input your project name details and then choose .NET 6 as SDK, select Storage emulator as storage account. For authorization level, choose Anonymous.
The authorization level determines what keys (if any) need to be present on the request in order to invoke the function. The authorization level can be one of the following values:
- anonymous — No API key is required.
- function — A function-specific API key is required. This is the default value if none is provided.
- admin — The master key is required.
After the project is initiated, it looks like the following screenshot:
Differences with .NET 5
Compared with .NET 5 Azure Function template with Visual Studio 2019, the project is now created as a library instead of executable program thus Program.cs doesn't exist any more.
The project file is also different:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
The project directly reference Functions SDK and also the target Azure Functions version is also v4 now instead of v3.
Explore the project
host.json
This JSON file is used to configure your Azure Function application. Refer this documentation to find out more available configurable properties:https://docs.microsoft.com/en-us/azure/azure-functions/functions-host-json.
local.settings.json
This file is used for local development configurations. All the keys in Values section will be passed as environment variables.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
When publishing the application to Azure, you can also configure these values in application settings. Alternatively, you could also leverage option pattern and appsettings.json as you can do with any .NET web applications if you conver the function to a worker one (executable instead of dll): Dependency Injection and Use appsettings.json in .NET 5 Azure Functions.
Function1.cs
This class file includes your primary function definition. You can modify the function accordingly.
Run the application
Press F5 to run the application. If you get firewall prompts, please allow it.
The application command prompt looks like the following screenshot:
The function can be triggered via open the following link in browser:
http://localhost:11000/api/Function1
The logs will print out and the browser will also print out the message as defined in the function:
Fix port error
You may encounter the following error if the default port is not available:
Port 7071 is unavailable. Close the process using that port, or specify another port using --port [-p].
I've published a workable solution for .NET 5: Port 7071 is unavailable. Close the process using that port, or specify another port using --port [-p].
For Visual Studio 2022, the fix is slightly different.
Open project properties and then go to Debug -> Open debug launch profiles UI.
Input the following line with the port you want to use:
host start --port 11000
Deploy Azure Functions V4 via Azure DevOps
Before deploying .NET 6 based function app to Azure, you need to create a Function App resource. Make sure you choose .NET 6 as runtime.
In Azure DevOps, you can use task Azure Functions to update a function application:
In the above setup, I also configured application worker runtime as dotnet-isolated (not in process dotnet runtime). Functions extension version is also specified as ~4.
For more details about configure application settings, refer to Azure DevOps - Configure Web or Function App Settings.