0
0
AzureHow-ToBeginner · 4 min read

How to Use Queue Trigger Azure Function: Simple Guide

To use a queue trigger in an Azure Function, create a function that listens to an Azure Storage Queue and runs automatically when a new message arrives. You configure the trigger by specifying the queue name and connection string in the function's bindings. This lets your function process messages without manual intervention.
📐

Syntax

The queue trigger in Azure Functions uses a binding that connects your function to an Azure Storage Queue. You specify the queue name and the storage account connection string. When a new message appears in the queue, the function runs automatically with the message content as input.

  • queueName: The name of the Azure Storage Queue to listen to.
  • connection: The name of the app setting holding the storage account connection string.
  • function parameter: Receives the queue message content.
csharp
public static class QueueTriggerFunction {
    [FunctionName("QueueTriggerFunction")]
    public static void Run(
        [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
        ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
    }
}
💻

Example

This example shows a complete Azure Function in C# that triggers when a message is added to the queue named myqueue-items. It logs the message content to the console.

csharp
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class QueueTriggerFunction {
    [FunctionName("QueueTriggerFunction")]
    public static void Run(
        [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
        ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
    }
}
Output
C# Queue trigger function processed: Hello from queue!
⚠️

Common Pitfalls

  • Not setting the correct connection string in the local.settings.json or Azure Function app settings causes the function to fail connecting to the queue.
  • Using the wrong queue name or a queue that does not exist will prevent the trigger from firing.
  • Forgetting to add the QueueTrigger attribute or misspelling it will cause the function not to trigger.
  • Not handling exceptions inside the function can cause message retries and possible poison messages.
csharp
/* Wrong: Missing connection string attribute */
[QueueTrigger("myqueue-items")] string myQueueItem

/* Right: Include connection string attribute */
[QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem
📊

Quick Reference

Remember these key points when using queue triggers:

  • Set queueName to your Azure Storage Queue name.
  • Set Connection to the app setting holding your storage account connection string.
  • Function runs automatically when a new message arrives.
  • Use logging to track message processing.
  • Handle errors to avoid message loss or infinite retries.

Key Takeaways

Configure the queue trigger with the correct queue name and storage connection string.
The function runs automatically when a new message arrives in the specified queue.
Always handle exceptions inside the function to prevent message loss or retries.
Use logging to monitor the processing of queue messages.
Verify your Azure Storage Queue exists and connection strings are correct before deploying.