An application processes background tasks using Azure Queue Storage. The application retrieves a message and processes it. Because processing can occasionally exceed the initial visibility timeout, the application must extend the visibility timeout of the retrieved message to prevent other workers from processing it concurrently.
Complete the C# code snippet below by filling in the blanks with the correct Azure Storage Queues SDK client library (.NET) method names and property names.
Cevap:using System;
using System.Threading.Tasks;
using Azure.Storage.Queues;
using Azure.Storage.Queues.Models;
public class QueueWorker
{
public async Task ProcessMessageAsync(string connectionString)
{
QueueClient queueClient = new QueueClient(connectionString, "orders");
// Retrieve a single message and hide it for 30 seconds
var response = await queueClient.【ReceiveMessagesAsync】(1, TimeSpan.FromSeconds(30));
if (response.Value.Length > 0)
{
QueueMessage message = response.Value[0];
// Simulating long-running operation...
// Extend the visibility timeout by another 60 seconds without altering the message text
await queueClient.【UpdateMessageAsync】(
message.MessageId,
message.【PopReceipt】,
visibilityTimeout: TimeSpan.FromSeconds(60)
);
// Complete processing...
}
}
}
using System.Threading.Tasks;
using Azure.Storage.Queues;
using Azure.Storage.Queues.Models;
public class QueueWorker
{
public async Task ProcessMessageAsync(string connectionString)
{
QueueClient queueClient = new QueueClient(connectionString, "orders");
// Retrieve a single message and hide it for 30 seconds
var response = await queueClient.【ReceiveMessagesAsync】(1, TimeSpan.FromSeconds(30));
if (response.Value.Length > 0)
{
QueueMessage message = response.Value[0];
// Simulating long-running operation...
// Extend the visibility timeout by another 60 seconds without altering the message text
await queueClient.【UpdateMessageAsync】(
message.MessageId,
message.【PopReceipt】,
visibilityTimeout: TimeSpan.FromSeconds(60)
);
// Complete processing...
}
}
}
Cevap
The code requires the ReceiveMessagesAsync method to retrieve messages from the queue with an initial visibility timeout, the UpdateMessageAsync method to modify the message state, and the PopReceipt property to provide the cryptographic lease receipt required to authorize the update operation.
To retrieve and lease messages, ReceiveMessagesAsync must be used. To extend the visibility lease, UpdateMessageAsync must be invoked. The operation requires both the MessageId and the PopReceipt of the message to uniquely identify and authorize the update.
Adım Adım Çözüm
Anahtar Kavram
Extending message lease visibility timeout using the Azure Storage Queues SDK for .NET.