Step 1 :

安装windows Azure package

Step 2 :

配置文件增加:

  1. <appSettings>
  2. <add key="StorageConnectionString" value="your connection string" />
  3. </appSettings>

Step 3 :

using this Azure class

    1. namespace Axe.AzureStorage
    2. {
    3. using System;
    4. using System.IO;
    5. using System.Runtime.Serialization.Formatters.Binary;
    6. using System.Threading;
    7. using System.Threading.Tasks;
    8. using Microsoft.WindowsAzure;
    9. using Microsoft.WindowsAzure.Storage;
    10. using Microsoft.WindowsAzure.Storage.Queue;
    11. public class WinAzureStorageAsync
    12. {
    13. private readonly CloudQueue queue;
    14. private readonly int timeoutSecond;
    15. private CloudQueueClient queueClient;
    16. public CloudQueueClient QueueClient
    17. {
    18. get
    19. {
    20. if (this.queueClient != null)
    21. return this.queueClient;
    22. var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    23. this.queueClient = storageAccount.CreateCloudQueueClient();
    24. return this.queueClient;
    25. }
    26. }
    27. ////since each time fetch message is not a block operation
    28. ////so need to set a timeout & keep fetching , default is 3 seconds
    29. private const int SleepInterval = 100;
    30. public WinAzureStorageAsync(string queueName, int timeoutSecond = 3)
    31. {
    32. queueName = queueName.ToLower();
    33. this.queue = this.QueueClient.GetQueueReference(queueName);
    34. if (!this.QueueClient.GetQueueReference(queueName).Exists())
    35. {
    36. this.queue.CreateIfNotExists();
    37. }
    38. this.timeoutSecond = timeoutSecond;
    39. }
    40. public async Task<CloudQueueMessage> GetMessage()
    41. {
    42. CloudQueueMessage message = null;
    43. var passed = 0;
    44. while (message == null && passed < this.timeoutSecond * 10 * SleepInterval)
    45. {
    46. message = await this.queue.GetMessageAsync();
    47. Thread.Sleep(SleepInterval);
    48. passed += SleepInterval;
    49. }
    50. if (message == null)
    51. {
    52. throw new TimeoutException("Get Message From Azure Queue Operation has been timeout");
    53. }
    54. await this.queue.DeleteMessageAsync(message);
    55. return message;
    56. }
    57. public async Task<string> GetString()
    58. {
    59. var msg = await this.GetMessage();
    60. return msg.AsString;
    61. }
    62. public async Task<byte[]> GetBytes()
    63. {
    64. var msg = await this.GetMessage();
    65. return msg.AsBytes;
    66. }
    67. public T Get<T>() where T : new()
    68. {
    69. var bytes = this.GetBytes();
    70. return this.BytesToT<T>(bytes.Result);
    71. }
    72. public async Task Add(string message)
    73. {
    74. await this.queue.AddMessageAsync(new CloudQueueMessage(message));
    75. }
    76. public async Task Add(byte[] bytes)
    77. {
    78. await this.queue.AddMessageAsync(new CloudQueueMessage(bytes));
    79. }
    80. public void Add<T>(T obj) where T : new()
    81. {
    82. var bytes = this.TToBytes(obj);
    83. this.Add(bytes);
    84. }
    85. /// <summary>
    86. /// Note : this operation make takes around 40 seconds to complete, reference here:
    87. /// http://msdn.microsoft.com/library/azure/dd179387.aspx
    88. /// </summary>
    89. /// <returns></returns>
    90. public async Task DeleteIfExists()
    91. {
    92. await this.queue.DeleteIfExistsAsync();
    93. }
    94. public async Task<bool> IsExist(string queueName)
    95. {
    96. queueName = queueName.ToLower();
    97. return await this.QueueClient.GetQueueReference(queueName).ExistsAsync();
    98. }
    99. public void ClearMessage()
    100. {
    101. this.queue.Clear();
    102. }
    103. private T BytesToT<T>(byte[] bytes)
    104. {
    105. using (var ms = new MemoryStream())
    106. {
    107. ms.Write(bytes, 0, bytes.Length);
    108. var bf = new BinaryFormatter();
    109. ms.Position = 0;
    110. var x = bf.Deserialize(ms);
    111. return (T)x;
    112. }
    113. }
    114. private byte[] TToBytes<T>(T obj)
    115. {
    116. var bf = new BinaryFormatter();
    117. using (var ms = new MemoryStream())
    118. {
    119. bf.Serialize(ms, obj);
    120. return ms.ToArray();
    121. }
    122. }
    123. }
    124. }

