http://www.developer.comservices/creating-an-azure-app-step-6-add-retry-logic.html

Back to article

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


January 18, 2012

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 Content\Site.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 \Views\Shared\_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 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


Thank you to the following technical experts for reviewing this article: Ted Neward, Chad Thomas, Samidip Basu, Jared Faris, Sean Kelly, Mike Wood and Brett Carlson.

 

This article was sponsored by Microsoft Corporation, but written independently by the author.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date