Windows Azure Web Sites – A First Look

This is a historic time for Microsoft and Windows Azure.  As I mentioned in my previous post, Microsoft recently held a big party in San Francisco last week at their Meet Windows Azure event.  If you didn’t catch it then, you can watch the recordings at www.MeetWindowsAzure.com.  Be sure to watch the digital chalk talks too, as they go a little deeper on many of the major new features announced.  This week is TechEd and Microsoft held a few special sessions on Monday as part of their “Learn Windows Azure” online event which was streamed live on Channel9.  There were a few Windows Azure announcements on Monday, and I’m hopeful for a few more this week.

I’m not going to spend a lot of time right now discussing all the new Windows Azure features.  You can find more details on the Windows Azure team blog, Scott Guthrie’s blog, and the Meet Windows Azure site.  A few of the major new features include:

  • Windows Azure Virtual Machines – Ability to create persistent VMs running Windows Server (and related server workloads such as SharePoint) and Linux.
  • Windows Azure Web Sites – details below.
  • New tools, languages, and SDK – Updated support for Java, .NET, PHP, and now Python, as well as tooling for VS2012 RC and Macs.
  • New Admin Portal – A new HTML based Windows Azure management portal with support for management of the new Preview features.
  • Cloud Services and Caching – “Hosted Services” are now called “Cloud Services” and include an additional option for distributed caching (support for AppFabric Cache and Memcached)

In this post, I want to explore some of the basics of using Windows Azure web sites.  I personally think this is a “game changer” for those looking to move to the Windows Azure platform.  Previously moving a basic web application to Windows Azure was often a little more painful than one might first expect.  Web roles are great – without a doubt they have a purpose.  However, for the basic ASP.NET MVC site with a simple SQL Server database, using a web role seems like overkill and unneeded complexity (at least that’s the complaint I hear a lot).

The basic idea of Windows Azure Web Sites (WAWS) is to allow developers to deploy web solutions very easily and with very little friction – to do so with just a couple of clicks and often in less than 1 minute.

  • Build – using .NET, PHP, or Node.js and connect with SQL Database or MySQL.
  • Deploy – with the tools you want to use, whether that is Team Foundation Service (TFS), Git, WebDeploy, or even good ol’ FTP.
  • Selection – chose and deploy from a gallery of popular open source solutions such as WordPress, Joomla, Umbraco and more.

Nate Totten has a nice write-up on WAWS on things to know about WAWS.

Note: Windows Azure Web Sites is available as a “Preview” release.  You will need to request access on the “Preview Features” page under the “account” tab, after you log into your Windows Azure account.  If you don’t have an account, you can sign up for one for free now.

The Walkthrough

To explore WAWS I thought I would start by creating a very basic ASP.NET MVC internet site. The site will use a traditional SQL membership provider for managing users (an out-of-the-box solution in the latest ASP.NET MVC project templates).  I’ll also configure the app to use log4net, well, because I love log4net and it demonstrates writing to the local file system without the need for invoking Windows Azure diagnostics or blob storage like might be necessary when using a web role.  I’ll then show deploying the solution via Visual Studio’s Web Deploy and updating via FTP.  Let’s get started!

Prerequisites

0. Install the Windows Azure SDK for .NET (Optional)

For the purposes of this specific example, we don’t technically need the SDK, but it just makes me feel better.  Click the big “install” button.

Windows Azure SDK Install

Then pick either Visual Studio 2010 or Visual Studio 2012 RC.  I’m going to pick Visual Studio 2012 RC.

Windows Azure SDK Install - Select IDE

Once the tools are installed, I can start writing some code.

1. Create the Project

With VS2012 RC, I’m going to create a new ASP.NET MVC4 web project.  Let’s call it “CrazyNinjaWeb”.

New Project

I’m going to create an “Internet Application” and let Visual Studio create the default project setup.  Running this right now should just show the basic starter template for an ASP.NET MVC project.  Nothing fancy here.

Basic Site Template

2. Add Log4Net

I like to use log4net for application logging.  I’ve been using it for a long time and I’m comfortable with it.  I can easily add log4net to my project by using the NuGet package manager and the command Install-Package log4net.

Install log4net via nuget

Now I just need to configure log4net.  For this example, I’m going to configure a basic RollingFileAppender.  This is the basic sample configuration.

log4net configuration

Add a little code to my AssemblyInfo.cs file:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

And then add a little code to my HomeController.cs file to write crazy ninja type of log statements.

private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public ActionResult Index() { ViewBag.Message = "Modify this template to kick-start your ASP.NET MVC application."; if (log.IsDebugEnabled) { for (int i = 0; i < 100; i++) { log.Debug("Hello World! " + DateTime.Now.ToString()); } } return View(); }

3. Add SQL Azure

One of the features of the ASP.NET MVC template I chose was that it has the plumbing already in place for a membership store.  By default the template appears to use a local SQL Express file.  As far as I can tell, WAWS doesn’t support SQL Express, so I’m going to set up a SQL Database instance (starting at $4.99/month + bandwidth).

