Azure WebJobs Storage Queues client library for .NET - version 5.3.3
This extension provides functionality for accessing Azure Storage Queues in Azure Functions.
Install the Storage Queues extension with NuGet:
dotnet add package Azure.WebJobs.Extensions.Storage.Queues
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
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.
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.
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.
The following set of examples shows how to receive and react to messages that are being added to the queue.
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);
}
}
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());
}
}
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());
}
}
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);
}
}
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"]);
}
}
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.
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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);
}
}
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);
}
}
Please refer to sample functions app.
Please refer to Monitor Azure Functions for troubleshooting guidance.
Read the introduction to Azure Function or creating an Azure Function guide.
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.
Azure SDK for .NET feedback
Azure SDK for .NET is an open source project. Select a link to provide feedback: