You are developing a batch telemetry processing service in C# that consumes messages from an Azure Queue Storage queue named telemetry-ingest. The service must retrieve up to 32 messages at a time and prevent other instances from processing these messages for 5 minutes while they are being processed. Once a message is successfully processed, it must be permanently removed from the queue. Complete the C# code snippet by filling in the blanks with the correct Azure Storage Queues SDK for .NET method names.
Answer:using System;
using System.Threading.Tasks;
using Azure.Storage.Queues;
using Azure.Storage.Queues.Models;
public async Task ProcessTelemetryBatchAsync(QueueClient queueClient)
{
var response = await queueClient.【ReceiveMessagesAsync】(
maxMessages: 32,
visibilityTimeout: TimeSpan.FromMinutes(5)
);
foreach (var message in response.Value)
{
try
{
ProcessTelemetry(message.Body.ToString());
await queueClient.【DeleteMessageAsync】(message.MessageId, message.PopReceipt);
}
catch (Exception ex)
{
// Log error
}
}
}
using System.Threading.Tasks;
using Azure.Storage.Queues;
using Azure.Storage.Queues.Models;
public async Task ProcessTelemetryBatchAsync(QueueClient queueClient)
{
var response = await queueClient.【ReceiveMessagesAsync】(
maxMessages: 32,
visibilityTimeout: TimeSpan.FromMinutes(5)
);
foreach (var message in response.Value)
{
try
{
ProcessTelemetry(message.Body.ToString());
await queueClient.【DeleteMessageAsync】(message.MessageId, message.PopReceipt);
}
catch (Exception ex)
{
// Log error
}
}
}
Answer
The first blank must be filled with ReceiveMessagesAsync to retrieve multiple messages with a visibility timeout, and the second blank must be filled with DeleteMessageAsync to delete the processed message from the queue using its ID and pop receipt.
ReceiveMessagesAsync is the correct method in the Azure.Storage.Queues SDK to retrieve one or more messages and hide them from other consumers by setting a visibility timeout. DeleteMessageAsync is the correct method to remove a message from the queue after processing, requiring both the message ID and the pop receipt.
Step-by-Step Solution
Key Concept
Retrieving and deleting queue messages using the Azure.Storage.Queues SDK for .NET
Estimated Time:2m 0s