Microsoft & .NETCreating a Windows Azure Application: A Step-by-Step Guide

Creating a Windows Azure Application: A Step-by-Step Guide

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Windows Azure, as powerful and intriguing as it is, can seem daunting to many developers seeking to take their first steps into cloud computing. Developers may have the perception that Windows Azure is too big to wrap their arms around. The perception that Windows Azure is too big and too different is a fallacy.

Need Help? If help with Windows Azure is needed, the Microsoft Platform Ready team is available at http://www.microsoftplatformready.com. The site contains many helpful resources such as a jumping off point for tools, insightful blogs, email, chat, and phone help options, and much more. This is also were Microsoft Partner Network members can find information for earning a “Powered by Windows Azure” logo. If promotional help is needed, the Microsoft Platform Ready team can help with that as well via the Windows Azure Marketplace. Powered by Azure In this article, developers new to Windows Azure will learn how to create a basic ASP.NET MVC3 application, which uses SQL Azure as the database. By the end of this article, you will realize that the Windows Azure platform isn’t more complicated than the traditional Microsoft .NET development landscape – just a bit different.

In order to start with a solid foundation, it is important to understand a few of the core Windows Azure concepts. Both Windows Azure and SQL Azure are hosted in Microsoft’s world-class data centers around the world. Windows Azure provides developers with on-demand compute, storage, networking, identity management, caching and content delivery capabilities. SQL Azure provides relational database capabilities as an on-demand service. Windows Azure is an extremely rich platform, and there is a lot you can do with it.

In order to demonstrate building an application on Windows Azure, we’ll create a basic ASP.NET MVC3 site that uses SQL Azure as the database. The ASP.NET MVC3 site will be hosted in a Windows Azure web role (a container for your web application). The application you will build is a web site that can be used to track your personal golf history. The web site will store a record of each golf course played, the date it was played, score, and a few other key golf statistics. By the time you’re done, you’ll have a fully functional web application running in Windows Azure. Let’s get started!

Try Windows Azure Now. Absolutely Free. No Obligation. Try it free.

 

What You Will Need

To begin, you’ll want to make sure you have a few prerequisites installed on your development machine. We’ll add a few more components along the way, but to get going you’ll need at least the following:

  • Windows 7
  • Visual Studio 2010
  • Microsoft Web Platform Installer 3.0 (this will be used for installing the additional tools)
  • Windows Azure SDK for .NET
  • ASP.NET MVC3 runtime & tooling

Once Visual Studio 2010 is installed, the easiest way to get started is to go to Windows Azure .NET Developer Center at https://www.windowsazure.com/en-us/develop/net/. Clicking on the large green “install” button will start the process of installing the necessary tools.

 

.NET Developer Center

 

From there you will be prompted to the launch the Microsoft Web Platform Installer, which will load the necessary Windows Azure developer tools onto your machine. Part of the tooling to be installed will be the Windows Azure development and storage emulator. These emulators allow for a majority of Windows Azure development to be done locally on a development machine, without any need for connecting to a Windows Azure data center.

Web PI Installer

If you need any help getting started, you can always ask for help via the Microsoft Platform Ready site at http://www.microsoftplatformready.com.

With the necessary tools installed, it’s time to start creating the site.

Step 1 – Create an ASP.NET MVC3 Site

To begin, launch Visual Studio 2010 as an Administrator. You will need Administrative permissions once we start testing the application with the Windows Azure development emulator. Create a new ASP.NET MVC3 web application and name it “MyGolfStory”.

New ASP.NET MVC3 Project

You’re going to create a new Internet Application. For the purposes of this demo you will not need to create a unit test project, but a best practice would be to do so.

New ASP.NET MVC3 Project, Unit Tests

Creating a new ASP.NET MVC3 project adds several usefull references for us. One of those is a reference to EntityFramework. You will be using Entity Framework 4.1 as the ORM (Object Relational Mapper) for our data access layer. Entity Framework can be used with SQL Azure just like it would with SQL Server on-premises. To learn more about Entity Framework, please visit http://msdn.microsoft.com/en-us/data/aa937723.

