I gave an Autoscaling Azure talk at the Brisbane Azure User Group (BAUG) on the 18th April 2012. This series of posts will walk through the demo I put together for the talk using the Autoscaling Application Block (WASABi).
What do we need to get started?
You’ll need Visual Studio 2010 and the Windows Azure SDK for .NET. You’ll also need a Windows Azure account – you can sign up for a 90-day free trial or use your MSDN Subscription.
What are we building and deploying?
We will be building and deploying an Azure application that will be the target of the autoscaling. I decided to build a simple ASP.NET MVC 3 web application that also allowed me to manipulate some simple metric that WASABi could monitor for its autoscaling rules. WASABi can monitor a number of metrics but for the demo I decided on the queue length of a queue on the Windows Azure Queue Storage Service as this was the simplest to get up and running.
Create Storage Account
Once you have a Windows Azure account log in to the Windows Azure preview portal and click on the NEW button. We need a storage account to hold the queue we’ll be using in the demo. Select Storage > Quick Create and fill in the details for your storage account. I have selected baugautoscalingapp as my url and Southeast Asia as my region since I live in Australia. The subscription I am using is the subscription linked to my MSDN. I have also disabled geo-replication for the demo storage account. Click on Create Storage Account.

Wait for the storage creation to complete successfully. You should be rewarded with an Online status.

The easiest way to interact with your storage account is to use Azure Storage Explorer 5. You will need your access key to interact with your storage account so click on Manage Keys in the portal and copy the Primary Access Key to the clipboard.

Click on Add Account in the Azure Storage Explorer and populate the details. I have pasted my access key copied from the portal into the storage account key field. Click Add Storage Account.

Confirm that you can connect to your storage account. You can see that we have not created any queues in our storage account yet.

Create Web Application
Now on to the actual web application. Click on New Project in Visual Studio and select Visual C# > Cloud > Windows Azure Cloud Service. I have a folder C:\Projects\WasabiDemo where I am creating the solution with the name, WebApplication. Click OK to create the project.
Select Windows Azure Tools – June 2012 on the next dialog. Select the ASP.NET MVC3 Web Role and click on the pencil to rename the role to WasabiDemoWebRole. Click OK.
Select the empty ASP.NET MVC 3 project template as we want this project as lean as possible. Click OK.
Open the WasabiDemoWebRole in the WebApplication Azure project from the Solution Explorer. Change the VM size to Extra small and Save. This is useful in maximising the hours allocated to your subscription. You can get 6 Extra small instances for the cost of a Small instance.

