Output bindings for Azure Queue Storage allow your Azure Functions to send messages to an Azure Queue without needing to write explicit SDK code. This simplifies your function code and improves its readability.
When you define a queue output binding, you specify the Azure Queue Storage connection string and the queue name. The binding then provides a mechanism to send data to that queue.
How it Works
You define the output binding in your function's function.json
file. The runtime then injects an output binding object into your function code. You can use this object to send data to the specified queue.
Example: C#
In C#, you typically use the ICollector<T>
or IAsyncCollector<T>
interface to send messages.
function.json
{
"scriptFile": "MyQueueFunction.cs",
"bindings": [
{
"name": "myQueueTrigger",
"type": "queueTrigger",
"direction": "in",
"queueName": "myinputqueue",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputQueue",
"type": "queue",
"direction": "out",
"queueName": "myoutputqueue",
"connection": "AzureWebJobsStorage"
}
]
}
C# Function Code
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
public static class MyQueueFunction
{
[FunctionName("MyQueueFunction")]
public static void Run(
[QueueTrigger("myinputqueue")] string myQueueItem,
[Queue("myoutputqueue")] out string outputQueue,
ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
// Process the incoming message and create an output message
outputQueue = $"Processed: {myQueueItem.ToUpper()}";
log.LogInformation($"Sent message to output queue: {outputQueue}");
}
}
C# Function Code (Async Collector)
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
public static class MyQueueFunctionAsync
{
[FunctionName("MyQueueFunctionAsync")]
public static async Task RunAsync(
[QueueTrigger("myinputqueue")] string myQueueItem,
[Queue("myoutputqueue")] IAsyncCollector<string> outputQueue,
ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
var processedMessage = $"Processed Async: {myQueueItem.ToLower()}";
await outputQueue.AddAsync(processedMessage);
log.LogInformation($"Sent message to output queue asynchronously: {processedMessage}");
}
}
Example: JavaScript
In JavaScript (Node.js), you access the output binding via the context.bindings
object.
function.json
{
"scriptFile": "index.js",
"bindings": [
{
"name": "myQueueTrigger",
"type": "queueTrigger",
"direction": "in",
"queueName": "myinputqueue",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputQueue",
"type": "queue",
"direction": "out",
"queueName": "myoutputqueue",
"connection": "AzureWebJobsStorage"
}
]
}
JavaScript Function Code
module.exports = async function (context, myQueueItem) {
context.log('JavaScript queue trigger function processed work item', myQueueItem);
// Create an output message
const outputMessage = `Processed by JS: ${myQueueItem.toUpperCase()}`;
// Assign to the output binding
context.bindings.outputQueue = outputMessage;
context.log('Sent message to output queue:', outputMessage);
};
JavaScript Function Code (Multiple Outputs)
You can also send multiple messages to the same output queue by assigning an array.
module.exports = async function (context, myQueueItem) {
context.log('JavaScript queue trigger function processed work item', myQueueItem);
// Create multiple output messages
const outputMessages = [
`First part: ${myQueueItem}`,
`Second part: ${myQueueItem.split('').reverse().join('')}`
];
// Assign the array to the output binding
context.bindings.outputQueue = outputMessages;
context.log('Sent multiple messages to output queue:', outputMessages);
};
Configuration Details
type
: Must bequeue
for an output binding.direction
: Must beout
.queueName
: The name of the Azure Queue Storage queue to send messages to.connection
: The name of the application setting that contains the Azure Queue Storage connection string. Defaults toAzureWebJobsStorage
if not specified.
Key Benefits
- Simplified Code: No need to manage Azure SDK clients or connection strings within your function logic.
- Declarative Configuration: Bindings are defined in
function.json
, making it easy to see and manage dependencies. - Scalability: Azure Functions runtime manages the interactions with Queue Storage, ensuring efficient and scalable message delivery.