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.

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.

Demo

Extra (Using Docker)

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

  2. Write a docker-compose.yml

  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

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

Top comments (0)