- Storage Account。 和之前介绍的Azure Table和AzureBlob一样。你须要一个StorageAccount,仅仅须要创建1次AzureStorageAccount就好了,它们3个是共享的。

创建好之后。就能够使用下面属性来訪问Azure的Storage了:

private static CloudStorageAccount StorageAccount{get{var creds = new StorageCredentials(AccountName, Key);var account = new CloudStorageAccount(creds, useHttps: true);return account;}}

- 创建Azure Q

public static void CreateIfNotExist(){// Create the queue clientCloudQueueClient queueClient = StorageAccount.CreateCloudQueueClient();CloudQueue queue = queueClient.GetQueueReference(OrdersQueue);// Create the queue if it doesn't already existqueue.CreateIfNotExists();}

须要注意的就是Q的名字。所有小写。

- 入队

   /// <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);}

代码逻辑非常easy,就是向Queue中加入消息。只是要注意,这里仅仅是为了演示没有考虑多线程环境以及并发情形,详细场景中为了不堵塞线程,你通过须要使用Asyn版本号的方法,即:

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 clientCloudQueueClient queueClient = StorageAccount.CreateCloudQueueClient();// Retrieve a reference to a queueCloudQueue queue = queueClient.GetQueueReference(OrdersQueue);// Peek at the next messageIEnumerable<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 queuevar queue = queueClient.GetQueueReference(OrdersQueue);var retrievedMessage = queue.GetMessage();//Process the message in less than 30 seconds, and then delete the messagequeue.DeleteMessage(retrievedMessage);return retrievedMessage.AsString;}

完整的測试代码:

    [TestMethod]public void AzureQ_Test(){// - create QAzureQueueManager.CreateIfNotExist();// - Add 5 messages to Qfor (int i = 0; i < 5; i++){AzureQueueManager.AddMsg(string.Format("hello_{0}",i));    }// peek all messages , Assert the order is correctvar 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 msgvar msg = AzureQueueManager.DequeueMsg();Assert.IsTrue(msg == "hello_0");// - peek all messages , assert the first msg has been dequeuedmsgs = 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查看结果:

Windows Azure 系列-- Azure Queue的操作相关推荐

  1. Windows Azure系列-- Azure Table的CRUD操作

    1. 首先还是按照Azure Storage 的Pkg: 2. 可以下载AzureStorage Explorer 来管理Azure Storage的状态 https://azurestorageex ...

  2. 初码-Azure系列-迁移PHP应用至Azure的一些实践记录和思考

    最近客户在逐步迁移应用从阿里云到Azure,这次又轮到一个PHP+MySQL应用了,顺便也记一下流水账. 需求:迁移部署在阿里云上的ECS服务器(系列2,IO优化+2核4G+50G的SSD云盘+10M ...

  3. 如何在ASP.NET Core中使用Azure Service Bus Queue

    原文:USING AZURE SERVICE BUS QUEUES WITH ASP.NET CORE SERVICES 作者:damienbod[1] 译文:如何在ASP.NET Core中使用Az ...

  4. Azure系列1.1.2 —— 用于 IntelliJ 的 Azure 工具包的登录说明

    (文中大部分内容(95%)Azure官网上有,我只是把我自己实际操作中遇到的问题在这里阐述一下.) 先决条件 若要完成文章中的步骤,需要安装用于 IntelliJ 的 Azure 工具包,该工具包需要 ...

  5. wsl 上使用docker_首先通过在WSL和Docker中进行测试,将ASP.NET Core从Windows上的Azure应用服务迁移到Linux

    wsl 上使用docker I updated one of my websites from ASP.NET Core 2.2 to the latest LTS (Long Term Suppor ...

  6. 史上最新最全的来自成都的Azure系列文章,助你上云!老少皆宜,童叟无欺!

     这是成都MVP 张俊森的系列Azure文章.各位尽情收藏吧. 文章 链接 1.Azure虚拟机部署 http://blog.51cto.com/rdsrv/2071039 2.Azure资源组迁 ...

  7. Azure Functions + Azure Batch实现MP3音频转码方案

    客户需求 客户的环境是一个网络音乐播放系统,根据网络情况提供给手机用户收听各种码率的MP3歌曲,在客户没购买歌曲的情况下提供一个三十秒内的试听版本.这样一个系统非常明确地一个需求就是会定期需要将一批从 ...

  8. Azure机器学习——Azure机器学习介绍

    Azure机器学习介绍 一.什么是Azure机器学习? 二.Azure 机器学习的几个重要概念 工作区(Workspace) 数据存储(Datastore) 计算目标(Compute Targets) ...

  9. Windows Mobile系列手机操作系统

    与其它掌上型电子设备的操作系统不同的是,Windows Mobile系列操作系统是在微软计算机的Windows操作系统上变化而来的,因此,它们的操作界面非常相似,熟悉计算机Windows系列操作系统的 ...

  10. Windows Mobile 系列文章索引---不断整理中(2009-07-08)

    Windows Mobile 高级编程系列 ØWindows Mobile 进阶系列.第零回.序 ØWindows Mobile 进阶系列.第一回.真的了解.NET CF吗? ØWindows Mob ...

最新文章

  1. MySQL隐藏换行符的处理
  2. fluent计算进出口的流量差
  3. 用 libpcap抓取http报文
  4. 解决android studio引用远程仓库下载慢(JCenter下载慢)
  5. Android Studio Gradle构建脚本
  6. hadoop之DataBlockScanner
  7. spring Boot 2.1.5 (1)---安装环境
  8. 个人计算机多核cpu好处,CPU是多核好还是高主频好?
  9. scala Option类入门解析
  10. 嵌入式系统调试仿真工具
  11. Spark Mllib里如何删除每一条数据中所有的双引号“”(图文详解)
  12. Git 提交代码步骤
  13. 总管家云CRM 解除业务员的后顾之忧
  14. Android R 设置壁纸流程和 Launcher 闪烁问题
  15. 手把手教你DIY一款属于自己的万能红外遥控器!
  16. 刷卡分期的套路有多深?信用卡、花呗、白条没有善人
  17. step7设置pcpg_怎么安装STEP7编程软件及PG/PC接口设置
  18. Oracle的客户端工具
  19. 金山系绕不开三件事:求伯君的爱情、雷军的人品,还有他的铁皮屋
  20. vc中操作INI文件函数

热门文章

  1. AlbertTransformerEncoder
  2. 6.5bert的家族成员-百度的ERNIE,ERNIE2.0,清华的ERNIE,RoBERTa,BERT-WWM,UniLM,MASS,TinyBERT,ELECTRA,SpanBERT
  3. Tensorflow:模型训练tensorflow.train
  4. Machine Learning lectures- 机器学习课程
  5. 吸顶灯怎么固定天花板_吸顶灯怎么安装?家庭圆形吸顶灯底座安装步骤(图文解说)...
  6. 蓝桥杯2016年第七届C/C++省赛B组第四题-快速排序
  7. 《Android群英传》— Android 书籍
  8. 7-1 宿舍谁最高? (20 分)
  9. hadoop/hbase/hive单机扩增slave
  10. 【翻译】Species distribution modeling 2 数据准备