Walkthrough: Azure Functions with an Azure Storage Queue Trigger

Azure Functions Queues
When it comes to building high-throughput applications, Azure provides a serverless computing technique called Azure Functions to run instances of short-running programs in a cost-effective manner.

One of the most common ways to trigger an Azure Function is with a Queue Trigger. When messages are added to a storage queue, your Function will be triggered (sometime after the message is added), as Azure will automatically dequeue the first message and provide it to your Function as a parameter.

In such a way, you can build systems which respond dynamically to an increase in demand, assuring that the data is safely, yet momentarily, stored in a queue.

To complete this walkthrough, you should familiarise yourself with queues:

You will need...

  1. Visual Studio (preferably Professional 2019 or better)

  2. Azure Storage Emulator (VS component)

  3. .NET Core 3.1 SDK

  4. Azure subscription (to create resources)

What we will create

  1. An Azure Storage Account

  2. A queue for our data

  3. A console application for adding messages to the queue

  4. A triggered function to process our data

  5. A Function App in Azure for deployment

Configuring Azure

The first step is to configure our storage account and queue in Azure.

Setting up a Storage Account

Open your web browser and navigate to portal.azure.com

Firstly, create a new resource group and navigate to it.

Click the + Add button and search for Storage account.
In the list, you should find Storage account by Microsoft.
Select this, and click the Create button.

On the screen entitled Create storage account, fill in the required fields, making a careful note of your chosen Storage account name.
Because we want to use queues, we should select a general purpose v2 storage account.
To save costs during this test, I would suggest a Replication selection of LRS.
Follow the wizard to Create your storage account.

Deployment should take a few moments. When finished, click Go to resource

Inside the storage account, if we scroll down in the menu on the left hand side, we can see File service (File shares), Table service (Restaurant style...) and Queue service (Queues).

For this experiment, we will use File shares and Queues.

Create a queue

Start off by clicking on Queues in the menu for the Storage account.

Click the + Queue button, then type a name for the queue, and click OK.

In this example, we'll use a queue name of my-message-queue.

Locate the connection string

Under Settings, there is also a menu item named Access keys.

In this section, you'll find key1 and key2.
Clicking the Show keys button will reveal a Connection string for each.

NOTE: We will use this connection string later!

Programming the console application

Get Visual Studio opened up, and create a blank solution.

I always prefer to start off with a blank solution, where the name of it is your namespace root.

Right click on the solution and Add > New Project...

Find Console App (.NET Core) and click Next, making sure we've selected C# as our programming language.

Give it a name, e.g. MyAzureSolution.MessagePoster

Click Create.

Add NuGet packages

Right click on your project, and Manage NuGet Packages...

You need to install the following two NuGet packages:

  • Azure.Storage.Queues

  • Newtonsoft.Json

Coding Program.cs

As we prefer asynchronous programming, modify your Main entry point to be async:

static async Task Main(string[] args)
// code goes here

Add the necessary using directives to the top of the file:

using System.Text;
using System.Threading.Tasks;
using Azure.Storage.Queues;
using Newtonsoft.Json;

Add code to construct a QueueClient object, specifying your connection string as the first parameter, and the queue name (my-message-queue) as the second.

var client = new QueueClient(CONNECTION_STRING, "my-message-queue");

My connection string is hidden through the use of this constant. In reality, you'd be using an Environment Variable or pulling this from Azure Key Vault. For now, this is ok as we are just testing the system.

How to place messages in the queue

IMPORTANT: In order for data to be passed to your Azure Function successfully, your data must be representable as UTF-8 and base-64 encoded.
See: docs.microsoft.com

For convenience, let's add a function to our Program class.

private static string Base64Encode(string str)
var bytes = Encoding.UTF8.GetBytes(str);

return Convert.ToBase64String(bytes);

Although the encoded data must be less than 64KB in size, we can still send a class as a message, and receive this class the other end, in our Azure Function.

Right click on the project and Add > Class...

Call it MyMessageClass and click Add.

Let's make the class public, and add some properties and constructors:

public class MyMessageClass
public int ID { get; set; }
public string Content { get; set; }

public MyMessageClass() { }

public MyMessageClass(int id, string content)
this.ID = id;
this.Content = content;

Ok, back to our Program class. Let's declare a variable for our message:

var message = new MyMessageClass(123, "My awesome message content!");

Then, we will serialize this object to JSON and base-64 code the string as preparation for adding to the message queue.

var data = Program.Base64Encode(JsonConvert.SerializeObject(message));

Finally, send the encoded data to the queue, using the client.

await client.SendMessageAsync(data);

Build your Program and run it. On completion, go back to the portal.

Locate the queue message

Open your storage account resource in Azure, go to Queues, and click on the target queue (my-message-queue).