Next, make some minor user interface adjustments for the My Golf Story site. Modify the standard ASP.NET MVC3 layout to have a different background color (green), remove the login control, and add a few tabs that will later be used to manage golf courses and rounds.

The site’s background color is defined in the ContentSite.css style sheet. Change the background color to green.

CSS - Body Background Color

Since the site isn’t going to have any user management features, the default login view and also the associated configuration from the web.config file can be removed. Open the ViewsShared_Layout.cshtml file and remove the ‘logingdisplay’ <div>. In order to get the default layout to look good, you can add an empty <div>.

The _Layout.cshtml file also contains a list that serves as the navigation menu for the site. The site will need two more menu options – “Courses” and “Rounds”. These will map to the Courses and Rounds controllers and views to be created later.

When finished, the code should look like that in the image below.

Layout - Removed Login View and Add Empty Div

Next, open the Web.config file and remove the default ‘ApplicationServices’ connection string and elements related to membership. While they’re technically OK to leave, they’re not being used and removing them prevents any chance of errors related to code that may try to access them and the database not being available.

Web.config - Removed authentication modules

With the basic layout of the site complete, work can now continue with creating the data model.

Step 2 – Create the Model

For this project, a code first approach will be used with the Entity Framework for working with the data model. With this approach, the data model (i.e. entity classes) is created prior to a database or tables being created. Create a simple POCO (Plain Old CLR Objects) that will serve as the entity classes to work with. The Entity Framework will automatically create the necessary SQL Azure database and tables that relate to the defined POCO entity classes. To learn more about Entity Framework or code first with Entity Framework, please visit http://msdn.microsoft.com/en-us/data/aa937723.

While Entity Framework is used for this project, virtually any data access approach used to access SQL Server can also be used for working with SQL Azure. This could be ADO.NET, NHibernate, or any other data access technology you’re fond of. SQL Azure uses TDS (Tabular Data Stream) as its underlying protocol. This is the same protocol that SQL Server uses. It should be noted that SQL Azure is at this time not 100% feature compatible with SQL Server. Please visit http://msdn.microsoft.com/en-us/library/ff394115(v=MSDN.10).aspx for details on incompatible features.

First, create two new models – one to model a golf course, and the other to model a round at a course. These new model classes will be placed in the Models folder of the project.

	public class Course
	{
		public int CourseId { get; set; }
		public string Name { get; set; }
		public string Url { get; set; }
		public string AddressLine1 { get; set; }
		public string AddressLine2 { get; set; }
		public string City { get; set; }
		public string State { get; set; }
		public string ZipCode { get; set; }
	}
	
	public class Round
	{
		public int RoundId { get; set; }
		public int CourseId { get; set; }

		public DateTime RoundDate { get; set; }
		public int FinalScore { get; set; }
		public int ScoreIn { get; set; }
		public int ScoreOut { get; set; }
		public int GreensInRegulation { get; set; }
		public int Puts { get; set; }

		public virtual Course Course { get; set; }
	}
	

Now that the models are in place, we need to associate them with SQL Azure tables for persistence. This can be accomplished by using a database context class, DbContext. Create a new class, GolfContext, which simply derives from DbContext (in the System.Data.Entity namespace). The new GolfContext class will have two properties that will be used to work with the data. The GolfContext class should look like the code below.

	public class GolfContext : DbContext
	{
		public DbSet<Course> Courses { get; set; }
		public DbSet<Round> Rounds { get; set; }
	}
	

By default, the Entity Framework looks for a database connection string in the Web.config file that matches that name of the database context class. In this scenario, that would be GolfContext. The connection string is in the following format:

Data Source=tcp:YOUR-DATABASE-HERE.database.windows.net,1433; Database=GolfRounds; 
            User ID=YOUR-USERNAME@YOUR-SERVER; Password=YOUR-PASSWORD; Trusted_Connection=False; Encrypt=True;"

The connection string should look like the image below once complete.

Database Connection String v2

Notice that SQL logins are being used. This is because SQL Azure currently supports SQL logins only. You can leave these as placeholders for now. A SQL Azure account will be created in later steps.

Step 3 – Add the Controllers

