Created
February 15, 2020 14:40
-
-
Save giuleon/fd9d924c588af353b7f7084733215988 to your computer and use it in GitHub Desktop.
This snippet demonstrates how to delay the visibility of a message in an Azure Storage Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Logging; | |
using Newtonsoft.Json; | |
using Microsoft.Azure.Storage.Queue; | |
using Microsoft.Azure.Storage; | |
using Microsoft.Azure; | |
namespace QueueVisibility | |
{ | |
public static class addToQueueStorageWithdelay | |
{ | |
[FunctionName("addToQueueStorageWithdelay")] | |
public static async Task<IActionResult> Run( | |
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, | |
ILogger log) | |
{ | |
log.LogInformation("C# HTTP trigger function processed a request."); | |
string name = req.Query["name"]; | |
string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); | |
dynamic data = JsonConvert.DeserializeObject(requestBody); | |
name = name ?? data?.name; | |
string conn = Environment.GetEnvironmentVariable("StorageConnectionString"); | |
// Retrieve storage account from connection string. | |
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(conn); | |
// Create the queue client. | |
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); | |
// Retrieve a reference to a queue. | |
CloudQueue queue = queueClient.GetQueueReference("<name of your queue>"); | |
// Create the queue if it doesn't already exist. | |
queue.CreateIfNotExists(); | |
// Create a message and add it to the queue. | |
CloudQueueMessage message = new CloudQueueMessage(requestBody); | |
// The message will be visible in the queue after 3 days | |
queue.AddMessage(message, initialVisibilityDelay: TimeSpan.FromDays(3)); | |
return name != null | |
? (ActionResult)new OkObjectResult($"Hello, {name}") | |
: new BadRequestObjectResult("Please pass a name on the query string or in the request body"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment