Preparation
There are some steps we need to take.
- Install Azurite.
- 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
- Create the solution file.
dotnet new sln
- Create the "Producer" project.
dotnet new console -o Producer
- Create the "Consumer" project.
dotnet new console -o Consumer
- Add both projects to the solution file.
dotnet sln add Consumer Producer
Prepare the Producer
- Add the
Azure.Storage.Queues
package to the Producer project.dotnet add Producer package Azure.Storage.Queues
-
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
- Add the
Azure.Storage.Queues
package to the Producer project.dotnet add Producer package Azure.Storage.Queues
-
Please write these codes to create the Queue Client and receive the message.
Demo
Extra (Using Docker)
-
Write a Dockerfile for each application. Save each Dockerfile as "Dockerfile" without a double quote in each application directory.
-
Write a
docker-compose.yml
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.
Build the images.
docker compose build
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.
Top comments (0)