DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

8 2 1

.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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (6)

Collapse
 
sgaliamov profile image
s. galiamov

man, you are my hero today!

Collapse
 
jainjo profile image
Alexis Suarez

It was very useful I was looking exactly for this. Thanks!

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

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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay