Question

Difficulty: EasyImplement Azure Queue Storage Solutions

An operations engineer needs to implement a message consumer in C# for Azure Queue Storage using the modern Azure.Storage.Queues SDK. The consumer must acquire a message, process it, and then immediately delete it from the queue.

Complete the C# code snippet below by filling in the blanks. What are the correct asynchronous method names to retrieve and delete the message?

Answer:csharp
QueueClient queueClient = new QueueClient(connectionString, "work-items");

// Retrieve the message
QueueMessage[] messages = await queueClient.【ReceiveMessagesAsync】(maxMessages: 1);

if (messages.Length > 0)
{
QueueMessage message = messages[0];

// Process the message...

// Permanently remove the message from the queue
await queueClient.【DeleteMessageAsync】(message.MessageId, message.PopReceipt);
}

Answer

The message must be retrieved using the ReceiveMessagesAsync method, and then permanently deleted from the queue using the DeleteMessageAsync method.
The ReceiveMessagesAsync method retrieves one or more messages and makes them invisible to other processors. It provides the PopReceipt which, along with the MessageId, must be passed to DeleteMessageAsync to successfully remove the message from the queue.

Step-by-Step Solution

1
Identify the method required to pull a message from the queue and mark it as invisible to other consumers.
ReceiveMessagesAsync is the standard asynchronous method to retrieve messages.
This method hides the message for a default visibility timeout and returns a PopReceipt required for later deletion.
2
Identify the method required to permanently delete the message once processing is complete.
DeleteMessageAsync removes the message using its ID and PopReceipt.
Explicitly deleting the message ensures it does not return to the queue when the visibility timeout expires.

Key Concept

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