Create a couple of controllers to work with our Course and Round entities. Now would be a good time to build the solution to make sure there are no errors. Building the solution at this point is also necessary for Visual Studio to be able to pick up the model classes that will be used in the controllers that will be created.

Create a new controller for both the Course and the Round models. To do that, right click on the “Controllers” folder in the ASP.NET MVC3 project, select “Add”, and then “Controller”. This will launch a dialog that will set up the scaffolding for the new controller. Select one of the model classes (Course or Round), and also the data context class to use (GolfContext). This should be done for both a CourseController and a RoundController.

Add Controller - Course - MyGolfStory

This process will automatically create a lot of baseline code in the project. The wizard will automatically generate the basic Create, Insert, Update, and Delete methods in the controller (notice the Template option in the wizard) as well as the respective views.

 

New Course and Round Views - MyGolfStory

The solution should build successfully at this point as well.

Step 4 – Sign up for a Windows Azure Account

A Windows Azure account is needed in order to use SQL Azure. Details on signing up for free 90-day trial of Windows Azure can be found at https://www.windowsazure.com/en-us/pricing/free-trial/. If you have an MSDN subscription, Windows Azure usage is included and you can use that as well. Remember, the Microsoft Platform Ready team can help with any questions related to creating a new Windows Azure Account. For help, please visit http://www.microsoftplatformready.com.

Step 5 – Create the SQL Azure Database

While Entity Framework will create the necessary database and tables, it will not create the SQL Azure server. In order to create the SQL Azure server, sign in to the Windows Azure Management Portal at http://windows.azure.com, doing so with the Windows Live ID associated with a valid Windows Azure subscription.

The first thing to do is create a new SQL Azure server.

  1. In the panel on the left side, click on “Database”.
  2. In the left panel, expand the “Subscriptions” folder if necessary to see the available Windows Azure subscriptions.
  3. Highlight the Windows Azure subscription that the SQL Azure server should be associated with. The SQL Azure servers will be listed below the subscription, as can be seen in the image below.

    New SQL Azure Server - Steps 1 - 3

  4. Click the “Create” button in the Server section of the top ribbon.
  5. Select the Region where the SQL Azure server should be located. This will be one of the six available Windows Azure data centers.
  6. Create a new SQL Azure administrative login and password. This username and password is like the SQL Server ‘sa’ account. You can create application specific logins, but for this walkthrough, just use the administrative account.
  7. By default, all communication with SQL Azure is blocked by a firewall. In order to connect to SQL Azure, firewall rules need to be enabled to allow external services to connect, as well as services running inside a Windows Azure data center. To add a new firewall rule, click the “Add” button on the “Create Server” dialogue to launch a new window that will allow you to enter the IP address to be added to the firewall rule list.

    SQL Azure - Firewall Rules - Blur

  8. At this point the SQL Azure server should be created and listed under the subscription selected earlier.

With the SQL Azure server created, add the necessary information to the Web.config file. Earlier a “GolfContext” connection string was created in the Web.config file. The placeholders there should now be replaced with the actual values for the SQL Azure server, the SQL Azure user ID (format of userID@server), and the password.

At this point proceed with launching the site. Simply press F5 from within Visual Studio to launch the site in Debug mode. If everything was done correctly to this point, the site should launch without any errors. No SQL Azure database has been created yet, because no Entity Framework code has been invoked. To do so, click either the “Courses” or “Rounds” navigation menu tabs at the top of the page. The page may take a few seconds to load – that’s Entity Framework creating the desired SQL Azure database and tables.

At this point proceed to the Windows Azure Management Portal to see the “GolfRounds” database created on the specified SQL Azure server. To view the tables, launch the SQL Azure Management Portal by selecting “Manage” from the “Database” section in the top ribbon. The SQL Azure Management Portal allows for viewing SQL Azure tables (such as the new Courses and Rounds tables), viewing dependencies, viewing execution plans, and other general database management features.

SQL Azure Management Portal v2

To make sure it works, feel free to try to add a new Course and then a new Round.

Step 6 – Add Retry Logic

At this point a new Course and Round should be able to be added to the SQL Azure database via the new ASP.NET MVC3 application. However, there is at least one more step that needs to be done. Working with SQL Azure is a little different than working with a SQL Server instance on-premises. It is often assumed when working with local systems, that there will be a highly reliable connection between the application server and the SQL Server database. It is also assumed the SQL Server database will essentially allow applications to beat up on it as much as wanted without complaining. With SQL Azure, this is not the case.

