- 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 client
CloudQueueClient queueClient = StorageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference(OrdersQueue); // Create the queue if it doesn't already exist
queue.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 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查看结果:

Windows Azure 系列-- Azure Queue的操作的更多相关文章

  1. Windows Azure 系列-- Azure Redis Cache的配置和使用

    假设还没有配置Azure Power shell 能够參照这里进行配置:http://blog.csdn.net/lan_liang/article/details/46850221 打开Azure ...

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

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

  3. 初码-Azure系列-存储队列的使用与一个Azure小工具(蓝天助手)

    初码Azure系列文章目录 将消息队列技术模型简化,并打造成更适合互联网+与敏捷开发的云服务模式,好像已经是行业趋势,阿里云也在推荐使用消息服务(HTTP协议为主)而来替代消息队列(TCP协议.MQT ...

  4. 初码-Azure系列-记一次MySQL数据库向Azure的迁移

    初码Azure系列文章目录 还在继续给客户迁移不同的系统到Azure,这一次是一个系统的MySQL数据库要迁移,将迁移过程记录一下 原系统环境 数据库版本:MySQL Community Editio ...

  5. 初码-Azure系列-文章目录

    系统迁移 初码-Azure系列-记一次MySQL数据库向Azure的迁移 初码-Azure系列-迁移PHP应用至Azure的一些实践记录和思考 初码-Azure系列-记一次从阿里云到Azure的迁移和 ...

  6. 初码-Azure系列-如何在控制面板中选择中文版操作系统

    之前在文章<初码-Azure系列-记一次从阿里云到Azure的迁移和部署>中说到,默认的Windows Server 2016操作系统是英文版,后来摸索出中文版的方法,如下:

  7. 微软云消息队列 Azure service bus queue

    前言 第一次使用消息队列,遇到了一些问题:同一个消息有多次出列.是一个消息只入列一次,还是多次?还是因为出列问题,出列了多次? Microsoft Azure service bus queue Az ...

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

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

  9. Windows 上安装 Azure PowerShell及Azure PowerShell部署虚拟机

    一.Azure PowerShell部署   1.使用 PowerShellGet 在 Windows 上安装 Azure PowerShell 从 Azure PowerShell 版本 6.0 开 ...

随机推荐

  1. WebApi Owin SelfHost OAuth2 - client_credentials

    参考:http://neverc.cnblogs.com/p/4970996.html

  2. 数据库-mysql中文显示问题

    一:在mysql 下面查看带中文的记录显示乱码 mysql> select * from role; +----+------+ | id | name | +----+------+ | 1 ...

  3. python基础-实现进度条功能,for和yield实现

    实现进度条功能 方法一:简单FOR实现打印进度条功能 for i in range(10): print("#",end="",flush=True) time ...

  4. java 多线程总结篇3之——生命周期和线程同步

    一.生命周期 线程的生命周期全在一张图中,理解此图是基本: 线程状态图 一.新建和就绪状态 当程序使用new关键字创建了一个线程之后,该线程就处于新建状态,此时它和其他的Java对象一样,仅仅由Jav ...

  5. 淘宝开放平台TOP SDK调用对接淘宝或天猫

    如果在淘宝/天猫上开了网店,用户自己也有一套自己的管理平台,这时可能会考虑和淘宝进行数据对接.这就需要考虑调用阿里提供的开发接口来推送和接收数据. 对接的方式有2种,一种是通过http接口,另外一种是 ...

  6. Fedora下Eclipse/MyEclipse崩溃的解决方案

    Fedora19下使用myeclipse2013时,打开不到一分钟就异常关闭. 默认在home目录下生成一个log文件,里面显示的错误信息,说是libsoup.so文件导致出错.第一个想法是删除这个文 ...

  7. C# 在RichTextBox根据内容自动调整高度

    private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e)   {   richTextB ...

  8. HTML5元素2

    用于分组的元素 元素 说明 类型 HTML5与其他的变化 blockquote 表示引自他处的大段内容 流 无变化 dd 用在dl元素之中,表示定义 无 无变化 div 一个没有任何既定语义的通用元素 ...

  9. 面试题49:把字符串转换成整数(atoi)

    需要考虑的问题都已在程序中注释 bool isValid; int StrToInt(const char* str) { isValid = false; //不合法情形1:空指针 if (str ...

  10. Django实战(21):使用内置的Amin管理用户

    到目前为止,我们开发的所有功能都是匿名访问的,这显然不够安全.通常我们会要求注册的用户通过用户名和密码登录,只有登录后的用户才可以管理产品.套用专业的说法就是:第一步是认证,验证用户是否是他所宣称的那 ...