Silverlight Hack

About Me

Welcome to Silverlighthack.com. This is a site where you can find many articles on Silverlight, Windows Phone 7 and .NET related technologies.

My name is Bart Czernicki. I have been working with computers since 1988 and have over 12 professional years in the IT field focusing on architecture, technology strategy and product management. I currently work as a Sr. Software Architect at a large software development company.

Below is the cover of my new book that shows how Silverlight's unique RIA features can be applied to create next-generation business intelligence (BI 2.0) applications.

Contact: bartczernicki@gmail.com

NONE of the comments or opinions expressed here should be considered ofmy past or current employer(s). The code provided is as-is without anyguarantees or warranties.

Step by step instructions to deploy ASP.NET MVC 3 to Windows Azure

Abstract: This article will show you the steps to overcome the lack of Visual Studio 2010/Azure SDK tooling to deploy ASP.NET MVC 3 as a web role to the Azure cloud. As of the time this article was written, there are many articles out there that overcomplicate the steps and some are even outdated with the new Azure 1.4 SDK release. This article will allow you to work with the full ASP.NET MVC 3 tooling. This article will be relevant until Microsoft releases an update to the Visual Studio 2010 tooling for Azure (probably not until the Azure 1.5 SDK). The Visual Studio 2010 project/source code is included below.

10/01/2011 Update: The latest Windows Azure SDK (September 2011 Update) now includes ASP.NET MVC 3 by default. However, if you are interested in including ASP.NET MVC4 in Azure, these steps can be followed below (with some changes).

Active Windows Azure hosted service account (there is a promotion going on to receive Windows Azure for FREE until September 30th 2011, much better than the FREE 30 day trial if you just want to test things out or run a small blog: http://www.microsoft.com/windowsazure/free-trial/)

1) Create a new ASP.NET MVC 3 Project

Open Visual Studio 2010 (as an Administrator)

The Azure Cloud emulator needs to run in elevated privaledges

Inside Visual Studio 2010, go to File -> New Project

Select "ASP.NET MVC 3 Web Application"

I named the project "AspNetMVC3DeployToAzure" *you can call it whatever you like)

Select "Internet Application" from the next dialog windo

Note that we will not be deploying the ASP.NET membership provider to SQL Azure (that will be for another time)

Just as long as you do not click Log On the application will function correctly

If you build the application and debug it, you should see the generic ASP.NET MVC 3 startup screen

2) Add a Windows Azure Cloud Project

In the Visual Studio 2010 Solution Explorer, right-click on the AspNetMvc4DeployToAzure solution and select Add -> New Project...

In the New Project Dialog,

select "Cloud" from the Installed Templates

select the "Windows Azure Project"

I named the project "WindowsAzureAspNetMvc3Deploy" (you can name it whatever you prefer)

In the next dialog, do NOT select any Windows Azure roles. Leave it empy and click OK.

3) Add the Azure Web Role components to the ASP.NET MVC 3 Project

In the Solution Explorer, highlight the ASP.NET MVC 3 project by clicking on it (this will tell Visual Studio the context you are in(

Select the WindowsAzure.WebRole package and click Install. Close the dialog after you are done (VS 2010 will ask you to reload the project...click OK) The following changes will be made to the ASP.NET MVC 3 Project:

The package will add the necesary Windows Azure SDK 1.4 assemblies

The package will make changes necessary in the web.config for Windows Azure

The package will add a class that facilitates the new Azure SDK 1.4 change for performign a task when the role is deployed

In the Solution Explorer, right-click on the Roles folder of the Windows Azure project. (this will tell our Windows Azure cloud project that we want to associate to the ASP.NET MVC 3 web role)

Select Add -> "Web Role Project in Solution..."

In the previous step, we ran the NuGet package that "converted" our traditional ASP.NET MVC 3 application to a web role by adding all the necessary assemblies. Therefore, this step is telling Azure that we want to add an existing Web Role from our current solution.

In the "Associate with Web Role" dialog, click OK (as we only have one available project)

Your solution should now look like the screen shot below.

Note that we have additional Azure assemblies referenced in the ASP.NET MVC 3 project, we have a new class WebRole.cs, the web.config has Azure elements in it and inside the Azure cloud project we have a new web role added

.

You can now buiild and debug the solution. If you did everything properly, this will launch the Windows Azure emulator and run the application.

4) Add the ASP.NET MVC 3 assemblies to the bin folder

Note: the emulator is just an emulator, so it is still utilizing resources on your workstation (i.e. ASP.NET MVC 3, Web Matrix assemblies). In a real Azure Virtual Machine, you will ONLY have the basic .NET 4.0 assemblies (Microsoft only keeps the VMs to a bare minimum). Therefore, we need to add those assemblies as part of the deployment into Azure VM.

Note: An alternative approach is to utilize the ASP.NET MVC 3 installer as a Azure start up task (each time the VM is restarted or re-provisioned in case of an error or recovery process) or manually set each assembly to "Copy Local" to True.

In the ASP.NET MVC 3 Tools Update Microsoft provides a very easy way to add all the required ASP.NET MVC 3 assemblies automatically for bin deployment. (thanks to Mike Brown from http://azurecoding.net/ for pointing this out).

You Solution Explorer in Visual Studio should look like this after adding the ASP.NET MVC 3 Dependency assemblies.

:

5) Publish to Windows Azure

Now you are ready to publish to Windows Azure. Right-click on the Windows Azure project and select Publish. Select your Azure credentials and storage information.

Note: If you do not have the Azure hosted service, storage or credentials set up you will have to create it. Step by step instructions can be found here (Microsoft has better instructions than I will write on this)

I highly recommed you also select the "Enable Intellitrace for .NET 4 Roles". This will show you any errors that you get if you don't have the proper ASP.NET MVC 3 assemblies installed. Here is a good article on how you can view the IntelliTrace logs: http://www.nickharris.net/2010/09/how-to-enable-intellitrace-on-windows-azure/. For exampe, if you forget to enable "Copy Local" to true for the System.Web.Mvc.dll the Azure publish process will cycle and you will not know why.

If you forget a certain assembly, you will also get an error like this (if you turn off custom errors in the web.config)

Visual Studio 2010 Source Code For Project

This source code for this project includes some minor changes unrelated to the steps above: