Build Azure Functions V4 with .NET 6

Raymond Raymond event 2021-09-11 visibility 11,108
more_vert
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:

2021091101653-image.png

Input your project name details and then choose .NET 6 as SDK, select Storage emulator as storage account. For authorization level, choose Anonymous. 

2021091102318-image.png

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:

2021091103824-image.png

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:

2021091120846-image.png

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:

2021091121130-image.png

2021091121247-image.png

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

2021091120333-image.png

warning The above fix works for .NET 6 Azure functions that runs as Project instead of isolated process.

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. 

2021092584308-image.png

In Azure DevOps, you can use task Azure Functions to update a function application:

20210925120053-image.png

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.

warning This example task is added for isolated worker .NET 6 Azure V4 functions. The earlier example above is an in-process function application and you can convert it to a console application.

For more details about configure application settings, refer to Azure DevOps - Configure Web or Function App Settings.

References

C# 10.0 New Features

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts