JavaShuo
栏目
标签
只需三步:使用C# 操做 Azure 队列
时间 2019-11-19
标签
只需
三步
使用
c#
azure
队列
栏目
C#
繁體版
原文
原文链接
Step 1 :
安装windows Azure package
Step 2 :
配置文件增长:
html
[html]
view plain
copy
print
?
<
appSettings
>
<
add
key=
"StorageConnectionString"
value=
"your connection string"
/>
</
appSettings
>
Step 3 :
using this Azure class
windows
[csharp]
view plain
copy
print
?
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();
}
}
}
}
相关文章
1.
C# Azure 存储-队列
2.
FreeRTOS 队列操做——异步转同步
3.
C# 异步并发操做,只保留最后一次操做
4.
队列操做
5.
C# 使用Task执行异步操做
6.
只需三步!慢日志去无踪
7.
连续同源异步操做队列
8.
用Power-BI做BI报表,只需三四个步骤
9.
异步操做(三)
10.
c#操做RabbitMQ使用json格式写入消息队列
更多相关文章...
•
C# 队列(Queue)
-
C#教程
•
ionic 列表操作
-
ionic 教程
•
C# 中 foreach 遍历的用法
•
Java Agent入门实战(三)-JVM Attach原理与使用
相关标签/搜索
异步操做
只需
步操
azure
做操
操做
附操做步骤
三只
需用
只用
C#
C#教程
Spring教程
Hibernate教程
C#
应用
0
分享到微博
分享到微信
分享到QQ
每日一句
每一个你不满意的现在,都有一个你没有努力的曾经。
最新文章
1.
springboot在一个项目中启动多个核心启动类
2.
Spring Boot日志-3 ------>SLF4J与别的框架整合
3.
SpringMVC-Maven(一)
4.
idea全局设置
5.
将word选择题转换成Excel
6.
myeclipse工程中library 和 web-inf下lib的区别
7.
Java入门——第一个Hello Word
8.
在chrome安装vue devtools(以及安装过程中出现的错误)
9.
Jacob线上部署及多项目部署问题处理
10.
1.初识nginx
本站公众号
欢迎关注本站公众号,获取更多信息
相关文章
1.
C# Azure 存储-队列
2.
FreeRTOS 队列操做——异步转同步
3.
C# 异步并发操做,只保留最后一次操做
4.
队列操做
5.
C# 使用Task执行异步操做
6.
只需三步!慢日志去无踪
7.
连续同源异步操做队列
8.
用Power-BI做BI报表,只需三四个步骤
9.
异步操做(三)
10.
c#操做RabbitMQ使用json格式写入消息队列
>>更多相关文章<<