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);
}
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
Key Concept
Retrieving and deleting messages using the Azure.Storage.Queues SDK
Estimated Time:1m 0s