Working With Queue Storage Trigger Azure Functions​

Introduction

 
These days, Serverless computing is one of the key terms in the Cloud. This is a PaaS (Platform as a Service). It allows us to deploy and manage individual code functions without a need to deploy and manage on the individual Virtual Machine (VM). Azure Functions is a serverless compute service which runs our code on demand without needing to host it on the server and managing infrastructure. Azure Function allows us to write code in various languages, such as C#, F#, Node.js, Java, or PHP. It can be trigger from a variety of events such as HTTPTrigger, TimerTrigger, QueueTrigger etc. In my previous article, we learned about creating Azure function and look at HTTP triggered function and function created using the Azure portal. In this article, we will learn about Queue storage triggered Azure function.
 
Azure queue storage provides messaging between application components. It helps the application component to be decoupled and scale independently. It delivers asynchronous messaging for communication between application components. It can be stored large numbers of messages that can be accessed from anywhere over HTTP or HTTPS via authenticated call. The single message size is up to 64kb and queue contains a large number of messages up to a total capacity of the storage account.
 
In my previous article, I had shown how to create function app. When we create function app, it automatically deployed, and we can create functions under the function app.
 
Working With Queue Storage Trigger Azure Functions​
 
Now, expand the function app and click the "+" button. If it is the first function under the function app, select "In-portal" and click "Continue".
 
Alternatively, you can also create a function using editors, such as Visual Studio or Azure CLI.
 
Working With Queue Storage Trigger Azure Functions​
 
Now select "More templates" then click "Finish and view templates".
 
Working With Queue Storage Trigger Azure Functions​
 
Search field type "Queue" and select "Azure Queue Storage trigger" template.
 
Working With Queue Storage Trigger Azure Functions​
 
This template required "Microsoft.Azure.WebJobs.Extensions.Storage" extensions. If it is not installed, install the extension and click "Continue" after finish extension installation.
 
Working With Queue Storage Trigger Azure Functions​
 
Working With Queue Storage Trigger Azure Functions​
 
Now, fill up the following fields shown in the image. We need to fill the Name, Queue name, and Storage account connection information and click on the "Create" button.
 
Working With Queue Storage Trigger Azure Functions​
 
Once the function is created, you can view/edit function information such message parameter name, queue name, etc. Also, you can see the account name and key under the Documentation tab.
 
Working With Queue Storage Trigger Azure Functions​
 
Now, the function is ready, and is triggered when a new message is available in the Queue named "testqueue". To test our function, we can either use Microsoft Azure Storage Explorer tool or create own C# code.
 
Azure Storage Explorer is a tool that helps us to easily work with Azure storage data on different platform such as windows, macOS, and Linux. We can connect to our subscription and manipulate our queues, files, tables, and blobs using this tool. Additionally, we can also manipulate Azure Cosmos DB Storage and Azure Data Lake Storage using this tool.
 
After Azure Storage Explorer tool has installed successfully, launch this tool and click on "Connection" icon on the left, select "Use a storage account name and key" option, and click "Next".
 
Working With Queue Storage Trigger Azure Functions​
 
Now, enter account name and key that generate when we create a function in Azure portal, click on "Next". It will show the connection summary and then, click on "Connect".
 
Working With Queue Storage Trigger Azure Functions​
 
Working With Queue Storage Trigger Azure Functions​
 
Expand "Storage Account", we find our function under this tree. Expand function and right click on "Queue" then enter the name of the queue to that we want to send a message.
 
Working With Queue Storage Trigger Azure Functions​
 
Now, we have configured our storage queue and we can test functionality by adding a message to queue (testqueue). From left panel of Storage Explorer, select a queue that you need to send a message and on the right panel, click "Add Message". Type your message to message textbox and click "Ok".
 
Working With Queue Storage Trigger Azure Functions​
 
Now, the message added to the queue and when you look at function log and verify that new message has been read by the function from the queue.
 
Working With Queue Storage Trigger Azure Functions​
 
Alternatively, we can also test our function using .NET Code. To display the concept, I have created a Console App (.NET framework). We need to add the following two packages to our project.
The following code is used to send message to the queue using C# code. The GetSetting method of CloudConfigurationManager class is used to retrieve a connection string from a configuration file and Parse method CloudStorageAccount used to get the reference of a storage account using a connection string. Using CreateCloudQueueClient method of the storage account, we can retrieve queues stored in Queue storage and using GetQueueReference method of CloudQueue class, we can get specific queue reference. Using method CreateIfNotExists, we can create a queue if it does not exist.
  1. using Microsoft.Azure;  
  2. using Microsoft.WindowsAzure.Storage;  
  3. using Microsoft.WindowsAzure.Storage.Queue;  
  4. using System;  
  5.   
  6. namespace AzureStorageQueueTest  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             // Parse the connection string   
  13.             // Return a reference to the storage account.  
  14.             CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));  
  15.   
  16.             // Create the queue client.  
  17.             CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();  
  18.   
  19.             // Retrieve queue reference from the container  
  20.             CloudQueue queue = queueClient.GetQueueReference("testqueue");  
  21.   
  22.             // Create queue if it does not exist  
  23.             queue.CreateIfNotExists();  
  24.   
  25.             //Create message   
  26.             CloudQueueMessage message = new CloudQueueMessage("Hello, Jignesh Trivedi");  
  27.   
  28.             //Add message to queue  
  29.             queue.AddMessage(message);  
  30.   
  31.             Console.WriteLine("Message sent");  
  32.             Console.ReadKey();  
  33.         }  
  34.     }  
  35. }  

Summary

 
In this article, we learned how to trigger Azure Functions using Azure Queue Storage service. Azure Queue Storage provides messaging between application components. It helps application components to be decoupled and scale independently. It can store large numbers of messages that can be accessed from anywhere over HTTP or HTTPS via authenticated call.


Similar Articles