Right click on the Models folder in the WasabiDemoWebRole project and select Add > Class. Create a class named QueueManager.
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
namespace WasabiDemoWebRole.Models
{
public class QueueManager
{
private string StorageConnectionString
{
get { return "DefaultEndpointsProtocol=https;AccountName=[REPLACE WITH YOUR STORAGE ACCOUNT NAME];AccountKey=[REPLACE WITH YOUR STORAGE ACCOUNT KEY]"; }
}
private string QueueName
{
get { return "workerqueue"; }
}
public int MessageCount
{
get { return RetrieveMessageCount(); }
}
private CloudQueue GetQueue()
{
var storageAccount = CloudStorageAccount.Parse(StorageConnectionString);
var queueClient = storageAccount.CreateCloudQueueClient();
var queue = queueClient.GetQueueReference(QueueName);
queue.CreateIfNotExist();
return queue;
}
public void AddMessage()
{
var queue = GetQueue();
var message = new CloudQueueMessage("Brisbane Azure User Group - Wasabi Demo Test Message");
queue.AddMessage(message);
}
public void RemoveMessage()
{
var queue = GetQueue();
var retrievedMessage = queue.GetMessage();
queue.DeleteMessage(retrievedMessage);
}
public int RetrieveMessageCount()
{
var queue = GetQueue();
return queue.RetrieveApproximateMessageCount();
}
}
}
You will need to replace the [REPLACE WITH YOUR STORAGE ACCOUNT NAME] and [REPLACE WITH YOUR STORAGE ACCOUNT KEY] tokens in the StorageConnectionString property with your storage account name and the primary access key value from your storage account.
Right click on the Controllers folder in the WasabiDemoWebRole project and select Add > Controller. Create a controller named HomeController and use the Empty controller template.
using System.Web.Mvc;
using WasabiDemoWebRole.Models;
namespace WasabiDemoWebRole.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Queue Manager";
var queueManager = new QueueManager();
ViewBag.MessageCount = queueManager.MessageCount;
return View();
}
[HttpPost]
public ActionResult Manage()
{
ViewBag.Message = "Queue Manager";
var queueAction = Request["QueueAction"];
var queueManager = new QueueManager();
if (queueAction.Equals("Add"))
{
queueManager.AddMessage();
}
else if (queueAction.Equals("Remove"))
{
if (queueManager.MessageCount > 0) { queueManager.RemoveMessage(); }
}
ViewBag.MessageCount = queueManager.MessageCount;
return View("Index");
}
}
}
Right click on View() in the Index() method in HomeController and select Add View … Click the Add button in the dialog.
@{
ViewBag.Title = "Brisbane Azure User Group - Wasabi Demo";
}
<h2>@ViewBag.Message</h2>
<p>Current Count of Messages in Queue Waiting to be Processed: @ViewBag.MessageCount</p>
<p>
<form action="/Home/Manage" method="POST">
<button type="submit" name="QueueAction" value="Add">Add</button>
<button type="submit" name="QueueAction" value="Remove">Remove</button>
</form>
</p>
Publish to Azure
Right click on the WebApplication Azure project in the Solution Explorer and click on Publish. Click on the Sign in to download credentials link in the Publish Windows Azure Application dialog. A publishsettings file containing publish credentials for your Azure account will be downloaded. 
Click on the Import button in the Publish Windows Azure Application dialog and import the publishsettings file you have just downloaded. Select your subscription from the drop down once it has been populated.
If you have not already provisioned a cloud service through the portal you will be prompted to create one. This is required to host your web role. I have selected baugautoscalingapp as the name of my cloud service and have selected Southeast Asia as my location.
The cloud service will be created.
This can be confirmed in the portal. The baugautoscalingapp cloud service has a service status of Created.

Change the deployment label in the Advanced Settings tab to something meaningful. Click Next.
Confirm the settings in the Summary and then click Publish.
You will see the details of the deployment in the Windows Azure Activity Log in Visual Studio.

The Deploying status will be visible on the cloud service via the portal.

The deployment is shown as complete in the Windows Azure Activity Log in Visual Studio

The cloud service has a service status of Created and a Production status of Running.

The web application is running in Azure. If we open the cloud service url in a browser and go to the Home/Manage route we can view our simple application. Clicking on the Add button will add messages to the queue and the Remove button will remove them. Below I have added 5 messages to the queue.
If we open up Azure Storage Explorer and click on the queues link we can see that the workerqueue is now available and contains 5 messages. The message contains the text from our QueueManager class.

We now have a running web application in Azure that can be scaled and control over the queue that will be used for metrics by the autoscaler.
Certificates
This is a section with some additional details around what that Publish Windows Azure Application dialog did behind the scenes. All interactions with Azure are managed via Management Certificates. This ensures that only authorised requests can be made to deploy, configure or scale resources within Azure.
If you look at your Certificate Store via the Certificates snap-in in MMC you will notice a certificate under Personal > Certificates that matches the name of the publishsettings file. The dialog automagically created a management certificate for you and added it to your store.

The Management Certificates cannot be viewed in the new preview portal so you have to take a trip down memory lane and open up the old portal – https://windows.azure.com/. Click on the Management Certificates section and you’ll see the related Management Certificates associated with your subscription. Each time a publishsettings file is generated a management certificate is added under your subscription. No additional certificates however are added to your local certificate store.
