使用 .NET 建立和管理 Azure 佇列記憶體和訊息
在此單元中,我們將說明如何藉由顯示來自 .NET 專案的代碼段,在 Azure 佇列記憶體中建立佇列和管理訊息。
程式代碼範例依賴下列 NuGet 套件:
- 適用於 .NET 的 Azure.Core 連結庫:此套件提供新式 .NET Azure SDK 用戶端連結庫的共用基本類型、抽象概念和協助程式。
- 適用於 .NET的 Azure.Storage.Common 用戶端連結庫:此套件提供由其他 Azure 記憶體用戶端連結庫共用的基礎結構。
- 適用於 .NET的 Azure.Storage.Queues 用戶端連結庫:此套件可讓您使用 Azure 佇列記憶體來儲存用戶端所存取的訊息。
- 適用於 .NET的 System.Configuration.ConfigurationManager 連結庫:此套件提供用戶端應用程式組態檔的存取權。
建立佇列服務用戶端
QueueClient
類別可讓您擷取儲存在佇列記憶體中的佇列。 以下是建立服務用戶端的其中一種方式:
QueueClient queueClient = new QueueClient(connectionString, queueName);
建立佇列
此範例示範如果佇列不存在,如何建立佇列:
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
// Create the queue
queueClient.CreateIfNotExists();
將訊息插入佇列
若要將訊息插入現有的佇列,請呼叫 SendMessage
方法。 訊息可以是字串(採用UTF-8格式)或位元組陣列。 下列程式代碼會建立佇列(如果不存在),並插入訊息:
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
// Create the queue if it doesn't already exist
queueClient.CreateIfNotExists();
if (queueClient.Exists())
{
// Send a message to the queue
queueClient.SendMessage(message);
}
查看下一則訊息
您可以呼叫 PeekMessages
方法,查看佇列中的訊息,而不從佇列中移除訊息。 如果您未傳遞 maxMessages
參數的值,預設值是查看一則訊息。
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
if (queueClient.Exists())
{
// Peek at the next message
PeekedMessage[] peekedMessage = queueClient.PeekMessages();
}
變更佇列訊息的內容
您可以直接在佇列中變更訊息的內容。 如果訊息代表工作工作,您可以使用此功能來更新工作工作的狀態。 下列程式代碼會以新的內容更新佇列訊息,並設定可見性逾時以再延長 60 秒。 這會儲存與訊息相關聯的工作狀態,並再讓用戶端再一分鐘繼續處理訊息。
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
if (queueClient.Exists())
{
// Get the message from the queue
QueueMessage[] message = queueClient.ReceiveMessages();
// Update the message contents
queueClient.UpdateMessage(message[0].MessageId,
message[0].PopReceipt,
"Updated contents",
TimeSpan.FromSeconds(60.0) // Make it invisible for another 60 seconds
);
}
取消佇列下一個訊息
分兩個步驟從佇列中取出訊息。 當您呼叫 ReceiveMessages
時,您會在佇列中取得下一個訊息。 從 ReceiveMessages
傳回的訊息對於任何從這個佇列讀取訊息的其他程式碼來說都是不可見的。 根據預設,此訊息會保持不可見 30 秒。 若要完成從佇列中移除訊息,您也必須呼叫 DeleteMessage
。 拿掉訊息的這個雙步驟程式可確保如果您的程式代碼因硬體或軟體失敗而無法處理訊息,則程式代碼的另一個實例可以取得相同的訊息,然後再試一次。 您的程式代碼會在處理訊息之後立即呼叫 DeleteMessage
。
// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
if (queueClient.Exists())
{
// Get the next message
QueueMessage[] retrievedMessage = queueClient.ReceiveMessages();
// Process (i.e. print) the message in less than 30 seconds
Console.WriteLine($"Dequeued message: '{retrievedMessage[0].Body}'");
// Delete the message
queueClient.DeleteMessage(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);
}
取得佇列長度
您可以取得佇列中訊息數目的估計值。
GetProperties
方法會傳回佇列屬性,包括訊息計數。
ApproximateMessagesCount
屬性包含佇列中的大約訊息數目。 此數目不低於佇列中實際訊息數目,但可能較高。
/// Instantiate a QueueClient which will be used to manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
if (queueClient.Exists())
{
QueueProperties properties = queueClient.GetProperties();
// Retrieve the cached approximate message count.
int cachedMessagesCount = properties.ApproximateMessagesCount;
// Display number of messages.
Console.WriteLine($"Number of messages in queue: {cachedMessagesCount}");
}
刪除佇列
若要刪除佇列及其中包含的所有訊息,請在佇列物件上呼叫 Delete
方法。
/// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
// Instantiate a QueueClient which will be used to manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, queueName);
if (queueClient.Exists())
{
// Delete the queue
queueClient.Delete();
}