.NET 6 Service Bus Trigger Azure Function - Part 1
Published: 2021-12-23 07:05:12

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lcjam65c9isje9j7oppy.png)
 
2\. Select at least Standard tier as it supports Topics feature.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtn4je6tf92srjlj4v89.png)
 
3\. Add a topic. Even though there are several options here, I use default settings for now.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5p973crh76tf4psmxqow.png)
  
4\. Add a subscription for Azure Function to monitor. I set max delivery count as "1" for now.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvz1q0cr9yfkonryxpxb.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wx00co4wl0urtrql57j2.png)
 
# 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vnyo6q3mu6dxm05neyh7.png)
 
2\. From Add role assignment, select "Azure Service Bus Data Receiver".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ng2vnfo5u2o2xqj485au.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/712qsm05xd2z9toncelt.png)
 
2\. Once project is created, Update ``ConnectionString`` argument of ``ServiceBusTrigger`` attribute. I use "ServiceBusConnection" as parameter name.

```csharp
[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](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=csharp#identity-based-connections) for more detail.

Please update host name to yours.

```json
{
  "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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rwcjcgc5qil66b9xb45r.png)
 
# 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hc8f8czgqeiu67dh0xbh.png)
 
3\. You should see break point is hit and incoming data as expected.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4v90lzai2nu3bjkyfj3a.png)

In the [next article](https://dev.to/kenakamu/net-6-service-bus-trigger-azure-function-part-1-2pd9), I will publish this function to Azure Function.