Azure Functions

Queue Triggers

Queue triggers allow you to create Azure Functions that execute in response to messages being added to an Azure Storage Queue or an Azure Service Bus Queue.

When to Use Queue Triggers

Queue triggers are ideal for implementing background processing, asynchronous workflows, and decoupling applications. When a message appears in a monitored queue, your function automatically runs to process that message.

Supported Queue Providers

Configuring a Queue Trigger

A queue trigger is defined by the {queueName} binding expression and connection string settings.

Azure Storage Queue Trigger Example (C#)

This example shows a C# function triggered by messages in a storage queue named myqueue-items.

using System;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace MyCompany.MyFunctions
{
    public class QueueTriggerCSharp
    {
        private readonly ILogger _logger;

        public QueueTriggerCSharp(ILogger logger)
        {
            _logger = logger;
        }

        [Function("QueueTriggerCSharp")]
        public void Run(
            [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem)
        {
            _logger.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

            // Add your message processing logic here
            // For example, parse the message, update a database, send an email, etc.
        }
    }
}
            

Azure Service Bus Queue Trigger Example (JavaScript)

This example shows a JavaScript function triggered by messages in a Service Bus queue named my-servicebus-queue.

module.exports = async function (context, mySbMsg) {
    context.log('JavaScript ServiceBus queue trigger function processed work item', mySbMsg);
    context.log(`body is: ${mySbMsg.body}`);
    context.log(`message id is: ${mySbMsg.messageId}`);

    // Add your message processing logic here
};
            

Connection Strings

The Connection property in the attribute (e.g., "AzureWebJobsStorage") refers to an application setting that contains the connection string for the queue service. For Azure Storage Queues, this is typically the default connection string pointing to your Storage Account.

For Service Bus, you'll need to configure a specific connection string setting, such as ServiceBusConnectionString.

Important Note on Processing

Queue triggers process messages one at a time by default. If a function execution fails, the message will typically remain in the queue to be retried later (depending on your queue configuration and retry policies). For critical operations, consider implementing idempotency in your function to handle potential duplicate message processing.

Key Concepts

Advanced Scenarios