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. Oracle wrap 测试的一些细节问题

    今天在做 wrap 的测试实验的时候,出现一个很奇怪的现象,就是加密不成功.具体表现为:1.加密后的文件大小为0kb. 2.加密后的文件仍然可视. 具体测试步骤如下: D:\Just4work\som ...

  2. 如何读懂Oracle文档中的语法图(转)

    本文转载自:http://kyle.xlau.org/posts/syntax-diagrams.html Oracle文档中用到了两种表达语法的方法,语法图和BNF. BNF, Backus-Nau ...

  3. uCGUI窗口初始化过程

    一.相关结构体和变量 重要的uCGUI系统全局变量 NextDrawWin                      下一个需要重绘的窗口句柄 WM__NumWindows       系统当前的总共 ...

  4. STM32之触摸屏

    一.触摸屏控制器简介 1.TSC2046概貌 2.TSC2046引脚描述 3.TSC2046应用电路 4.常见接线方法 YU=Y- YD=Y+ XL=X- XR=X+ 二.TSC2046编程注意事项 ...

  5. String类中常用的操作

    一.获取: 1.获取字符串的长度(注意是方法,不是跟数组的属性一样的) int length(); 1 public static void getLength(){ 2 String s = &qu ...

  6. SVN服务器使用(一)

    源代码版本控制软件很多,像VSS,SVN还有其他的软件,各有优缺点.Subversion是优秀的版本控制工具,下面主要介绍这个软件的使用. Subversion下载地址: http://subvers ...

  7. 练习--LINUX进程间通信之信号SIGNAL

    同样的,信号也不要太迷信可靠信号及不及靠信号,实时或非实时信号. 但必须要了解这些信号之间的差异,函数升级及参数,才能熟练运用. ~~~~~~~~~~~~~~~~ 信号本质 信号是在软件层次上对中断机 ...

  8. 如何让Activiti-Explorer使用sql server数据库

    从官网下载的Activiti-explorer的war文件内部默认是使用h2内存数据库的,如果想改用其他的数据库来做持久化,比如sql server,需要做如下配置. 1)修改db.propertie ...

  9. ANDROID_MARS学习笔记_S02_010_Animation_动画效果

    一.流程 1.把要实现动画的一系列图片复制到res/drawable文件夹 2.在此文件新建一个xml文件用来组织图片 3.在mainactivity中用imageView.setBackground ...

  10. Type-C设计上的防护

    Type C设计上各家芯片公司都提供了很多方案,但在防护方面很多留给了客户自己选择,这方面我可以重点聊聊,说起防护,无非就是过压过流防护. 过压防护,Type C的信号线有很多,都需要做静电防护,US ...