Getting Started with Windows Azure Platform Development
I am researching the Microsoft Windows Azure Platform. What follows are the notes I am taking as I learn this rich "cloud-based" environment. If you are just getting started with Windows Azure like I am, you may find these notes useful.
To keep the start up costs of programming Windows Azure applications to a minimum, I am focusing on primarily free resources.
Quick List of Links
- Windows Azure Platform Free 90 Day Trial
- Visual Web Developer 2010 Express
- Windows Azure Tools for Visual Studio
- Windows Azure Platform Management Portal
- Channel 9’s Cloud Cover
- Windows Azure Platform Training Kit
- Overview of Building an Application that Runs in a Hosted Service
- Tracing to Azure Compute Emulator SDK V1.3
- Microsoft’s Website Spark program
- Tips and Tools for a Better Azure Deployment Lifecycle
Get a Windows Azure Account
First, sign up for the Windows Azure Platform Free 90 Day Trial or otherwise create a Windows Azure Platform account.
Get Visual Studio
There are a variety of ways to program applications for Windows Azure. I happen to be fond of Visual Studio, having programmed in that environment for many years. The quickest and least expensive way to acquire Visual Studio is download and install Visual Web Developer 2010 Express.
After installing Visual Web Developer 2010 Express, create a new project. Within the New Project dialog, navigate to Installed Templates, Visual C#, and click Cloud. Visual Studio will then guide you through the process of installing the Windows Azure Tools for Visual Studio.
After installing the Windows Azure Tools for Visual Studio, run through the Code Quick Start tutorial, Create and deploy an ASP.NET application in Windows Azure. The tutorial will walk you through creating a very simple ASP.NET application and its deployment to Windows Azure. Read the tutorial very carefully as there is a lot of very important information to digest all at once.
Get Some Costs!
There’s a note in the Code Quick Start tutorial about configuring your VM to use the Extra-Small size. As an experiment, I left the size at the default setting of Small and deployed anyway. After I deployed, I let the sample application sit in staging on the Windows Azure Platform and it went unused for two days. It turns out that the free trial includes 25 compute hours for Small VMs. By the time I shut the sample application down, it accumulated 52 compute hours. I was billed 27 hours overage at $0.12 per hour. Keep this in mind as you work with this platform. If you’re concerned about costs, pay attention to the details.
Tip: To change the VM size, use Solution Explorer to navigate to the “Role” project that contains the service configuration and definition files ending in “cscfg” and “csdef”. Open the “Roles” folder, launch the WebRole and change the size using the user interface.
Get Some Training
Download and install the Windows Azure Platform Training Kit. The hands-on labs use a dependency checker to make sure you have all the correct software installed and running. If you’re using Visual Web Developer 2010 Express, the dependency checker will indicate that you do not have Visual Studio installed. Just ignore this and continue through the check out. The hands-on lab runs perfectly fine using the Express version of Visual Studio.
The first hands on lab (Exercise 1: Building Your First Windows Azure Application ) walks through the process of setting up a guest book web application. The first exercise will step you through building a class library that will serve as a data access layer (DAL). You will also create a simple web page within the context of a Web Role that uses the class library and uses Windows Azure resources to create storage objects. The second exercise sets up a Worker Role to process uploaded bit maps into thumbnails. The third exercise walks through the deployment of the finished application to Windows Azure.
Microsoft Windows Azure Basics
Windows Azure is a platform as a service (paas) environment that consists of three components: Fabric, Compute and Storage. Fabric refers to the network of interconnected servers and their connections. The Compute and Storage components are also a part of the Fabric. The Compute component provides a computation environment via Worker Role services. A special kind of Worker Role, called a Web Role is available that simplifies creating ASP.NET front-ended applications. The Storage component provides three types of scalable data storage: Tables, Blobs and Queues.
- Structured storage in the form of non-relational tables.
- You can use a Table to persist and index your application’s objects.
- Tables do not have a schema other than requiring identifiers. Typically, you’ll use a Table to efficiently store and retrieve objects using LINQ and data binding with an ObjectDataSource.
- Important Properties
- PartitionKey - unique identifier for the partition. By definition all table entities are organized by partition to support load balancing across storage nodes.
- RowKey - unique identifier for an entity within a given partition. Every insert, update, and delete operation requires the RowKey.
- Timestamp - maintained on the server side to record the time an entity was last modified. This property is required by only used by Windows Azure to provide optimistic concurrency.
- Unstructured storage in the form of block and paged blobs, which are optimized for streaming.
- You can upload a blob and commit it using their block IDs. Blocks can be no more than 65 MB in size.
- Larger blobs up to 1 TB in size can be stored in paged blobs.
- Storage of messages that may be read by any client who has access.
- A queue contains an unlimited number of messages. Each message must be less than 8 KB in size.
- Typically, you’ll use a queue to share messages between application roles - See Below.
- Other Windows Azure Storage Offerings
In the first exercise, much of the code that deals with Table Storage is encapsulated within a standard C# class library that serves as a DAL. default.aspx begins by creating objects from this library and also calls Windows Azure methods directly via Microsoft.WindowsAzure.StorageClient and other libraries. In the class library, the DataContext class provides a bridge between the data-binding logic and the collection of the POCOs (Plain old CLR objects). The DataSource in the library provides the actual LINQ query to retrieve a list of POCOs.
During initialization, Windows Azure applications typically retrieve an instance of a CloudStorageAccount storage account. The initialization of CloudStorageAccount requires a connection string. Usually this comes from the application’s configuration settings (”DataConnectionString”). To simplify the retrieval of the storage account’s configuration settings, initialize Windows Azure by calling SetConfigurationSettingPublisher in the Application_Start method of Global.asax.
To initialize the DAL to use Windows Azure storage, in the DataSource:
- Call CloudTableClient.CreateTablesFromModel
- Pass the type for the DAL’s DataContext.
- The base type for the DAL’s DataContext is Microsoft.WindowsAzure.StorageClient.TableServiceContext.
- Do this only once for the session. Use a static constructor for the DataSource class.
- Microsoft no longer recommends using CloudTableClient.CreateTablesFromModel. Rather you should use CloudTableClient.CreateTable.
- For the DataSource get a new instance of the DAL’s DataContext.
- Set the DAL’s DataContext’s RetryPolicy to an appropriate value, typically 3 times with 1 second between each retry.
- Do this during the regular constructor of the DAL’s DataContext.
When it is time to save the user’s entries from the user interface, create a new POCO object and initialize it with the values from the user interface. Create a new DataSource and add the POCO object to the data source.
To work with Blob storage in Windows Azure, first get a storage account (CloudStorageAccount) from configuration settings “DataConnectionString” just like when you requested a CloudTableClient in the above. Get a blobStorage from the storage account by calling CreateCloudBlobClient. Get a CloudBlobContainer from the blobStorage by calling GetContainerReference.
Create the container if it doesn’t exist already by calling CreateIfNotExist. Create a unique name for the blob and pass that along when requesting a blob from blobStorage using GetBlockBlobReference. Set the ContentType for the blob (usually something like image/pjpeg). Then upload the blob by calling the blob’s UploadFromStream method. The stream can come from a asp:FileUpload web control.
When it is time to save the user’s entries from the user interface, include in the POCO, the blob’s address contained in the blob’s Uri property. In this way, the blob is associated with the entry object in the Table storage.
Message Queue storage objects are created in a very similar manner as a blob and table storage by calling the storage account’s CreateCloudQueueClient method. Create a CloudQueue object by calling the client’s GetQueueReference passing a name that serves as an address for the queue.
When it is time to save the user’s entries from the user interface, create a new CloudQueueMessage and pass along a string that includes the message. This message will be retrieved by a separate Worker Role and serves as a trigger to convert the blob into a thumbnail.
In Windows Azure, a common pattern is to create a Worker Role that waits for a message to arrive in the Queue, processes the message and then performs some action on the message. For instance, you could use a worker role to convert an uploaded picture file into a thumbnail. The Web Role would store the uploaded picture file in a blob and then place a message on the queue. The Worker Role would see the message, convert the file into a thumbnail, store the thumbnail into the blob and then update the associated POCO's thumbnail's Uri property.
Just as in the Web Role, the Worker Role needs a storage account CloudStorageAccount from configuration settings "DataConnectionString" Only this time the setting is initialized by calling SetConfigurationSettingPublisher in the OnStart method of WorkerRole class.
During the OnStart method, Get a blobStorage and queue object in exactly the same manner as the web role by using the storageAccount's methods, CreateCloudBlobClient and CreateCloudQueueClient respectively. Set the permissions for the blob using the CloudBlobContainer.
In the hands-on lab, the worker role project carries a reference to the DAL. The worker role then uses the same DAL as the web role and is able to manipulate the same Table storage.
A worker role provides a Run method that is called to initiate processing. More information on the lifecycle methods of worker roles is in the MSDN Library article Overview of Building an Application that Runs in a Hosted Service. Typically, a worker role will check for messages in the queue by calling the GetMessage method. If there are no messages, we simply pause the worker role for one second, after which the Run method will exit and Windows Azure will then restart the worker role.
In order to convert an uploaded picture file into a thumbnail, you can use the .net System.Drawing library for simplicity. If this were a production application, a more robust technique would be called for. One posibility is to review Jeff Prosise's blog post Silverlight's Big Image Problem (and What You Can Do About It).
Debugging Windows Azure Applications
For development purposes, the Windows Azure Compute Emulator comes with the SDK tools and provides a Windows Azure environment simulation. In this simulation, you can run and test an application on your local computer before you actually deploy it to the Azure cloud. The Compute Emulator also provides a GUI to assist you with monitoring trace and other information. This GUI has a handy shortcut in the form of an icon in the Windows system tray.
For a step by step guide for writing to the diagnostic logs that appear in the Compute Emulator’s monitor window, review the MSDN article How to Initialize the Windows Azure Diagnostic Monitor. Basically, you write to the trace logs using the methods provided by System.Diagnostics.Trace. The SDK provides a “listener” library that you hook into your application via web.config.
Note that web applications actually run in a different application pool than the web and work roles. Subsequently, if you wish to write trace messages from a web page code behind such as default.aspx.cs, you’ll need to add a listener to web.config. Although the exercises in the hands-on lab use System.Diagnostics.Trace.TraceInformation to write trace information to the Azure Computer Emulator diagnostic monitor, the correct listener is not referenced in the lab and you will not see the trace information in the logs. More information on this issue is available from Andy Cross's article Tracing to Azure Compute Emulator SDK V1.3. To view trace output from the code in the hands-on lab, add the following listener to the web.config in the System.Diagnostic Trace Listeners section.
|XML |||copy code |||?|
<add type="Microsoft.ServiceHosting.Tools.DevelopmentFabric.Runtime.DevelopmentFabricTraceListener, Microsoft.ServiceHosting.Tools.DevelopmentFabric.Runtime, Version=220.127.116.11, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="DevFabricListener">
For development purposes, the Windows Azure Storage Emulator comes with the SDK tools and provides a Windows Azure environment simulation of Blob, Table and Queue storage. The Storage Emulator also provides a GUI to assist you with viewing blobs, and POCOs. This GUI is available from Visual Studio’s Server Explorer.
This is one area where using Visual Web Developer 2010 Express poses a challenge. Visual Web Developer 2010 Express does not provide a Server Explorer. Instead, a Data Explorer is provided and although Windows Azure Storage appears, I was unable to view anything. Visual Studio 2010 Professional does not have this issue.
It is possible to get a free version of Visual Studio 2010 Professional by joining Microsoft’s Website Spark program.
Deploying Windows Azure Applications
There are many things to consider when deploying applications to Windows Azure. At minimum, you’ll need to create a Storage Account and a Hosted Service. For the best performance and lowest costs, you'll also want to create an Affinity Group so that the Storage Account and the Hosted Service are deployed to the same location. Using the Windows Azure Management Portal found at https://windows.azure.com/, perform the following steps to create your application’s initial structure in Windows Azure:
- Create a Storage Account
- Create an Affinity Group
- Copy the Primary access key from the Properties Pane
- Create a Hosted Service
- Choose the Affinity Group you created in the step above
- Select the Do not Deploy option
The reason to defer deployment is so you can go back to the application source and make any last minute changes such as providing the AccountName and AccountKey in the connection strings listed in the ServiceConfiguration.cscfg using the Primary access keys from the step above.
One way to move your application from Visual Studio into Windows Azure is to create a package and then upload it. After right-clicking the cloud project in Solution Explorer, select Publish. Then choose Create Service Package Only in the Deploy Windows Azure project dialog. Visual Studio will first build your project and then create the Service Package which you’ll then upload using the Windows Azure Management Portal:
- From the Portal’s Home page, select Hosted Services, Storage Accounts & CDN.
- Select Hosted Services, then locate your service in the list.
- Right click the service and select New Staging Deployment.
- Use the dialog to name your deployment - use a version identifier of some kind.
- Use the dialog to point to the package and configuration files stored on your local computer.
The Portal will then upload your package and configuration files, prep the fabric and deploy your application. After the deployment completes, Windows Azure generates a temporary unique URL and makes it available in the properties panel. Click this to test your application in staging. Once your application is deployed in Windows Azure staging, you can do the following:
- Deploy to Production
- Works the same way as deploying to Staging.
- Alternatively, swap the VIP between Staging and Production (see below).
- Configure the hosted service
- Upgrade the hosted service
- Upload an updated package
- Upload an updated CSCFG file
- Instructions to upgrade the hosted service
- Promote Staging to Production - Virtual IP Swap (VIP-Swap)
- The Virtual IP (VIP) address of staging is swapped with the VIP address of production
- Instructions to swap VIPs