DEV Community 👩‍💻👨‍💻

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

.NET 6 Service Bus Trigger Azure Function - Part 1

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.

1. Create Service Bus.
Image description

2. Select at least Standard tier as it supports Topics feature.

Image description

3. Add a topic. Even though there are several options here, I use default settings for now.

Image description

4. Add a subscription for Azure Function to monitor. I set max delivery count as "1" for now.

Image description

Image description

Setup Access Control (IAM)

I add myself as Data Receiver for now.

1. Go to IAM menu of the Topic and Click "Add".

Image description

2. From Add role assignment, select "Azure Service Bus Data Receiver".

Image description

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.

Image description

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}");
}
Enter fullscreen mode Exit fullscreen mode

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"
  }
}
Enter fullscreen mode Exit fullscreen mode

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.

Image description

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.

Image description

3. You should see break point is hit and incoming data as expected.

Image description

In the next article, I will publish this function to Azure Function.

Top comments (4)

Collapse
 
petrkoutnycz profile image
Petr Koutny

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'

Collapse
 
kenakamu profile image
Kenichiro Nakamura

Yes and are you having local.seiings.json which have the seeing?

Collapse
 
petrkoutnycz profile image
Petr Koutny • Edited on

Actually, local.settings.json was correct. Problem was with <MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors> in our solution that I was not aware of.

Thread Thread
 
kenakamu profile image
Kenichiro Nakamura

Also when I used old version of VS, it didn't recognize the solution as Function project which failed to debug

Regex for lazy developers

regex for lazy devs

You know who you are. Sorry for the callout 😆