只需三步:使用C# 操作 Azure 队列的更多相关文章

  1. 只需三步--轻松反编译Android Apk文件

    安卓程序是通过java语言进行编写的,可以很容易进行反编译.很多apk文件被反编译后再二次打包,就成了自己的产品,很是流氓.下面我们来看看如何进行apk的反编译,以及常用的防反编译手段. 一.反编译A ...

  2. 只需三步 快速完善网站Sitemap

    越来越多的SEOer把优化的重点放在了站内优化上,细心的朋友应该查看一些前辈的robots.txt的时候不难发现,他们的robots中都加 入了一句Sitemap: http://www.dewang ...

  3. windows 下安装Apache httpd 只需三步

    1.下载 Apache 官网地址:http://httpd.apache.org/docs/current/platform/windows.html#down 找到这个, 看到这几个选项: Apac ...

  4. iOS - 外加字体(只需三步-教你轻松实现)

    外加字体 1.首先info.plist中加入属性Fonts provided by application,在item 0 处填写导入的ttf文件名 eg: <key>UIAppFonts ...

  5. 只需3步,快来用AI预测你爱的球队下一场能赢吗?

    摘要:作为球迷,我们有时候希望自己拥有预测未来的能力. 本文分享自华为云社区<用 AI 预测球赛结果只需三步,看看你爱的球队下一场能赢吗?>,作者:HWCloudAI. 还记得今年夏天的欧 ...

  6. vuex其实超简单,只需3步

    前言 之前几个项目中,都多多少少碰到一些组件之间需要通信的地方,而因为种种原因,event bus 的成本反而比vuex还高, 所以技术选型上选用了 vuex, 但是不知道为什么,团队里的一些新人一听 ...

  7. 只需一步,DLA开启TableStore多元索引查询加速!

    一.背景介绍 Data Lake Analytics(简称DLA)在构建第一天就是支持直接关联分析Table Store(简称OTS)里的数据,实现存储计算分离架构,满足用户基于SQL接口分析Tabl ...

  8. 如何把C++的源代码改写成C代码?而C改C++只需一步!

    ★ 如何把C++的源代码改写成C代码? C++解释器比C语言解释器占用的存储空间要大,想要在某些特定场合兼容C++代码,同时为了节省有限的存储空间,降低成本,也为了提高效率,将用C++语言写的源程序用 ...

  9. PDF怎么旋转页面,只需几步轻松搞定!

    有时候我们下载一个PDF文件里面有页面是旋转的情况,用手机看的时候可以把手机旋转过来看,那么用电脑的时候总不可能也转过来看吧,笔记本是可以的台式的是不行的,这个时候我们就需要把PDF文件中旋转的页面转 ...

随机推荐

  1. Websocket协议数据帧传输和关闭连接

    之前总结了关于Websocket协议的握手连接方式等其他细节,现在对socket连接建立后的数据帧传输和关闭细节总结. 一.数据帧格式 数据传输使用的是一系列数据帧,出于安全考虑和避免网络截获,客户端 ...

  2. JDBC链接

    //1. MySQL(http://www.mysql.com)mm.mysql-2.0.2-bin.jar  Connection con = null;  Class.forName( " ...

  3. mcollective安装过程

    参考 http://kisspuppet.com/2013/11/10/mcollective-middleware/ http://5lexin.com/blog/view/225/mco-ping ...

  4. 【原创】一起学C++ 之enum ---------C++ primer plus(第6版)

    枚举 定义:在默认情况下讲整数值赋给枚举量,第一个枚举量的值为0,第二个枚举量的值为1,依次+1 一.定义一个枚举,枚举类型,枚举量 *与C#相比个人认为C++的enum不好一点是不能通过枚举名点其中 ...

  5. Pair Project: Elevator Scheduler [电梯调度算法的实现和测试][关于电梯调度算法的附加思考]:刘耀先-11061183,罗凡-11061174

    本文为对于电梯调度算法的三个附加题思考 1.改进电梯调度的interface 设计, 让它更好地反映现实, 更能让学生练习算法, 更好地实现信息隐藏和信息共享. <1>进一步提高API定义 ...

  6. Angular js总结

    之前看过一些angular js的相关技术文档,今天在浏览技术论坛的时候发现有问angular js是做什么用的? 于是有了一个想法,对于自己对angular js 的认知做一个总结. 总结: ang ...

  7. hadoop2.4.0 安装配置 (2)

    hdfs-site.xml 配置如下: <?xml version="1.0" encoding="UTF-8"?> <?xml-styles ...

  8. C++ new operator, delete operator, operator new, operator delete, new placement

    http://www.younfor.com/cpp-new-placement-new-operator-new.html http://www.cnblogs.com/luxiaoxun/arch ...

  9. Handlebars 介绍

    最新项目用到了Ember.js前端框架,第一次使用这样的框架,准备国庆节花2天时间,研究一下它的用法. Ember框架的模板引擎用到了handlebars, 先看国外的一篇介绍文章:An Introd ...

  10. 教你6步定制你的Ubuntu桌面

    转自教你6步定制你的Ubuntu桌面 对于那些想要一个易于使用的界面的用户,Ubuntu是一个很好的Linux发行版,并且对于一个Linux新手也可以说是最好的Linux发行版.不过这产生了一些副作用 ...