Azure Queue Storage Using Development Storage Account

Azure Storage Explorer and Azure Storage Emulator

 
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.
 
Figure(1) 
 

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.
  1. 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.
  2. 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.
  1. using Microsoft.WindowsAzure.Storage;  
  2. 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.
  1. <connectionStrings>  
  2.    <add name="azurestorageconfigurationkey" connectionString="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==" providerName="System.Data.SqlClient"/>  
  3. </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. 
  1. 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. 
  2. 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 
  1. //Creating Queue  
  2. CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;  
  3. //Deafult Connection string reading from webconfig.  
  4. string connectionString = ConfigurationManager.ConnectionStrings["azurestorageconfigurationkey"].ToString();  
  5. CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();  
  6. CloudQueue queue = queueClient.GetQueueReference("demoqueue");  
  7. // If not exist then Create Queue.  
  8. await queue.CreateIfNotExistsAsync();  
  9.    
  10. //We are Created a message and add it to the queue using bellow code  
  11. CloudQueueMessage message = new CloudQueueMessage("Hello, C# Corner");  
  12. await queue.AddMessageAsync(message);  
  13.    
  14. // Peek the next message in the queue.  
  15. CloudQueueMessage peekedMessage = await queue.PeekMessageAsync();  
  16. dynamic FirstMesageInQueue = peekedMessage.AsString;  
  17.    
  18. // Get the next message in the queue.  
  19. CloudQueueMessage retrievedMessage = await queue.GetMessageAsync();  
  20. // Then delete the message.  
  21. await queue.DeleteMessageAsync(retrievedMessage);  
  22.    
  23. // Delete the queue.  
  24. queue.Delete();  
I hope it's helpful. Eat-> Code->Sleep->Repeat.