DEV Community

Cover image for Exploring Azure Queue Storage in .NET
Bervianto Leo Pratama
Bervianto Leo Pratama

Posted on

Exploring Azure Queue Storage in .NET

Preparation

There are some steps we need to take.

  1. Install Azurite.
  2. Set up the Projects.

Install Azurite

You may follow this tutorial page to install Azurite. We will Azurite as our testing when developing the application which uses Azure Queue Storage. We won't use Azure Queue Storage directly but use the simulator. Feel free to use any method that you want. I will cover how to use the emulator in Docker.

Set up the projects

  1. Create the solution file. dotnet new sln
  2. Create the "Producer" project. dotnet new console -o Producer
  3. Create the "Consumer" project. dotnet new console -o Consumer
  4. Add both projects to the solution file. dotnet sln add Consumer Producer

Prepare the Producer

  1. Add the Azure.Storage.Queues package to the Producer project. dotnet add Producer package Azure.Storage.Queues
  2. You may wonder how to authenticate or access the Azure Storage Queue. Please refer to this page for more information. Since we use Azurite, we may use the connection string. Please use Microsoft's recommended authentication to use the code in production. Please write these codes to create the Queue Client and send the message.

    // Producer
    using Azure.Storage.Queues;
    string queueName = "hello-test";
    string connectionString = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;";
    // Instantiate a QueueClient to create and interact with the queue
    QueueClient queueClient = new QueueClient(connectionString, queueName);
    Console.WriteLine($"Creating queue: {queueName}");
    // Create the queue
    await queueClient.CreateAsync();
    var i = 0;
    do
    {
    var name = $"Leo-{i}";
    Console.WriteLine($"Sending: {name}");
    await queueClient.SendMessageAsync(name);
    i += 1;
    Thread.Sleep(1000);
    } while (true);
    view raw Program.cs hosted with ❤ by GitHub

Prepare the Consumer

  1. Add the Azure.Storage.Queues package to the Producer project. dotnet add Producer package Azure.Storage.Queues
  2. Please write these codes to create the Queue Client and receive the message.

    using Azure.Storage.Queues;
    string queueName = "hello-test";
    string connectionString = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;";
    // Instantiate a QueueClient to create and interact with the queue
    QueueClient queueClient = new QueueClient(connectionString, queueName);
    do
    {
    Console.WriteLine($"Capture: {DateTime.Now}");
    var properties = queueClient.GetProperties();
    int cachedMessagesCount = properties.Value.ApproximateMessagesCount;
    // Display number of messages
    Console.WriteLine($"Number of messages in queue: {cachedMessagesCount}");
    var receivedMessages = await queueClient.ReceiveMessagesAsync(maxMessages: 10);
    if (receivedMessages.Value.Count() == 0)
    {
    Console.WriteLine("Empty message!");
    }
    foreach (var receivedMessage in receivedMessages.Value)
    {
    // Display the message
    Console.WriteLine($"ID: {receivedMessage.MessageId} Message: {receivedMessage.MessageText}");
    // ensure remove it after processing
    await queueClient.DeleteMessageAsync(receivedMessage.MessageId, receivedMessage.PopReceipt);
    }
    Console.WriteLine("------------");
    Thread.Sleep(3000);
    } while (true);
    view raw Program.cs hosted with ❤ by GitHub

Demo

Extra (Using Docker)

  1. Write a Dockerfile for each application. Save each Dockerfile as "Dockerfile" without a double quote in each application directory.

    FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
    WORKDIR /App
    # Copy everything
    COPY . ./
    # Restore as distinct layers
    RUN dotnet restore
    # Build and publish a release
    RUN dotnet publish -c Release -o out
    # Build runtime image
    FROM mcr.microsoft.com/dotnet/aspnet:8.0
    WORKDIR /App
    COPY --from=build-env /App/out .
    ENTRYPOINT ["dotnet", "Consumer.dll"]
    FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
    WORKDIR /App
    # Copy everything
    COPY . ./
    # Restore as distinct layers
    RUN dotnet restore
    # Build and publish a release
    RUN dotnet publish -c Release -o out
    # Build runtime image
    FROM mcr.microsoft.com/dotnet/aspnet:8.0
    WORKDIR /App
    COPY --from=build-env /App/out .
    ENTRYPOINT ["dotnet", "Producer.dll"]
  2. Write a docker-compose.yml

    services:
    consumer:
    build: ./Consumer
    producer:
    build: ./Producer
    azurite:
    image: mcr.microsoft.com/azure-storage/azurite
  3. Please ensure the connection string becomes like this. string connectionString = "DefaultEndpointsProtocol=https;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;QueueEndpoint=http://azurite:10001/devstoreaccount1;";
    We need to set the address to become the Azurite service name.

  4. Build the images. docker compose build

  5. Run the compose. docker compose up

Repository

Azure Storage Queue

Sample App for Tutorial in my dev.to

LICENSE

MIT




Summary

You need to ensure the messages are cleared after processing the message. You may use the queue to send a small JSON message. Ensure the message is not more than 64 KB.

Many use cases are using Azure Storage Queue. For example, we have e-commerce that consists of many microservices. We have a service that receives an order message from the customer. Order service will send the message into the queue and stock service will receive the message and ensure those goods are locked to the customer. The processes are asynchronous.

Do you have any suggestions? Feel free to comment here. Thank you for reading.

Sesame happy GIF

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 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