While SQL Azure is a reliable source for data, it is possible to experience transient errors when working with SQL Azure. By definition, a transient error is one that will go away quickly, often on the next attempt. These errors could be related to a bad internet connection between the application and the Windows Azure data center, or they could be related to some issues internal to SQL Azure, such as throttling. In any case, when working with SQL Azure (or any service consumed over the internet for that matter), it is always wise to build retry logic into the application. Doing so will add a little time to the overall development effort, but it is time well spent.

The Microsoft Patterns & Practices team has released a framework that greatly simplifies the process of adding retry logic to any application. The Transient Fault Handling Application Block is a library that can be added to any application that needs retry capabilities. The application block implements strategies to help with SQL Azure, Windows Azure storage (tables, blobs, queues), the Windows Azure Service Bus, and Windows Azure Caching.

The Transient Fault Handling Application Block can be added to a project via NuGet. If the NuGet Package Manager extension for Visual Studio is not yet installed, please follow the guidance at http://docs.nuget.org/docs/start-here/installing-nuget.

In order to add the Transient Fault Handling Application Block from the NuGet Package Manager Console, simply enter “Install-Package EnterpriseLibrary.WindowsAzure.TransientFaultHandling”at the Package Manager prompt. This will install the Transient Fault Handling Application Block, as well as any necessary dependencies.

NuGet - Adding the Transient Fault Handling App Block (snipped and edited)

The Transient Fault Handling Application Block is intelligent about how frequently an operation should be retried, and includes a set of error detection strategies to determine what error situation should trigger a retry. The Transient Fault Handling Application Block includes a few built-in error detection strategies, which encapsulates the conditions for a transient error. The SqlAzureTransientErrorDetectionStrategy does that for SQL Azure.

It should be noted that not every action should be retried. Carefully consider the implications of retrying an action. Retries should not be done indefinitely. At some point, for the sake of application performance and stability, the action should fail and a human may need to get involved.

For the purposes of this walkthrough, create a base controller class that can be used to encapsulate the retry policy to use in the application. The retry policy used here simply states that in the event a transient SQL Azure error is detected, the action should be retried 5 times, waiting 1 second before the first retry, and increasing 2 seconds between retries. An event handler can be used to determine when a retry occurs and to make a note of that, for example.

	public abstract class BaseController : Controller
	{
		protected RetryStrategy retryStrategy;
		protected RetryPolicy<SqlAzureTransientErrorDetectionStrategy> retryPolicy;

		protected BaseController()
		{
			retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
			retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(retryStrategy);

			retryPolicy.Retrying += (sender, args) => LogRetry(args);
		}

		protected virtual void LogRetry(RetryingEventArgs args)
		{
			var msg = string.Format("Retry Count: {0}, Delay: {1}; Exception: {2}.",
                args.CurrentRetryCount, args.Delay, args.LastException);
			Trace.WriteLine(msg);
		}
	}
	

With the base controller in place, modify our other controllers to use this new BaseController to take advantage of the retry logic. Any actions that execute against the SQL Azure database will need to have retry logic applied. This can be done by using the RetryPolicy.ExecuteAction() method and an anonymous function to carry out the database work.

	public class CourseController : BaseController
	{
		private GolfContext db = new GolfContext();

		public ViewResult Index()
		{
			var courses = retryPolicy.ExecuteAction(() =>
			{
				var items = db.Courses.ToList();
				return items;
			});
			return View(courses);
		}
		
		// . . . .
	}
	

The retry policy demonstrated in the image above will need to be done in both the CourseController and the RoundController – essentially any place that works with the GolfContext class (and thus SQL Azure). Please download the application source code for a full example.

Step 7 – Deploy the Application

