Introduction
In today’s article, we will take a look at Azure Queues and Serverless functions. These are two very fundamental artifacts of the Microsoft Cloud platform Azure. Queues are used for sending loosely coupled messages. This will ensure the robust performance of our applications. Azure serverless functions is a new concept related to Azure. It allows the developer to create code that will work without any platform needed. Scaling etc. all done automatically by the cloud platform itself. I will not cover much on the definitions of these two concepts as there are many articles covering the basic definitions and functionality. We will see these resources in action today.
Creating the Azure Queue
We will first create our queue. For this, we need to create a storage account in Azure. Log into your Azure portal subscription and follow the below steps,
Enter your details as below and always choose the cheapest option for learning unless there is a specific feature you want to test,
Choose the defaults for other options.
You can now add your queue to the storage account as below,
You now have a queue which we will populate through a .NET application.
Creating the console application in Visual Studio 2022 to populate the queue
Next, we create a .NET console application using Visual Studio 2022 and the .NET 6 framework.
We then add the Azure.Storage.Queues Nugget package as below,
Copy the connection string for the storage account as this is needed in the code,
Next, add a new class called “AzureQueueMessage” to the project and add the below code to it. Do not forget to add your connection string,
using Azure.Storage.Queues;
namespace ConsoleAppAzureQueue {
public class AzureQueueMessage {
public static async Task < bool > InsertMessage(string queueName, string message) {
try {
QueueClientOptions queueClientOptions = new QueueClientOptions();
queueClientOptions.MessageEncoding = QueueMessageEncoding.Base64;
var connectionString = "<Connection string here>";
var queueClient = new QueueClient(connectionString, queueName, queueClientOptions);
if (queueClient.Exists()) {
await queueClient.SendMessageAsync(message);
}
} catch {
return false;
}
return true;
}
}
}
Add the below code to the “Program.cs” file,
using ConsoleAppAzureQueue;
await AzureQueueMessage.InsertMessage("mnbtestqueue", "test");
Console.WriteLine("Message added!");
Now, if we run the application, we see a message is added to the Azure queue we just created.
Creating the Serverless function to read the queue
The final part of the article is to create a serverless function that reads the message as soon as it is added into the queue. We follow the below steps. We start by creating a function app,
We can see the newly created function app below,
We now create the function.
Remember to select the Azure Queue Storage trigger.
Update the integration settings as below.
Open the logs as above.
We now run the console application again and a message is added to our queue.
This message is picked up by the function and processed as below,
Summary
In this article, we took a look at Azure queues. How we can populate them using a .NET console application. We finally looked at how we can create a serverless function in Azure to pick the message from this queue and process it. I hope this gives an idea of how these resources can be used in our solutions. Happy coding!