Solution Review: Applying a Queue Trigger
Explore how to apply a queue trigger in Azure Functions using .NET. Learn to bind HTTP trigger outputs to storage queues, enabling message passing and asynchronous processing in serverless applications.
We'll cover the following...
We'll cover the following...
Overview
The complete solution is presented in the code widget below:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace StorageQueueTriggerDemo
{
public class Functions
{
[FunctionName("JobTrigger")]
[return: Queue("job-queue")]
public static async Task<string> TriggerJob(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
string jobName = req.Query["jobName"];
var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
jobName = jobName ?? data?.jobName;
if (string.IsNullOrWhiteSpace(jobName))
return null;
return jobName;
}
[FunctionName("JobExecutor")]
public void ExecuteJob([QueueTrigger("job-queue", Connection = "AzureWebJobsStorage")] string item, ILogger log)
{
Console.WriteLine($"The following job is scheduled: {item}");
}
}
}
The solution to putting a message on a storage queue from an HTTP trigger
Solving the challenge
First, we bind the return value of the ...