As .NET 6 is GA now, I will explain how we can develop Azure Function with .NET6. In this article, I use following setup and develop function in local.
- Service Bus Topic Trigger
- User Managed Identity Connection
- .NET 6
You can use any other binding and connection type as you wish, but I believe you may be interested in Managed Identity connection at least.
Prerequisites
- Visual Studio 2022 (or any other IDE supports .NET 6)
- Azure Subscription
Create Service Bus namespace and topic
First thing first, let's create Service Bus topic.
2. Select at least Standard tier as it supports Topics feature.
3. Add a topic. Even though there are several options here, I use default settings for now.
4. Add a subscription for Azure Function to monitor. I set max delivery count as "1" for now.
Setup Access Control (IAM)
I add myself as Data Receiver for now.
1. Go to IAM menu of the Topic and Click "Add".
2. From Add role assignment, select "Azure Service Bus Data Receiver".
3. Select your account and assign to the member.
I will do the same for Azure Function Managed Id later.
Create Azure Function Project
As Service Bus is ready now, let's create Azure Function.
1. From Visual Studio 2022, create new Function Project. Select .NET 6 and Service Bus Topic trigger. Update topic and subscription name.
2. Once project is created, Update ConnectionString
argument of ServiceBusTrigger
attribute. I use "ServiceBusConnection" as parameter name.
[FunctionName("Function1")]
public void Run([ServiceBusTrigger(
"mytopic",
"FunctionSubscription",
Connection = "ServiceBusConnection")]string mySbMsg)
{
_logger.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
}
3. Open local.settings.json and add <ConnectionName>__fullyQualifiedNamespace like below, which enables Identity based connection. See here for more detail.
Please update host name to yours.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"ServiceBusConnection__fullyQualifiedNamespace": "myservicebusforaf.servicebus.windows.net"
}
}
4. Open NuGet Manager and update Microsoft.Azure.WebJobs.Extensions.ServiceBus to version 5 or later as it's required to user Managed Identity base connection.
Debug
Time to test the setup.
1. Enter F5 to start debugging. Also place break point at Run method.
2. From Azure Portal, go to "Service Bus Explorer" to send test data.
3. You should see break point is hit and incoming data as expected.
In the next article, I will publish this function to Azure Function.
Top comments (6)
man, you are my hero today!
It was very useful I was looking exactly for this. Thanks!
I'm having this issue when building, does that look familiar? :-( Microsoft.NET.Sdk.Functions.Build.targets(32, 5): Function [XYZ]: cannot find value named 'ServiceBusConnection' in local.settings.json that matches 'connection' property set on 'serviceBusTrigger'
Yes and are you having local.seiings.json which have the seeing?
Actually,
local.settings.json
was correct. Problem was with<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
in our solution that I was not aware of.Also when I used old version of VS, it didn't recognize the solution as Function project which failed to debug