public static void CommandQueuePoller(CommandHandler service) { //Trace.WriteLine("LogPoller has started -- seeing if there's anything in the queue for me!!"); CloudQueueMessage msg = CommandQueue.GetMessage(); if (msg == null) { //Trace.TraceInformation("COMMAND QUEUE nothing found - going back to sleep"); } while (msg != null) { string myMessage = msg.AsString; Trace.TraceInformation("Got message {0}", myMessage); //probably should do some checking here before deleting... CommandQueue.DeleteMessage(msg); //split the ID from the type -- first item is ID, second is type string[] messageParts = myMessage.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (messageParts.Length != 2) { Trace.TraceError("COMMAND QUEUE message improperly formed. {0}", myMessage); continue; } CloudBlockBlob theBlob = CommandBlobContainer.GetBlockBlobReference(messageParts[0]); Type commandType = Type.GetType(messageParts[1]); CommandBase theCommand = null; using (MemoryStream msBlob = new MemoryStream()) { byte[] logBytes = theBlob.DownloadByteArray(); msBlob.Write(logBytes, 0, logBytes.Length); BinaryFormatter bf = new BinaryFormatter(); msBlob.Position = 0; object theObject = bf.Deserialize(msBlob); theCommand = Convert.ChangeType(theObject, commandType) as CommandBase; } if (theCommand != null) { service.Handle(theCommand); } else { Trace.TraceInformation("COMMAND BLOB Could not deserialize message from queue id {0}", myMessage); } msg = CommandQueue.GetMessage(); } }
private static void InitializeStorageResources(CloudStorageAccount storageAccount) { var blobStorage = storageAccount.CreateCloudBlobClient(); LogBlobContainer = blobStorage.GetContainerReference("logitems"); CommandBlobContainer = blobStorage.GetContainerReference("commands"); var queueStorage = storageAccount.CreateCloudQueueClient(); LogQueue = queueStorage.GetQueueReference("logmessages"); CommandQueue = queueStorage.GetQueueReference("commandmessages"); bool isStorageFinalized = false; while (!isStorageFinalized) { try { LogBlobContainer.CreateIfNotExist(); LogQueue.CreateIfNotExist(); CommandBlobContainer.CreateIfNotExist(); CommandQueue.CreateIfNotExist(); isStorageFinalized = true; } catch (StorageClientException sce) { if (sce.ErrorCode == StorageErrorCode.TransportError) { Trace.TraceError("Storage services initialization failure. " + "Check your storage account configuration settings. " + "If running locally, ensure that the Development Storage service is running. " + "Message: '{0}'", sce.Message); } else { throw sce; } } } }