Azure Functions Queue Storage Output Bindings (C#)

This article explains how to use the Queue Storage output binding with Azure Functions written in C#. This binding allows your function to write messages to an Azure Queue Storage queue.

Note: This article focuses on the C# programming model for Azure Functions. For information on other languages, see the language-specific documentation.

Binding Configuration

The Queue Storage output binding is configured in the function.json file (for v1 and v2 programming models) or via attributes in your C# code (for v3 and later programming models).

function.json (v1/v2 Model)

The following is an example of a function.json configuration for an output binding:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "queue",
      "direction": "out",
      "queueName": "outqueue",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Properties:

C# Attributes (v3+ Model)

In the C# v3+ programming model, you use attributes to define bindings.

The Microsoft.Azure.WebJobs.QueueAttribute attribute is used to configure the output binding. You can also use Microsoft.Azure.WebJobs.Storage.Queue.IAsyncCollector<string> or Microsoft.Azure.WebJobs.Storage.Queue.ICollector<string> as the parameter type.

using Microsoft.Azure.WebJobs;

            public static class QueueOutputFunction
            {
                [FunctionName("QueueOutputTrigger")]
                public static void Run(
                    [QueueTrigger("inqueue")] string myQueueItem,
                    [Queue("outqueue", Connection = "AzureWebJobsStorage")] out string outputQueueItem)
                {
                    outputQueueItem = $"Message added to output queue: {myQueueItem}";
                    // Log any additional information if needed
                }
            }
            

Alternatively, you can use IAsyncCollector for asynchronous operations:

using Microsoft.Azure.WebJobs;
            using Microsoft.Azure.WebJobs.Extensions.Storage; // For IAsyncCollector
            using System.Threading.Tasks;

            public static class QueueOutputAsyncFunction
            {
                [FunctionName("QueueOutputAsyncTrigger")]
                public static async Task Run(
                    [QueueTrigger("inqueue-async")] string myQueueItem,
                    [Queue("outqueue-async", Connection = "AzureWebJobsStorage")] IAsyncCollector<string> outputQueueItem)
                {
                    await outputQueueItem.AddAsync($"Asynchronous message to output queue: {myQueueItem}");
                }
            }
            

Example: Writing to a Queue

Consider a function that is triggered by a message on an inqueue and writes a modified message to an outqueue. This is a common pattern for message processing pipelines.

C# Code (v3+ Model)

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

            public static class ProcessMessageAndOutput
            {
                [FunctionName("ProcessMessageAndOutput")]
                public static void Run(
                    [QueueTrigger("requests", Connection = "AzureWebJobsStorage")] string requestMessage,
                    [Queue("responses", Connection = "AzureWebJobsStorage")] out string responseMessage,
                    ILogger log)
                {
                    log.LogInformation($"Processing request: {requestMessage}");

                    // Process the request message...
                    string processedData = requestMessage.ToUpper();

                    // Create the response message
                    responseMessage = $"Processed: {processedData}";

                    log.LogInformation($"Response sent to queue: {responseMessage}");
                }
            }
            

In this example:

Handling Different Data Types

By default, the Queue Storage output binding handles strings. If you need to send complex objects, you can serialize them to JSON strings before sending them to the queue.

Example: Sending a JSON Object

using Microsoft.Azure.WebJobs;
            using Microsoft.Extensions.Logging;
            using Newtonsoft.Json; // Make sure to add the Newtonsoft.Json NuGet package

            public class CustomMessage
            {
                public string Id { get; set; }
                public string Payload { get; set; }
            }

            public static class SendJsonObjectFunction
            {
                [FunctionName("SendJsonObjectFunction")]
                public static void Run(
                    [QueueTrigger("input-json")] string inputJson,
                    [Queue("output-json", Connection = "AzureWebJobsStorage")] out string outputJson,
                    ILogger log)
                {
                    log.LogInformation($"Received JSON: {inputJson}");

                    var messageObject = JsonConvert.DeserializeObject<CustomMessage>(inputJson);

                    // Modify the object
                    messageObject.Payload = messageObject.Payload.ToUpper();

                    // Serialize back to JSON for sending to the output queue
                    outputJson = JsonConvert.SerializeObject(messageObject);

                    log.LogInformation($"Sending JSON to output queue: {outputJson}");
                }
            }
            
Tip: Ensure you have the Microsoft.Azure.WebJobs.Extensions.Storage NuGet package installed for the Queue Storage bindings and Newtonsoft.Json for JSON serialization.

Key Considerations

This article provides a comprehensive overview of using Queue Storage output bindings in Azure Functions with C#. Refer to the official Azure documentation for more advanced configurations and scenarios.