Azure Functions Queue Storage Trigger (C#)

This document details how to use the Queue Storage trigger for Azure Functions written in C#.

Overview

The Queue Storage trigger starts a Function when a message is added to a specific Azure Queue Storage queue. This is a common pattern for processing asynchronous tasks, handling work queues, and decoupling services.

Trigger Configuration

The Queue Storage trigger is configured using an attribute in your C# function code or through the function.json file.

C# Attribute Configuration

The most common way to define a Queue Storage trigger in C# is by using the QueueTrigger attribute.

C#
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace MyFunctionApp
{
    public static class QueueProcessor
    {
        [FunctionName("ProcessQueueMessage")]
        public static void Run(
            [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
            ILogger log)
        {
            log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

            // Your processing logic here
            // For example, parse the message, interact with other services, etc.
        }
    }
}

Attribute Parameters

function.json Configuration

Alternatively, you can configure the trigger declaratively in the function.json file. This is less common for C# code-first development but is useful for other languages or when using certain deployment methods.

function.json
{
  "scriptFile": "__init__.py", // For Python, adjust for C# if needed or rely on attribute
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Note: When using C# attributes, the function.json is generated automatically. If you are manually creating function.json, ensure the name property matches the parameter name in your C# method.

Input Binding

The message content from the queue is passed as the input to your function. The type of the input parameter can be:

POCO Deserialization Example

Define a C# class that matches the structure of your queued messages.

C#
public class MyQueueMessage
{
    public string Id { get; set; }
    public string Data { get; set; }
}

Update your function signature to accept this POCO:

C#
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace MyFunctionApp
{
    public static class QueueProcessor
    {
        [FunctionName("ProcessQueueMessagePOCO")]
        public static void Run(
            [QueueTrigger("myqueue-items-json", Connection = "AzureWebJobsStorage")] MyQueueMessage myQueueItem,
            ILogger log)
        {
            log.LogInformation($"C# Queue trigger function processed message ID: {myQueueItem.Id}");
            log.LogInformation($"Data: {myQueueItem.Data}");
        }
    }
}

Function Execution and Message Handling

When a message is successfully processed by your function, it is automatically deleted from the queue.

If your function throws an unhandled exception:

Queue Properties and Deletion

The trigger automatically handles the visibility timeout and deletion of messages. You do not need to manually manage this for successful executions. For advanced scenarios where you need more control, consider using the Queue Storage SDK directly within your function.

Connection String Configuration

Ensure your Azure Storage connection string is correctly configured. For local development, this is typically done in the local.settings.json file under the Values section:

local.settings.json
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=YOUR_STORAGE_ACCOUNT_NAME;AccountKey=YOUR_STORAGE_ACCOUNT_KEY;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

In Azure, you configure this in the Function App's "Configuration" settings.

Best Practices

The Queue Storage trigger polls the queue for new messages. There might be a slight delay between a message appearing in the queue and your function being triggered.