只需三步:使用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文件中旋转的页面转 ...
随机推荐
- Oracle wrap 测试的一些细节问题
今天在做 wrap 的测试实验的时候,出现一个很奇怪的现象,就是加密不成功.具体表现为:1.加密后的文件大小为0kb. 2.加密后的文件仍然可视. 具体测试步骤如下: D:\Just4work\som ...
- 如何读懂Oracle文档中的语法图(转)
本文转载自:http://kyle.xlau.org/posts/syntax-diagrams.html Oracle文档中用到了两种表达语法的方法,语法图和BNF. BNF, Backus-Nau ...
- uCGUI窗口初始化过程
一.相关结构体和变量 重要的uCGUI系统全局变量 NextDrawWin 下一个需要重绘的窗口句柄 WM__NumWindows 系统当前的总共 ...
- STM32之触摸屏
一.触摸屏控制器简介 1.TSC2046概貌 2.TSC2046引脚描述 3.TSC2046应用电路 4.常见接线方法 YU=Y- YD=Y+ XL=X- XR=X+ 二.TSC2046编程注意事项 ...
- String类中常用的操作
一.获取: 1.获取字符串的长度(注意是方法,不是跟数组的属性一样的) int length(); 1 public static void getLength(){ 2 String s = &qu ...
- SVN服务器使用(一)
源代码版本控制软件很多,像VSS,SVN还有其他的软件,各有优缺点.Subversion是优秀的版本控制工具,下面主要介绍这个软件的使用. Subversion下载地址: http://subvers ...
- 练习--LINUX进程间通信之信号SIGNAL
同样的,信号也不要太迷信可靠信号及不及靠信号,实时或非实时信号. 但必须要了解这些信号之间的差异,函数升级及参数,才能熟练运用. ~~~~~~~~~~~~~~~~ 信号本质 信号是在软件层次上对中断机 ...
- 如何让Activiti-Explorer使用sql server数据库
从官网下载的Activiti-explorer的war文件内部默认是使用h2内存数据库的,如果想改用其他的数据库来做持久化,比如sql server,需要做如下配置. 1)修改db.propertie ...
- ANDROID_MARS学习笔记_S02_010_Animation_动画效果
一.流程 1.把要实现动画的一系列图片复制到res/drawable文件夹 2.在此文件新建一个xml文件用来组织图片 3.在mainactivity中用imageView.setBackground ...
- Type-C设计上的防护
Type C设计上各家芯片公司都提供了很多方案,但在防护方面很多留给了客户自己选择,这方面我可以重点聊聊,说起防护,无非就是过压过流防护. 过压防护,Type C的信号线有很多,都需要做静电防护,US ...