Microsoft provides Azure Storage Explorer and Azure Storage Emulator tools for developers who want to test and use storage accounts and services without getting a paid Azure subscription. In this article, I will discuss how to use Azure Queue Storage using Development Storage Account, that is free.
Installation
Download Microsoft Azure Storage Explorer from the below URL:
https://azure.microsoft.com/en-in/features/storage-explorer/
Download Microsoft Azure Storage Emulator - v5.8 (please download v5.8 version only)
https://azure.microsoft.com/en-in/downloads/
Key Points
Microsoft Azure Storage Explorer and Microsoft Azure Storage Emulator, both should be active or running.
Start Explorer and Emulator
First, run your Explorer and Emulator as in the below snapshot. This will be your explorer.
What is Azure Queue Storage?
Azure Queue Storage is a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated calls using HTTP or HTTPS. A single queue message can be up to 64 KB in size, and a queue can contain millions of messages, up to the total capacity limit of a storage account.
How to create an Azure Queue storage?
There are two ways to create an Azure Queue Storage.
-
In Azure Storage Explorer, Right-click on the Queue and select create queue option, in the text box enter your queue name. Please see Figure (1). Note, the queue name should be in lowercase.
- We can create using C# code programmatically.
For programming what is required?
Below namespace is required to be imported in your code. Run bellow NuGet command in your solution.
- using Microsoft.WindowsAzure.Storage;
- using Microsoft.WindowsAzure.Storage.Queue;
Click On Tools => Hover On => NuGet Package Manager and select package manager console.
Execute the below command.
Install-Package WindowsAzure.Storage -Version 9.3.3
This will be the default credentials and we are storing at the web config file.
DefaultEndpointsProtocol=http;
AccountName=devstoreaccount1;
AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==
Please add the below key in the web config in the connection string section.
- <connectionStrings>
- <add name="azurestorageconfigurationkey" connectionString="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==" providerName="System.Data.SqlClient"/>
- </connectionStrings>
Create a queue.
As of now, in our Azure Storage Explorer, we don't have any queue, right?
Figure(2)
Please refer Figure(2) in that under Queues as of now we don't have any queues.
Using the below code we create a Queue and named as demoqueue. Please refer Figure (3).
Figure (3)
As you can see in the below Figure (4) that demoqueue is now available under Queue storage.
Figure(4)
Now, we have created a queue. Our next step is to insert messages to the queue.
Create a message and add it to the queue. For code, see below. As you can see from this code, we create a CloudQueueMessage object and call its AddMessageAsync method.
Figure(5)
After executing the above code, a new message will be added to our queue. See Figure(6).
Figure(6)
Read a message from a queue.
Peek the next message from a queue.
You can peek at the message in the front of a queue without removing it from the queue by calling the PeekMessageAsync method. See code below. The below code will read the first message from the queue.
Figure(7)
Read and remove a message from a queue.
Your code can remove (dequeue) a message from a queue in two steps.
- Call GetMessageAsync to get the next message from a queue. A message returned from GetMessageAsync becomes invisible to any other code reading messages from this queue. By default, this message stays invisible for 30 seconds.
- To remove this message, call DeleteMessageAsync method.
This two-step process of removing a message assures that if your code fails to process a message due to hardware or software failure, another instance of your code can get the same message and try again. The following code calls DeleteMessageAsync right after the message has been processed.
Figure(8)
Delete a queue
To delete a queue and all messages in it, call Delete method of queue object. The following code delete a queue and its messages.
Figure(9)
Summary
In this blog, we have discussed the following:
- Install Azure Storage Explorer and Azure Storage Emulator.
- Create an Azure Storage Queue.
- Add a message to Azure Storage Queue.
- Read a message from a Azure Storage Queue.
- Delete a message from a Azure Storage Queue.
- Delete Azure Storage Queue.
Complete Code Snipet
-
- CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
-
- string connectionString = ConfigurationManager.ConnectionStrings["azurestorageconfigurationkey"].ToString();
- CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
- CloudQueue queue = queueClient.GetQueueReference("demoqueue");
-
- await queue.CreateIfNotExistsAsync();
-
-
- CloudQueueMessage message = new CloudQueueMessage("Hello, C# Corner");
- await queue.AddMessageAsync(message);
-
-
- CloudQueueMessage peekedMessage = await queue.PeekMessageAsync();
- dynamic FirstMesageInQueue = peekedMessage.AsString;
-
-
- CloudQueueMessage retrievedMessage = await queue.GetMessageAsync();
-
- await queue.DeleteMessageAsync(retrievedMessage);
-
-
- queue.Delete();
I hope it's helpful. Eat-> Code->Sleep->Repeat.