Azure Queue with Azure Functions – Part I

Azure Queue with Azure Functions – Part I

We are all aware of how much powerful and useful can Azure function can be. But it is more astonishing to know that it can be more powerful if we use Azure Queue with it.

In this blog, I will focus on how to integrate Azure Queues with Azure functions/Timer trigger.

If you want to know more about Azure Queue and Azure Function individually then please first check the below links:

Before moving forward, we will need an Azure account. We can create a free account or use pay-as-you-go or any subscription if we have.

Let get started by first creating the Azure Function by selection New(new) and search for function app. Once we get the app click on create. We will get a popup as shown in below figure.

Azure1

We will fill the details for the app and click on create.

Once the app is created, we can navigate to the app. At first, there would be no functions. We need to create a function to process Azure Queues.

We will first create an Azure Timer Trigger which will create a message in Azure Queue.

Left Pane

We will create the Function by clicking the + sign beside Functions and then select Timer with C# and click Create the function.

CreateFunction

Once the Timer Function is created, we will get code editor opened for the Timer Function as shown below.

TriggerFunction.PNG

We will now try to get the output in Azure Queue. We will start by setting up our output variable. We will do this by clicking on integrate (Intigrate).

We will see options opened in the right pane of the page.

RightPaneWindow

We will now add the new output by clicking the “New Output” button and selecting Azure Queue Storage from the shown screen.

RightPaneWindow2Click Select to select the output variable type.

After we select, we will find the below screen displayed.

RightPaneWindow3

We need to provide three details to create the output variable:

  1. Message parameter name: It will be the name for our output variable.
  2. Queue name: This will be the Queue where our message will be stored. If the Queue does not exist, it will create the Queue.
  3. Storage account connection: In this, we need to select the storage where will our Queue be stored. You can create a new storage account by click on new.

Once we are done with providing the info, we will click on save to save our output variable details. After we save we can see our output variable in Outputs.

Outputsave

We will now come back to our code editor.  We will first create a QueueMessage class which will the data type for our output variable.

public class QueueMessage{
   public string Message{get;set;}
   public string Time{get;set;}
}

Message and Time are the default property of Azure Queue Message.

After creating the class, we will add the variable as an output parameter to the Run function.

Let us create an object of type QueueMessage and store dummy text in the message and time will be the current time. After we assign the values to the object, we will pass it to the output variable. After the changes, our code will look as below.

using System;
public static void Run(TimerInfo myTimer, TraceWriter log,out QueueMessage outputQueueItem){
   var date=DateTime.Now.ToString("hh:mm:ss");
   var queueItem=new MyQueueItem();
   queueItem.Time=date;
   queueItem.Message=date;
   outputQueueItem=queueItem;
}
public class QueueMessage{
   public string Message{get;set;}
   public string Time{get;set;}
}

When we run the function, a Queue Message will be created in the QueueStorage>Queue.

We can verify the data by Cloud Explorer in Visual Studio 2017.

We will see that in our next part of the blog.

Azure Queue with Azure Functions – Part II

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s