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. C#细节忽略的问题:int 与 int?

    int 与 int? 天天都在看,却不知道这2有什么区别呢? 首先说明下这个?的由来吧:C#值类型使不可谓null的,但是sql server的 int 确是可以为null的. 废话不多说直接上代码 ...

  2. Custome Buble Data Point

    <navigation:Page xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/to ...

  3. Vijos p1165 火烧赤壁 离散化+单调栈

    题目链接:https://vijos.org/p/1165 题意:输入n(n <= 20,000)段线段的端点,问所有线段的长度总和为多少? input: -1 1 5 11 2 9 outpu ...

  4. hadoop2.2.0+hive-0.10.0完全分布式安装方法

    hadoop+hive-0.10.0完全分布式安装方法 1.jdk版本:jdk-7u60-linux-x64.tar.gz http://www.oracle.com/technetwork/cn/j ...

  5. sharepoint 2013 sp1

    http://www.microsoft.com/en-us/download/details.aspx?id=42544 http://soussi-imed.over-blog.com/artic ...

  6. 敏捷开发的特点(转自MBAlib)

    敏捷开发的特点 敏捷方法主要有两个特点,这也是其区别于其他方法,尤其是重型方法的最主要特征: (1)敏捷开发方法是“适应性”(Adaptive)而非“预设性” (Predictive). 这里说的预设 ...

  7. [转载]MongoDB开发学习(2)索引的基本操作

    索引能够极大的提高查询的效率.在数据库中简历索引必不可少. 在MongoDB中可以很轻松的创建索引. 默认索引_id_ 开启MongoDB服务器,创建数据库cnblogs,创建集合Users .(关于 ...

  8. PHP mysql_real_escape_string() 函数

    定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符. 下列字符受影响: \x00 \n \r \ ' " \x1a 如果成功, ...

  9. nginx 域名rewrite跳转

    转自:http://blog.csdn.net/xingfujie/article/details/7337832 需求:nginx规则,所有对OA.bccom.info的访问,redirect到uc ...

  10. [string]Codeforces158C Cd and pwd commands

    题目链接 题意很清楚 和linux的语句是一样的 pwd输出路径 cd进入 ..回上一层目录 此题完全是string的应用 String的用法 vector<string> s; int ...