The My Golf Story application is nearly complete. The final step is to deploy it to Windows Azure. In order to deploy the application to Windows Azure, the application needs to be associated with a Windows Azure deployment project. Fortunately, Visual Studio makes this very easy.

  1. Right click on the MyGolfStory ASP.NET MVC3 web project in Solution Explorer and then click on “Add Windows Azure Deployment Project”.

    Add Windows Azure Deployment Project

    Doing so will add a new MyGolfStory.Azure project to the solution and associate the MyGolfStory application with a Windows Azure web role.

    MyGolfStory.Azure Added to Solution

  2. With the deployment project in place, it is now time to publish the application to Windows Azure. To start that process, right click on the MyGolfStory.Azure project in Solution Explorer and select “Publish”.

    Publish Azure Project

    Note: The same can be accomplished by right clicking on the MyGolfStory project and selecting “Publish to Windows Azure”.

  3. This will bring up a wizard to guide your through the steps of publishing the application to Windows Azure. The first time a Windows Azure application is published from a new machine, a publish settings file will need to be downloaded. Subsequent publishes (of any Windows Azure solution) from that same machine should not need to that file again.

    Publish - Download Credentials

  4. Clicking on the “Sign in to download credentials” link should open a web page which will prompt you to sign in using the Windows Live ID associated with the Windows Azure subscription to be used.
  5. Download the publish profile (.publishsettings) file to your computer. This file will be needed in the next steps.

    Download PublishSettings File

  6. The publishing wizard should still be open. Click on the “Import” button and then browse and select the publish profile (the .publishsettings) file previously downloaded.

    Publish - Import Credentials

  7. Select the Windows Azure subscription to publish the application to and then click “Next”.
  8. It is here that a new hosted service can be created if one doesn’t already exist. The hosted service is a container for an application in Windows Azure. Enter a name that identifies the application, along with a Window Azure data center to publish to.

    New Hosted Service Dialog

  9. For the purposes of this walkthrough, leave the other options to their defaults, but feel free to explore them later. Clicking on the “Advanced Settings” tab will display options for creating a new storage account to use with the hosted service. The Windows Azure deployment package will be temporarily uploaded to Windows Azure blob storage (in the “vsdeploy” container”) in that storage account. Click “Next” to proceed.

    Publish - New Hosted Service Complete

  10. The final page of the wizard will display a summary of the deployment that is about to take place. Clicking the Save icon to the right of the Target profile dropdown box will allow the current settings to be saved for future use. Clicking on “Publish” will start the deployment process.

    Publish - New Hosted Service Final Step

    The first time a new hosted service is deployed to Windows Azure, the process can take 10-15 minutes. Windows Azure is performing several steps to prepare the service, including provisioning a new virtual machine, configuring the operating system, creating the web role, deploying application code, and configuring the load balancer and networking.

    Subsequent deployments to Windows Azure will take much less time. By default Visual Studio will perform an update to your existing application, instead of a completely new deployment.

  11. To monitor the Windows Azure deployment progress, do so from the Windows Azure Activity Log window in Visual Studio.

    Start of deployment - MyGolfStory

  12. Once the deployment is complete, clicking on the Website URL in the deployment monitor window will launch the site.

    Completed Deployment - MyGolfStory

What Was Just Accomplished

This walkthrough demonstrated how simple it is to create an ASP.NET MVC3 application that uses SQL Azure for the database, and Entity Framework as the ORM. While SQL Azure works in many ways like SQL Server on-premises, there are some special considerations that need to be accounted for – primarily the need for retries. Finally, the necessary steps were taken to deploy the new application to Windows Azure.

The My Golf Story application is a good starting point to explore the features available with Windows Azure. For example, pictures of the course, scorecard or snapshots from the round could easily be stored in Windows Azure blob storage and then displayed on the site. Another area to explore would be securing access with Windows Azure Access Control Services or even creating a Windows Phone application to supplement the web front-end.

Next Steps

Proceed to www.windowsazure.com to learn more about Windows Azure and to get the development tools. The Windows Azure Developer Center contains SDKs for many popular languages or frameworks such as .NET, Java, PHP, and node.js.

With the developer tools in place, it’s time to set up a new Windows Azure account. A free 90-day trial account can be obtained at https://www.windowsazure.com/en-us/pricing/free-trial/.

The full solution source code for this project can be found at http://bit.ly/MyGolfStoryAzure.

Microsoft Platform Ready

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories