只需三步:使用C# 操作 Azure 队列
Step 1 :
安装windows Azure package
Step 2 :
配置文件增加:
- <appSettings>
- <add key="StorageConnectionString" value="your connection string" />
- </appSettings>
Step 3 :
using this Azure class
- namespace Axe.AzureStorage
- {
- using System;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.WindowsAzure;
- using Microsoft.WindowsAzure.Storage;
- using Microsoft.WindowsAzure.Storage.Queue;
- public class WinAzureStorageAsync
- {
- private readonly CloudQueue queue;
- private readonly int timeoutSecond;
- private CloudQueueClient queueClient;
- public CloudQueueClient QueueClient
- {
- get
- {
- if (this.queueClient != null)
- return this.queueClient;
- var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
- this.queueClient = storageAccount.CreateCloudQueueClient();
- return this.queueClient;
- }
- }
- ////since each time fetch message is not a block operation
- ////so need to set a timeout & keep fetching , default is 3 seconds
- private const int SleepInterval = 100;
- public WinAzureStorageAsync(string queueName, int timeoutSecond = 3)
- {
- queueName = queueName.ToLower();
- this.queue = this.QueueClient.GetQueueReference(queueName);
- if (!this.QueueClient.GetQueueReference(queueName).Exists())
- {
- this.queue.CreateIfNotExists();
- }
- this.timeoutSecond = timeoutSecond;
- }
- public async Task<CloudQueueMessage> GetMessage()
- {
- CloudQueueMessage message = null;
- var passed = 0;
- while (message == null && passed < this.timeoutSecond * 10 * SleepInterval)
- {
- message = await this.queue.GetMessageAsync();
- Thread.Sleep(SleepInterval);
- passed += SleepInterval;
- }
- if (message == null)
- {
- throw new TimeoutException("Get Message From Azure Queue Operation has been timeout");
- }
- await this.queue.DeleteMessageAsync(message);
- return message;
- }
- public async Task<string> GetString()
- {
- var msg = await this.GetMessage();
- return msg.AsString;
- }
- public async Task<byte[]> GetBytes()
- {
- var msg = await this.GetMessage();
- return msg.AsBytes;
- }
- public T Get<T>() where T : new()
- {
- var bytes = this.GetBytes();
- return this.BytesToT<T>(bytes.Result);
- }
- public async Task Add(string message)
- {
- await this.queue.AddMessageAsync(new CloudQueueMessage(message));
- }
- public async Task Add(byte[] bytes)
- {
- await this.queue.AddMessageAsync(new CloudQueueMessage(bytes));
- }
- public void Add<T>(T obj) where T : new()
- {
- var bytes = this.TToBytes(obj);
- this.Add(bytes);
- }
- /// <summary>
- /// Note : this operation make takes around 40 seconds to complete, reference here:
- /// http://msdn.microsoft.com/library/azure/dd179387.aspx
- /// </summary>
- /// <returns></returns>
- public async Task DeleteIfExists()
- {
- await this.queue.DeleteIfExistsAsync();
- }
- public async Task<bool> IsExist(string queueName)
- {
- queueName = queueName.ToLower();
- return await this.QueueClient.GetQueueReference(queueName).ExistsAsync();
- }
- public void ClearMessage()
- {
- this.queue.Clear();
- }
- private T BytesToT<T>(byte[] bytes)
- {
- using (var ms = new MemoryStream())
- {
- ms.Write(bytes, 0, bytes.Length);
- var bf = new BinaryFormatter();
- ms.Position = 0;
- var x = bf.Deserialize(ms);
- return (T)x;
- }
- }
- private byte[] TToBytes<T>(T obj)
- {
- var bf = new BinaryFormatter();
- using (var ms = new MemoryStream())
- {
- bf.Serialize(ms, obj);
- return ms.ToArray();
- }
- }
- }
- }
只需三步:使用C# 操作 Azure 队列的更多相关文章
- 只需三步--轻松反编译Android Apk文件
安卓程序是通过java语言进行编写的,可以很容易进行反编译.很多apk文件被反编译后再二次打包,就成了自己的产品,很是流氓.下面我们来看看如何进行apk的反编译,以及常用的防反编译手段. 一.反编译A ...
- 只需三步 快速完善网站Sitemap
越来越多的SEOer把优化的重点放在了站内优化上,细心的朋友应该查看一些前辈的robots.txt的时候不难发现,他们的robots中都加 入了一句Sitemap: http://www.dewang ...
- windows 下安装Apache httpd 只需三步
1.下载 Apache 官网地址:http://httpd.apache.org/docs/current/platform/windows.html#down 找到这个, 看到这几个选项: Apac ...
- iOS - 外加字体(只需三步-教你轻松实现)
外加字体 1.首先info.plist中加入属性Fonts provided by application,在item 0 处填写导入的ttf文件名 eg: <key>UIAppFonts ...
- 只需3步,快来用AI预测你爱的球队下一场能赢吗?
摘要:作为球迷,我们有时候希望自己拥有预测未来的能力. 本文分享自华为云社区<用 AI 预测球赛结果只需三步,看看你爱的球队下一场能赢吗?>,作者:HWCloudAI. 还记得今年夏天的欧 ...
- vuex其实超简单,只需3步
前言 之前几个项目中,都多多少少碰到一些组件之间需要通信的地方,而因为种种原因,event bus 的成本反而比vuex还高, 所以技术选型上选用了 vuex, 但是不知道为什么,团队里的一些新人一听 ...
- 只需一步,DLA开启TableStore多元索引查询加速!
一.背景介绍 Data Lake Analytics(简称DLA)在构建第一天就是支持直接关联分析Table Store(简称OTS)里的数据,实现存储计算分离架构,满足用户基于SQL接口分析Tabl ...
- 如何把C++的源代码改写成C代码?而C改C++只需一步!
★ 如何把C++的源代码改写成C代码? C++解释器比C语言解释器占用的存储空间要大,想要在某些特定场合兼容C++代码,同时为了节省有限的存储空间,降低成本,也为了提高效率,将用C++语言写的源程序用 ...
- PDF怎么旋转页面,只需几步轻松搞定!
有时候我们下载一个PDF文件里面有页面是旋转的情况,用手机看的时候可以把手机旋转过来看,那么用电脑的时候总不可能也转过来看吧,笔记本是可以的台式的是不行的,这个时候我们就需要把PDF文件中旋转的页面转 ...
随机推荐
- C#细节忽略的问题:int 与 int?
int 与 int? 天天都在看,却不知道这2有什么区别呢? 首先说明下这个?的由来吧:C#值类型使不可谓null的,但是sql server的 int 确是可以为null的. 废话不多说直接上代码 ...
- Custome Buble Data Point
<navigation:Page xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/to ...
- Vijos p1165 火烧赤壁 离散化+单调栈
题目链接:https://vijos.org/p/1165 题意:输入n(n <= 20,000)段线段的端点,问所有线段的长度总和为多少? input: -1 1 5 11 2 9 outpu ...
- 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 ...
- sharepoint 2013 sp1
http://www.microsoft.com/en-us/download/details.aspx?id=42544 http://soussi-imed.over-blog.com/artic ...
- 敏捷开发的特点(转自MBAlib)
敏捷开发的特点 敏捷方法主要有两个特点,这也是其区别于其他方法,尤其是重型方法的最主要特征: (1)敏捷开发方法是“适应性”(Adaptive)而非“预设性” (Predictive). 这里说的预设 ...
- [转载]MongoDB开发学习(2)索引的基本操作
索引能够极大的提高查询的效率.在数据库中简历索引必不可少. 在MongoDB中可以很轻松的创建索引. 默认索引_id_ 开启MongoDB服务器,创建数据库cnblogs,创建集合Users .(关于 ...
- PHP mysql_real_escape_string() 函数
定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符. 下列字符受影响: \x00 \n \r \ ' " \x1a 如果成功, ...
- nginx 域名rewrite跳转
转自:http://blog.csdn.net/xingfujie/article/details/7337832 需求:nginx规则,所有对OA.bccom.info的访问,redirect到uc ...
- [string]Codeforces158C Cd and pwd commands
题目链接 题意很清楚 和linux的语句是一样的 pwd输出路径 cd进入 ..回上一层目录 此题完全是string的应用 String的用法 vector<string> s; int ...