In this Azure article, we will discuss a step-by-step tutorial to send and read messages using Azure Functions from Service Bus Queues.
How to Use Azure Functions to Send And Read Messages From Azure Service Bus Queues
Below is the functionality we are doing one by one
- Creating an Azure Service Bus Queue in the Azure Portal
- Building an HTTP trigger Azure Function for sending the messages to the Queue.
- Creating a Service Bus Queue triggers the Azure function for reading the messages from the Queue
Before starting the actual functionality, let’s discuss the Prerequisites needed here.
Prerequisites
- You must have an Azure subscription. If you don’t have it now, you can create an Azure free account.
- You must download and install Visual Studio 2019 on your development machine.
Assuming you are ready with all the Prerequisites needed here, Let’s start with the actual functionality.
Creating an Azure Service Bus Queue in the Azure Portal
Follow the below two links to create the Azure service bus namespace and queue in the Azure Portal.
How to create Azure Service bus namespace
Creating an Azure Service bus queue in Azure Portal (Follow step-7 to step-9).
Now, I have already created the Azure Service bus namespace and the service Bus Queue.
Creating an HTTP trigger Azure Function for sending the messages to the Queue
Follow the below steps
- Launch Visual Studio 2019 on your development machine.
- Click on Create a new project button.
- Search for Azure Functions –> Select the Azure Functions template –> Click on the Next button.

4. On Configure your new project window, Provide a name for your project and then choose the location where you wish to save the project –> Click on the Create button.

5. On the Create a new Azure Functions Application window, fill out the below details
- Project Template: Choose the HTTP trigger option.
- Authorization level: Choose Anonymous.
- Make Sure Azure Functions v3 (.NET Core) is selected.
Finally, click on the Create button. Now, our project has been created successfully.

Let’s modify the class and function name to a meaningful name “SendingMessageToQueue”.
Now, as a next step, we need to install the below Nuget Package
“Microsoft.Azure.Webjobs.Extensions.ServiceBus”. To install the NuGet package, Right click on the Solution –> Select the Manage NuGet Packages for Solution option –> Search for Microsoft.Azure.Webjobs.Extensions.ServiceBus, and then on the search result, click on the Install button.

Now, as a next step, you need to get the connection string of the Azure Service Bus Namespace. For that, you need to navigate to the Service Bus Namespace in the Azure Portal –> Click on the Shared access policies from the left navigation –> Click on the RootManageSharedAccessKey –> Click on the copy button next to the Primary Connection String.

Now, you must add the connection string in your local.settings.json file like below.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureWebJobsServiceBus": "Your Azure Service Bus Connection String that you have copied above"
}
}Now Modify your Function1.cs file like below
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Servic