This article shows steps to create HTTP triggered Azure Functions with .NET 6 with Visual Studio 2022.
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:
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
The project directly reference Functions SDK and also the target Azure Functions version is also v4 now instead of v3.
Explore the project
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.
This file is used for local development configurations. All the keys in Values section will be passed as environment variables.
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.
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:
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