Question

Difficulty: HardImplement Azure Queue Storage Solutions

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
}
}
}

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

1
Identify the method required to retrieve multiple messages asynchronously with custom parameters in Azure.Storage.Queues.
ReceiveMessagesAsync is identified as the method that accepts maxMessages and visibilityTimeout parameters and returns a Response containing an array of messages.
The scenario requires retrieving up to 32 messages at once and locking them (hiding them) for 5 minutes, which is done using ReceiveMessagesAsync.
2
Identify the method required to remove a message from the queue after processing is complete.
DeleteMessageAsync is identified as the method that takes a MessageId and PopReceipt to permanently delete the message.
Messages in Azure Storage Queues must be explicitly deleted after processing to prevent them from becoming visible again after the visibility timeout expires.

Key Concept

Retrieving and deleting queue messages using the Azure.Storage.Queues SDK for .NET
Estimated Time:2m 0s
Rate this question