Azure Functions Triggers and Bindings - Part 1 - Queue

2023, Jan 11

This is part 1 of the Azure Functions Triggers and Bindings series where Triggers and Bindings are explored with C# examples. In this post, Azure Queues are explored with In-Process and Isolated Functions.

Triggers and Bindings

There are 2 use cases that we can take advantage of when dealing with Azure Queues1 on Azure Functions:

  • Listen to queues with QueueTrigger, applied to both In-Process and Isolated models.
  • Save messages to the queue with Queue and QueueOutput, an output binding applied to In-Process and Isolated models respectively.

Both triggers and bindings are explored below with In-Process and Isolated model examples. Both bind to types from the NuGet package Azure.Storage.Queues, with slightly different implementations.

Triggers

A single trigger QueueTrigger is available in both implementations.

In-Process Triggers

The In-Process model leverages the NuGet package Microsoft.Azure.WebJobs.Extensions.Storage, version 5.0.1 (currently).

Trigger.cs

public class Trigger
{
    [FunctionName("Trigger")]
    public void Run([QueueTrigger("alert-queue", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
    }
}

Isolated Triggers

The Isolated model leverages the NuGet package Microsoft.Azure.Functions.Worker.Extensions.Storage, version 5.0.1 (currently).

Trigger.cs

public class Trigger
{
    private readonly ILogger _logger;

    public Trigger(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<Trigger>();
    }

    [Function("Trigger")]
    public void Run([QueueTrigger("alert-queue", Connection = "AzureWebJobsStorage")] string myQueueItem)
    {
        _logger.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
    }
}

Testing Triggers

To validate the triggers, either start the In-process or Isolated model, then submit a message to the queue by using Azure Storage Explorer.

Storage Explorer

  1. Create the queue
  2. Click to add a message
  3. Type the message text
  4. Click OK to add the message to the queue

This should trigger the QueueTrigger functions.

Bindings

There is only a single binding with queues, the Output Queue.

We make use of an HttpTrigger only to exemplify the output queue.

In-Process Bindings

Output.cs

public static class Output
{
    [FunctionName("Output")]
    [return: Queue("alert-queue")]
    public static async Task<string> RunAsync(
        [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        string message = await new StreamReader(req.Body).ReadToEndAsync();

        return message;
    }
}

Isolated Bindings

Output.cs

public class Output
{
    private readonly ILogger _logger;

    public Output(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<Output>();
    }

    [Function("Output")]
    [QueueOutput("alert-queue")]
    public async Task<string> RunAsync([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req)
    {
        _logger.LogInformation("C# HTTP trigger function processed a request.");

        string message = await new StreamReader(req.Body).ReadToEndAsync();

        return message;
    }
}

Testing Bindings

Submit a POST request to validate the binding. This is possible because we are using the HttpTrigger here:

curl --location --request POST 'http://localhost:7258/api/Output' \
--header 'Content-Type: text/plain' \
--data-raw 'something went wrong'

If you have a breakpoint on the function Trigger, you'll notice that it also gets triggered, as it listens to the queue alert-queue, which was just populated with the output binding.

The examples are available on PlayGoKids repository.

More posts will come to exemplify other Triggers and Bindings available in Azure Functions. Stay tuned.


  1. References: Overview