Sending and Receiving Messages in Windows Azure Storage Queue with JavaScript Serializer

In this blog post, I will demonstrate sample code for sending and receiving messages in Windows Azure Storage Queue with JavaScript Serializer for serializing and de-serializing messages. We are adding a custom type to Windows Azure Queue where we are using JavaScriptSerializer Class provided by System.Web.Script.Serialization namespace, for the serialization.

Creating a Windows Azure Storage Queue

The below method creates a Windows Azure Storage Queue

  1. public static CloudQueue GetAzureQueue(string queueName)
  2. {
  3.     var storageAccount = Microsoft.WindowsAzure.Storage
  4.         .CloudStorageAccount.Parse(
  5.         RoleEnvironment.GetConfigurationSettingValue
  6.         ("StorageConnectionString"));
  7.     var queueClient = storageAccount.CreateCloudQueueClient();
  8.     CloudQueue queue = queueClient.GetQueueReference(queueName);
  9.     return queue;
  10. }

Adding Messages to Windows Azure Storage Queue

The below code block in the ASP.NET MVC Action Method, creates a Windows Azure Storage Queue and adding messages to the Queue. For adding a custom type to the Queue, we are serialize the model as a string message using JavaScript Serializer

  1. [HttpPost]
  2. public ActionResult Create(CustomerFormModel form)
  3. {
  4.     if (ModelState.IsValid)
  5.     {
  6.         //maps to domain object from view model
  7.         var command = Mapper.Map<CustomerFormModel,
  8.             NewCustomerCommand>(form);
  9.         var serializer = new JavaScriptSerializer();
  10.         var modelAsString = serializer.Serialize(command);
  11.         var queueClient = AzureStorageHelper.GetAzureQueue(QueueName);
  12.         queueClient.CreateIfNotExists();
  13.         //Add messages to the Azure Queue
  14.         queueClient.AddMessage(new CloudQueueMessage(modelAsString));               
  15.         return RedirectToAction("Index");
  16.     }
  17. return View(form);
  18. }

Receiving Messages from Windows Azure Storage Queue 

The code block below provides the implementation in a Worker Role for retrieving messages from Windows Azure Storage Queue and de-serializing the message string as a custom type. Finally, we creates a Windows Azure Table entity from the values of de-serialized type and inserting to Windows Azure Table.

  1. CloudTable customerTable = AzureStorageHelper.GetAzureTable(TableName);
  2.     customerTable.CreateIfNotExists();
  3.     CloudQueue customerQueue = AzureStorageHelper.GetAzureQueue(QueueName);
  4.     customerQueue.CreateIfNotExists();
  5.     var receivedMessage=customerQueue.GetMessage();
  6.     if (receivedMessage != null)
  7.     {
  8.         // Process the message
  9.         var serializer = new JavaScriptSerializer();
  10.         var command = serializer.Deserialize<NewCustomerCommand>(receivedMessage.AsString);
  11.                         
  12.         // Create a new Azure Table entity.
  13.         Customer customer=new Customer{
  14.             Id=Guid.NewGuid().ToString(),
  15.             PartitionKey = command.LastName,
  16.             RowKey = command.FirstName,
  17.             FirstName=command.FirstName,
  18.             LastName=command.LastName,
  19.             Address=command.Address,
  20.             Email=command.Email,
  21.             Phone=command.Phone                                
  22.         };
  23.  
  24.         // Create the TableOperation that inserts the customer entity.
  25.         TableOperation insertOperation = TableOperation.Insert(customer);
  26.  
  27.         // Execute the insert operation.
  28.         customerTable.Execute(insertOperation);
  29.         customerQueue.DeleteMessage(receivedMessage);
  30.     }
Published Tuesday, April 30, 2013 1:06 PM by shiju
Filed under: , ,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)