Here, you should see the message, with the JSON object that you sent. Notice that the message does not show in base-64 encoded format.

Create a Function App application

Back in Visual Studio, right click on the project and Add > New Project...

Find the Azure Functions project template (C# / Azure / Cloud) and click Next.

Give it a name, e.g. MyAzureSolution.TriggeredFunctions

Click Create.

On the next screen, ensure that Azure Functions v3 (.NET Core) is selected, and then select Queue trigger in the list.

Ensure to select Storage account Storage emulator to allow for easy local testing.

Leave the Connection string setting name blank, it is not necessary, as a Function App is usually connected to exactly one storage acount.

For Queue name, specify the queue name that we created earlier (my-message-queue).

When you're ready, click Create.

Coding the Function

By default, the Azure Function template contains one class (Function1). You can easily rename this class, but it is the [FunctionName("...")] attribute that dictates the name of the Function shown in Azure.

Remove the Connection = "" property from the [QueueTrigger] attribute - it is not needed.

We do, however, need to specify the actual connection string on our configuration file.

Setting the connection string

The project contains a file called local.settings.json. This file is used only locally, and is not checked in with Git or used by Azure.

The file contains a single JSON object with a property "Values", and inside that, "AzureWebJobsStorage".

Replace the default value ("UseDevelopmentStorage=true") with the connection string to your storage account, that we used earlier.

Build and run

Build the Azure Functions project and then run it in Debug mode. You should see that Azure Storage Emulator starts up and runs in the background.

Basically, this is a console application which is hosting your Azure Function.

Azure Functions Core Tools runs the project and locates all the Functions inside this project, and what type of triggers they have.

You should see Functions: Function1: queueTrigger

Then, as we have one message in our queue (from our earlier run of the MessagePoster), the Function will be immediately triggered and you'll see a message that says:

C# Queue trigger function processed: {"ID":123,"Content":"My awesome message content!"}

If you've got this far - congratulations, you've created your first queue-triggered Azure Function!

Deploying the Function App to Azure

Now that our basic coding is done, we want to deploy our TriggeredFunctions project to Azure, so we can test it without using the Azure Storage Emulator.

Create a Function App in Azure

Go to the portal (portal.azure.com) again and open the resource group that you were working in earlier.

Click + Add and search for Function App, then click Create.

Give it a name. This must be unique (a subdomain of .azurewebsites.net).

Publish - Code
Runtime stack - .NET Core
Version - 3.1

Click Next: Hosting >

Now, locate your storage account, or Create new if there are none listed.

For the Operating system you should select Windows if you want Severless technology.

This leads you to Plan: Consumption (Serverless)

Click Review + create

If validation passes, you can go ahead and click Create.

Once the resource is created, click Go to resource

Connection strings in the Function App

Under Settings, click the Configuration menu item.

Locate the application setting "AzureWebJobsStorage" and then "Click to show value".

You'll see here that the account name and account key have already been populated for you, as you selected your storage account during the creation process.

Publish your Function App

Go back to Visual Studio, and right click on your TriggeredFunctions project and click Publish.

Target: Azure


Specific tagret: Azure Function App (Windows)


Functions instance: Select the function app you created in Azure.

You can leave Run from package file (recommended) checked.


When the Publishing window opens, just click Publish.

It will take a minute or so.

Verify in Azure

Back in the portal, find your Function App.

In the menu, click on Functions.

Here, you should see Function1, with a Trigger type "Queue", Status "Enabled".

If you do, great job, you're ready to do the final test.

Test the solution

Go back to Visual Studio (one last time...) and run your MessagePoster console application.

After it completes, come back to the portal.

View the log files storage

Inside the portal, locate your storage account and go inside this resource.

Under File service in the menu, click File shares.

Click the function app name in the File shares list, which has been created for you.

Navigate through the following directories:

LogFiles > Application > Functions > Function > Function1

You should here see a *.log file, the timestamp with some hexadecimal suffix.

Click this file, and then click Download. Open it up in Notepad.

If you've done everything correctly, you will see the following in the log...

C# Queue trigger function processed: {"ID":123,"Content":"My awesome message content!"}

Great job!

If you completed this whole task, then you're one step closer to mastering Azure Functions.

Of course, in reality, we would want to deserialize our JSON content into an object and perform necessary tasks based on this content.

Tips and tricks

  • Keep execution time short, 30 seconds or less.

  • Use the [FunctionName("...")] attribute well

  • Group your static functions into separate classes

Happy coding! Please remember to support me by leaving a comment or sharing this article 😁
Hey you! I need your help!

Thanks for reading! All the content on this site is free, but I need your help to spread the word. Please support me by:

  1. Sharing my page on Facebook
  2. Tweeting my page on Twitter
  3. Posting my page on LinkedIn
  4. Bookmarking this site and returning in the near future
Thank you!