Azure Storage Queue is a component of Azure Storage Account which is used for messaging service. We can Add message to a Queue and also dequeue (Delete) the message. When dequeue a message then the dequeue count will get increased.
Generally adding and queue message and deleting a queue message has been done by programmatically. So, my intention is to demonstrate a simple CRUD operation on queue message using C# console app.
Step 1: Create a C# console app. (I will use Visual Studio 2022).
Step 2: Use NuGet Package Manager and download “Azure.Storage.Queues”.
Step 1: Create a C# console app. (I will use Visual Studio 2022).
Step 2: Use NuGet Package Manager and download “Azure.Storage.Queues”.
Step 3: Get the connection string first. To get the connection details 1st we have to go to the storage account and then Access key section. Remember the connection string is a part of azure storage account.
Get the queue name which you have just created. In my case the queue name is “lazylearnerqueue”.
Step 4: Now we are ready with our console app and we can start the Create operation.
- We need to create a connection using the above mention connection string and queue name.
- Add “using Azure.Storage.Queues;” reference to the code.
- Use “QueueClient” to create the connection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Storage.Queues;
namespace AzureStorageQueueCRUDApp
{
internal class AzureStorageQueueCRUDApp
{
static void Main(string[] args)
{
//Get the connection string from azure storage account Access Key
string AzureStorageQueueConnection ="*************";
string QueueName = "lazylearnerqueue";
createQueueMessage("Test Message 1", QueueName, AzureStorageQueueConnection);
}
static void createQueueMessage(string queueMessageBody, string QueueName, string AzureStorageQueueConnection)
{
//Create a connection.
QueueClient queueClient = new QueueClient(AzureStorageQueueConnection, QueueName);
if(queueClient.Exists())
{
queueClient.SendMessage(queueMessageBody);
Console.WriteLine(queueMessageBody);
}
}
}
}
We create a .Net object as a Message:
- We need to transfer a .net object to a JSON string and then we can send it to the Queue.
- We can create a class in our project.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Azure.Storage.Queues;
namespace AzureStorageQueueCRUDApp
{
public class ModelObject
{
public string Name { get; set; }
public string Description { get; set; }
public string Type { get; set; }
}
internal class AzureStorageQueueCRUDApp
{
static void Main(string[] args)
{
//Get the connection string from azure storage account Access Key
string AzureStorageQueueConnection = "***********";
string QueueName = "lazylearnerqueue";
createQueueMessageUsingObject(new ModelObject() { Description = "Description Detail",Name="Name details",Type = "Type Details"}, QueueName, AzureStorageQueueConnection);
}
static void createQueueMessageUsingObject(ModelObject queueMessageBody, string QueueName, string AzureStorageQueueConnection)
{
//Create a connection.
QueueClient queueClient = new QueueClient(AzureStorageQueueConnection, QueueName);
if (queueClient.Exists())
{
var SerializeModelObject = JsonSerializer.Serialize(queueMessageBody);
queueClient.SendMessage(SerializeModelObject);
Console.WriteLine("Message is created");
Console.ReadLine();
}
}
}
}
No comments:
Post a Comment