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
- public static CloudQueue GetAzureQueue(string queueName)
- {
- var storageAccount = Microsoft.WindowsAzure.Storage
- .CloudStorageAccount.Parse(
- RoleEnvironment.GetConfigurationSettingValue
- ("StorageConnectionString"));
- var queueClient = storageAccount.CreateCloudQueueClient();
- CloudQueue queue = queueClient.GetQueueReference(queueName);
- return queue;
- }
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
- [HttpPost]
- public ActionResult Create(CustomerFormModel form)
- {
- if (ModelState.IsValid)
- {
- //maps to domain object from view model
- var command = Mapper.Map<CustomerFormModel,
- NewCustomerCommand>(form);
- var serializer = new JavaScriptSerializer();
- var modelAsString = serializer.Serialize(command);
- var queueClient = AzureStorageHelper.GetAzureQueue(QueueName);
- queueClient.CreateIfNotExists();
- //Add messages to the Azure Queue
- queueClient.AddMessage(new CloudQueueMessage(modelAsString));
- return RedirectToAction("Index");
- }
- return View(form);
- }
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.
- CloudTable customerTable = AzureStorageHelper.GetAzureTable(TableName);
- customerTable.CreateIfNotExists();
- CloudQueue customerQueue = AzureStorageHelper.GetAzureQueue(QueueName);
- customerQueue.CreateIfNotExists();
- var receivedMessage=customerQueue.GetMessage();
- if (receivedMessage != null)
- {
- // Process the message
- var serializer = new JavaScriptSerializer();
- var command = serializer.Deserialize<NewCustomerCommand>(receivedMessage.AsString);
- // Create a new Azure Table entity.
- Customer customer=new Customer{
- Id=Guid.NewGuid().ToString(),
- PartitionKey = command.LastName,
- RowKey = command.FirstName,
- FirstName=command.FirstName,
- LastName=command.LastName,
- Address=command.Address,
- Email=command.Email,
- Phone=command.Phone
- };
- // Create the TableOperation that inserts the customer entity.
- TableOperation insertOperation = TableOperation.Insert(customer);
- // Execute the insert operation.
- customerTable.Execute(insertOperation);
- customerQueue.DeleteMessage(receivedMessage);
- }