To create the SQL Database, I log into the new preview management site at http://manage.windowsazure.com. I’m going to click on the SQL DATABASES in the left side navigation pane, and then the “+” Add button on the bottom toolbar.  This loads up a dialog for me to enter details about my new SQL Database.

New SQL Database 2 - edit

Then I enter my SQL administrative credentials and select which Windows Azure datacenter to place the SQL Database in.

SQL Database - Server Settings

Once that is entered, click the checkbox to complete.  My new SQL Database is ready in just a few seconds!

New SQL Database - Ready - edit

I need to be sure to allow my IP address to connect to this SQL Database.  I can do that from the Configure screen and entering in a firewall rule name and my IP address.

SQL Database - Firewall - edit

Now I need the connection string.  Easy enough. Click the little right arrow next to the database name to go to the configuration screen.  From this screen I can click on the “Show connection string” link under “quick glance” (right side of page) to show me the connection strings for various languages.

SQL Database - Configuration - edit

With the connection string, I can update my web application’s web.config to use SQL Database instead of the default SQL Express.

web.confg - new sql database connection string - edit

4. Test Locally

With both log4net and SQL Database now configured, I should check to make sure this all works locally before trying to deploy to Windows Azure Web Sites.  Press F5 in Visual Studio to Start with Debugging.  I’ll try to register a new user, as that should test both writing a log file and connecting to SQL Database.

Register User

And we have log files . . .

log4net log files in explorer - edit

And we have a new SQL Azure database and tables . . . excellent!

initial database tables

5. Create a Windows Azure Web Site

With the application working locally, it’s time to move it to a Windows Azure Web Site. Up until now we really haven’t done anything “Windows Azure” specific, outside of creating the SQL Database.

Using the “New” button in the bottom application bar, we can create a new Web Site, and select the option to use an existing database.

New Web Site with Database

Next, select the database that was created earlier, providing the necessary credentials too.create new web site - edit

In just a few seconds, the web site is created!!  Browsing to the site now shows the default landing page for WAWS (hostingstart.html).

HostingStart Page

6. Deploy via Visual Studio

Going to the configuration area of the new site (click on the little white arrow next to the site name), I have all sorts of new options for WAWS.  Here I can see some key metrics about my site’s resource usage, deployment options, mode (Shared vs. Reserved), location, linked resources (the SQL Database that was created earlier), etc.

Now that the WAWS is created, it’s time to deploy our superb web app!  To do this, I’m going to click the link to “Download publish profile” and elect to save this file to my desktop.

Download Settings Link

I can now right-click on my web project, and select Publish.  This brings up the standard Visual Studio web publishing dialog.  Here I will import the .publishsettings file I just downloaded.  This populates the publishing wizard with the necessary details about where I want to publish my app (my Windows Azure Web Site).  I can choose from several deployment options, including Web Deploy, FTP, etc.

To start with, I’ll select Web Deploy.  Notice all the settings are filled in for me.Deployment Options via VStudio

If I select FTP, not all the options are filled in.  I’m not sure why (a “preview” or RC bug perhaps), but if I exit out of this wizard and then back in, the FTP settings appear.

Click “Next”.  I’ve already supplied a connection string in my web.config, so I’m going to uncheck the box that prompts to update the destination web.config.

Deployment via Web Deploy - Settings

Now click the “Next” button and optionally choose to preview the changes about to be made.  Just click the “Publish” button to start the process.

If I go to my web site now, I should see my deployed application.

Deployed Site

7. Update via an FTP client

Requirements change!  I need to quickly update some HTML in one of my views.  I can make the change and update that file via FTP!  I can do that in Visual Studio, but I’d like to do so via FileZilla.  To do so, I need the connection details.  As far as I can tell, the web site configuration page only tells me the FTP address, but not the default credentials (although I can change them from the site).  To get the default credentials, I can open up the .publishsettings file in Notepad++.

Digging into the XML in the .publishsettings file I can see the following:

Entering this information into FileZilla I can connect to my WAWS via FTP.  Awesome!

FTP to WAWS

I’m going to make a quick change to the Index.cshtml file, replacing some of the template code with a simple H1 element.  Using FileZilla to upload that changed file to my web site.

Site - Final

Doesn’t get much easier than that.  Or does it . . .

8. Optional Optimization

Windows Azure MVP Maarten Balliauw has a nice blog post on using NuGet Package Restore to have WAWS automatically pull down any NuGet packages your site is using.  This should help on upload time and let’s Windows Azure do the work.  Check it out – pretty slick feature!

Summary

Windows Azure Web Sites looks to be a powerful new feature of the Windows Azure platform.  Not only can I create basic web sites like I demonstrated here, but I can also create a web site from a gallery of popular tools.  WAWS supports a nice scale out model too – scaling from a single shared instance, to multiple shared instances, and up to multiple reserved instances – all while maintaining the very simple deployment model.  There’s still a lot more to explore here . . . I’ve only touched on some of the basics with WAWS.