Last active
February 25, 2016 15:26
-
-
Save BMeyerKC/a4c215a63f43eba4639c to your computer and use it in GitHub Desktop.
This is might little helper for Azure Queue Storage. Allows me to make and retrieve a queue based on class name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Easy203k.Repo; | |
using System.Configuration; | |
using Microsoft.WindowsAzure.Storage.Queue; | |
namespace Easy203k.Shed | |
{ | |
public class MessageShed : BaseShed | |
{ | |
CloudQueueClient queueClient; | |
public MessageShed(IEasyRepository conn, CloudQueueClient queueClient) : base(conn) | |
{ | |
this.queueClient = queueClient; | |
} | |
private CloudQueue GetQueue(string queueName) | |
{ | |
queueName = string.Format("{0}{1}", queueName, ConfigurationManager.AppSettings["environment"]).ToLowerInvariant(); | |
var queue = queueClient.GetQueueReference(queueName); | |
queue.CreateIfNotExists(); | |
return queue; | |
} | |
private T GetMessageObject<T>() | |
{ | |
var queue = GetQueue(typeof(T).Name); | |
var message = queue.GetMessage(); | |
if (message == null) return default(T); | |
var rtn = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(message.AsString); | |
queue.DeleteMessage(message); | |
return rtn; | |
} | |
public void MessageQueueStore(object obj) | |
{ | |
GetQueue(obj.GetType().Name).AddMessage(new CloudQueueMessage(Newtonsoft.Json.JsonConvert.SerializeObject(obj))); | |
} | |
public T MessageQueueGet<T>() | |
{ | |
return GetMessageObject<T>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment