更新时间:2022-08-16 08:25:12
- Storage Account。 和之前介绍的Azure Table和AzureBlob一样。你须要一个StorageAccount,仅仅须要创建1次AzureStorageAccount就好了,它们3个是共享的。
private static CloudStorageAccount StorageAccount { get { var creds = new StorageCredentials(AccountName, Key); var account = new CloudStorageAccount(creds, useHttps: true); return account; } }
public static void CreateIfNotExist() { // Create the queue client CloudQueueClient queueClient = StorageAccount.CreateCloudQueueClient(); CloudQueue queue = queueClient.GetQueueReference(OrdersQueue); // Create the queue if it doesn't already exist queue.CreateIfNotExists(); }
/// <summary>
/// add msg to Q
/// </summary>
/// <param name="msg"></param>
public static void AddMsg(string msg)
{
CloudQueueClient queueClient = StorageAccount.CreateCloudQueueClient();
// Retrieve a reference to a queue.
CloudQueue queue = queueClient.GetQueueReference(OrdersQueue);
// Create a message and add it to the queue.
CloudQueueMessage message = new CloudQueueMessage(msg);
queue.AddMessage(message);
}
queue.AddMessageAsync(message);
/// <summary> /// peek a number of messages from Q /// </summary> /// <param name="count"></param> /// <returns></returns> public static IList<string> Peek(int count) { // Create the queue client CloudQueueClient queueClient = StorageAccount.CreateCloudQueueClient(); // Retrieve a reference to a queue CloudQueue queue = queueClient.GetQueueReference(OrdersQueue); // Peek at the next message IEnumerable<CloudQueueMessage> peekedMessages = queue.PeekMessages(count); return peekedMessages.Select(m => m.AsString).ToList(); }
/// <summary> /// dequeue a msg /// </summary> /// <returns></returns> public static string DequeueMsg() { var queueClient = StorageAccount.CreateCloudQueueClient(); // Retrieve a reference to a queue var queue = queueClient.GetQueueReference(OrdersQueue); var retrievedMessage = queue.GetMessage(); //Process the message in less than 30 seconds, and then delete the message queue.DeleteMessage(retrievedMessage); return retrievedMessage.AsString; }
[TestMethod] public void AzureQ_Test() { // - create Q AzureQueueManager.CreateIfNotExist(); // - Add 5 messages to Q for (int i = 0; i < 5; i++) { AzureQueueManager.AddMsg(string.Format("hello_{0}",i)); } // peek all messages , Assert the order is correct var msgs = AzureQueueManager.Peek(5); Assert.IsTrue(msgs.Count == 5); Assert.IsTrue(msgs[0] == "hello_0"); Assert.IsTrue(msgs[1] == "hello_1"); Assert.IsTrue(msgs[2] == "hello_2"); Assert.IsTrue(msgs[3] == "hello_3"); Assert.IsTrue(msgs[4] == "hello_4"); // - dequeue msg var msg = AzureQueueManager.DequeueMsg(); Assert.IsTrue(msg == "hello_0"); // - peek all messages , assert the first msg has been dequeued msgs = AzureQueueManager.Peek(5); Assert.IsTrue(msgs.Count == 4); Assert.IsTrue(msgs[0] == "hello_1"); Assert.IsTrue(msgs[1] == "hello_2"); Assert.IsTrue(msgs[2] == "hello_3"); Assert.IsTrue(msgs[3] == "hello_4"); }
最后,使用Azure Storage Explorer查看结果:
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5390615.html,如需转载请自行联系原作者