只需三步:使用C# 操作 Azure 队列

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


[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 
    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.   
    9.   
    10.     using Microsoft.WindowsAzure;  
    11.     using Microsoft.WindowsAzure.Storage;  
    12.     using Microsoft.WindowsAzure.Storage.Queue;  
    13.   
    14.   
    15.     public class WinAzureStorageAsync  
    16.     {  
    17.         private readonly CloudQueue queue;  
    18.         private readonly int timeoutSecond;  
    19.         private CloudQueueClient queueClient;  
    20.         public CloudQueueClient QueueClient  
    21.         {  
    22.             get  
    23.             {  
    24.                 if (this.queueClient != null)  
    25.                     return this.queueClient;  
    26.   
    27.   
    28.                 var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));  
    29.                 this.queueClient = storageAccount.CreateCloudQueueClient();  
    30.                 return this.queueClient;  
    31.             }  
    32.         }  
    33.   
    34.   
    35.         ////since each time fetch message is not a block operation  
    36.         ////so need to set a timeout & keep fetching , default is 3 seconds  
    37.         private const int SleepInterval = 100;  
    38.   
    39.   
    40.         public WinAzureStorageAsync(string queueName, int timeoutSecond = 3)  
    41.         {  
    42.             queueName = queueName.ToLower();  
    43.             this.queue = this.QueueClient.GetQueueReference(queueName);  
    44.             if (!this.QueueClient.GetQueueReference(queueName).Exists())  
    45.             {  
    46.                 this.queue.CreateIfNotExists();  
    47.             }  
    48.   
    49.   
    50.             this.timeoutSecond = timeoutSecond;  
    51.         }  
    52.   
    53.   
    54.         public async Task<CloudQueueMessage> GetMessage()  
    55.         {  
    56.             CloudQueueMessage message = null;  
    57.   
    58.   
    59.             var passed = 0;  
    60.               
    61.             while (message == null && passed < this.timeoutSecond * 10 * SleepInterval)  
    62.             {  
    63.                 message = await this.queue.GetMessageAsync();  
    64.                 Thread.Sleep(SleepInterval);  
    65.                 passed += SleepInterval;  
    66.             }  
    67.             if (message == null)  
    68.             {  
    69.                 throw new TimeoutException("Get Message From Azure Queue Operation has been timeout");  
    70.             }  
    71.             await this.queue.DeleteMessageAsync(message);  
    72.   
    73.   
    74.             return message;  
    75.         }  
    76.   
    77.   
    78.         public async Task<string> GetString()  
    79.         {  
    80.             var msg = await this.GetMessage();  
    81.             return msg.AsString;  
    82.         }  
    83.   
    84.   
    85.         public async Task<byte[]> GetBytes()  
    86.         {  
    87.             var msg = await this.GetMessage();  
    88.             return msg.AsBytes;  
    89.         }  
    90.   
    91.   
    92.         public T Get<T>() where T : new()  
    93.         {  
    94.             var bytes = this.GetBytes();  
    95.             return this.BytesToT<T>(bytes.Result);  
    96.         }  
    97.   
    98.   
    99.         public async Task Add(string message)  
    100.         {  
    101.             await this.queue.AddMessageAsync(new CloudQueueMessage(message));  
    102.         }  
    103.   
    104.   
    105.         public async Task Add(byte[] bytes)  
    106.         {  
    107.             await this.queue.AddMessageAsync(new CloudQueueMessage(bytes));  
    108.         }  
    109.   
    110.   
    111.         public void Add<T>(T obj) where T : new()  
    112.         {  
    113.             var bytes = this.TToBytes(obj);  
    114.             this.Add(bytes);  
    115.         }  
    116.   
    117.   
    118.         /// <summary>  
    119.         /// Note : this operation make takes around 40 seconds to complete, reference here:  
    120.         /// http://msdn.microsoft.com/library/azure/dd179387.aspx  
    121.         /// </summary>  
    122.         /// <returns></returns>  
    123.         public async Task DeleteIfExists()  
    124.         {  
    125.             await this.queue.DeleteIfExistsAsync();  
    126.         }  
    127.   
    128.   
    129.         public async Task<bool> IsExist(string queueName)  
    130.         {  
    131.             queueName = queueName.ToLower();  
    132.             return await this.QueueClient.GetQueueReference(queueName).ExistsAsync();  
    133.         }  
    134.   
    135.   
    136.         public void ClearMessage()  
    137.         {  
    138.             this.queue.Clear();  
    139.         }  
    140.   
    141.   
    142.         private T BytesToT<T>(byte[] bytes)  
    143.         {  
    144.             using (var ms = new MemoryStream())  
    145.             {  
    146.                 ms.Write(bytes, 0, bytes.Length);  
    147.                 var bf = new BinaryFormatter();  
    148.                 ms.Position = 0;  
    149.                 var x = bf.Deserialize(ms);  
    150.                 return (T)x;  
    151.             }  
    152.         }  
    153.   
    154.   
    155.         private byte[] TToBytes<T>(T obj)  
    156.         {  
    157.             var bf = new BinaryFormatter();  
    158.             using (var ms = new MemoryStream())  
    159.             {  
    160.                 bf.Serialize(ms, obj);  
    161.                 return ms.ToArray();  
    162.             }  
    163.         }  
    164.     }  
    165. }  
原文地址:https://www.cnblogs.com/sennly/p/4181690.html