Share via


Azure WebJobs Storage Queues client library for .NET - version 5.3.3

This extension provides functionality for accessing Azure Storage Queues in Azure Functions.

Getting started

Install the package

Install the Storage Queues extension with NuGet:

dotnet add package Azure.WebJobs.Extensions.Storage.Queues

Prerequisites

You need an Azure subscription and a Storage Account to use this package.

To create a new Storage Account, you can use the Azure Portal, Azure PowerShell, or the Azure CLI. Here's an example using the Azure CLI:

az storage account create --name <your-resource-name> --resource-group <your-resource-group-name> --location westus --sku Standard_LRS

Authenticate the client

In order for the extension to access Queues, you will need the connection string which can be found in the Azure Portal or by using the Azure CLI snippet below.

az storage account show-connection-string -g <your-resource-group-name> -n <your-resource-name>

The connection string can be supplied through AzureWebJobsStorage app setting.

Key concepts

Using Queue trigger

The queue storage trigger runs a function as messages are added to Azure Queue storage.

Please follow the tutorial to learn about how to listen to queues in Azure Functions.

Using Queue binding

Azure Functions can create new Azure Queue storage messages by setting up an output binding.

Please follow the binding tutorial to learn about using this extension for producing messages into queues in Azure Functions.

Examples

Listening to queue

The following set of examples shows how to receive and react to messages that are being added to the queue.

Binding queue message to string

public static class QueueTriggerFunction_String
{
    [FunctionName("QueueTriggerFunction")]
    public static void Run(
        [QueueTrigger("sample-queue")] string message,
        ILogger logger)
    {
        logger.LogInformation("Received message from sample-queue, content={content}", message);
    }
}

Binding queue message to BinaryData

public static class QueueTriggerFunction_BinaryData
{
    [FunctionName("QueueTriggerFunction")]
    public static void Run(
        [QueueTrigger("sample-queue")] BinaryData message,
        ILogger logger)
    {
        logger.LogInformation("Received message from sample-queue, content={content}", message.ToString());
    }
}

Binding queue message to QueueMessage

public static class QueueTriggerFunction_QueueMessage
{
    [FunctionName("QueueTriggerFunction")]
    public static void Run(
        [QueueTrigger("sample-queue")] QueueMessage message,
        ILogger logger)
    {
        logger.LogInformation("Received message from sample-queue, content={content}", message.Body.ToString());
    }
}

Binding queue message to custom type

public static class QueueTriggerFunction_CustomObject
{
    public class CustomMessage
    {
        public string Content { get; set; }
    }

    [FunctionName("QueueTriggerFunction")]
    public static void Run(
        [QueueTrigger("sample-queue")] CustomMessage message,
        ILogger logger)
    {
        logger.LogInformation("Received message from sample-queue, content={content}", message.Content);
    }
}

Binding queue message to JObject

public static class QueueTriggerFunction_JObject
{
    [FunctionName("QueueTriggerFunction")]
    public static void Run(
        [QueueTrigger("sample-queue")] JObject message,
        ILogger logger)
    {
        logger.LogInformation("Received message from sample-queue, content={content}", message["content"]);
    }
}

Publishing messages to queue

The following set of examples shows how to add messages to queue by using Queue attribute.

The QueueTrigger is used just for sample completeness, i.e. any other trigger mechanism can be used instead.

Publishing message as string

public static class QueueSenderFunction_String_Return
{
    [FunctionName("QueueFunction")]
    [return: Queue("sample-queue-2")]
    public static string Run(
        [QueueTrigger("sample-queue-1")] string message,
        ILogger logger)
    {
        logger.LogInformation("Received message from sample-queue-1, content={content}", message);
        logger.LogInformation("Dispatching message to sample-queue-2");
        return message;
    }
}

Publishing message as BinaryData

public static class QueueSenderFunction_BinaryData_Return
{
    [FunctionName("QueueFunction")]
    [return: Queue("sample-queue-2")]
    public static BinaryData Run(
        [QueueTrigger("sample-queue-1")] BinaryData message,
        ILogger logger)
    {
        logger.LogInformation("Received message from sample-queue-1, content={content}", message.ToString());
        logger.LogInformation("Dispatching message to sample-queue-2");
        return message;
    }
}

Publishing message as QueueMessage

public static class QueueSenderFunction_QueueMessage_Return
{
    [FunctionName("QueueFunction")]
    [return: Queue("sample-queue-2")]
    public static QueueMessage Run(
        [QueueTrigger("sample-queue-1")] QueueMessage message,
        ILogger logger)
    {
        logger.LogInformation("Received message from sample-queue-1, content={content}", message.Body.ToString());
        logger.LogInformation("Dispatching message to sample-queue-2");
        return message;
    }
}

Publishing message as custom type through out parameter

public static class QueueSenderFunction_CustomObject_OutParamter
{
    public class CustomMessage
    {
        public string Content { get; set; }
    }

    [FunctionName("QueueFunction")]
    public static void Run(
        [QueueTrigger("sample-queue-1")] CustomMessage incomingMessage,
        [Queue("sample-queue-2")] out CustomMessage outgoingMessage,
        ILogger logger)
    {
        logger.LogInformation("Received message from sample-queue-1, content={content}", incomingMessage.Content);
        logger.LogInformation("Dispatching message to sample-queue-2");
        outgoingMessage = incomingMessage;
    }
}

Publishing message as custom type through collector

public static class QueueSenderFunction_CustomObject_Collector
{
    public class CustomMessage
    {
        public string Content { get; set; }
    }

    [FunctionName("QueueFunction")]
    public static void Run(
        [QueueTrigger("sample-queue-1")] CustomMessage incomingMessage,
        [Queue("sample-queue-2")] ICollector<CustomMessage> collector,
        ILogger logger)
    {
        logger.LogInformation("Received message from sample-queue-1, content={content}", incomingMessage.Content);
        logger.LogInformation("Dispatching message to sample-queue-2");
        collector.Add(incomingMessage);
    }
}

Accessing queue properties

public static class Function_BindingToQueueClient
{
    [FunctionName("QueueFunction")]
    public static async Task Run(
        [QueueTrigger("sample-queue")] string message,
        [Queue("sample-queue")] QueueClient queueClient,
        ILogger logger)
    {
        logger.LogInformation("Received message from sample-queue, content={content}", message);
        QueueProperties queueProperties = await queueClient.GetPropertiesAsync();
        logger.LogInformation("There are approximatelly {count} messages", queueProperties.ApproximateMessagesCount);
    }
}

Configuring the extension

Please refer to sample functions app.

Troubleshooting

Please refer to Monitor Azure Functions for troubleshooting guidance.

Next steps

Read the introduction to Azure Function or creating an Azure Function guide.

Contributing

See the Storage CONTRIBUTING.md for details on building, testing, and contributing to this library.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Impressions