Friday, January 14, 2011

How To Publish an ASP.NET MVC 3 App to Windows Azure

With the final, "RTM" version of ASP.NET MVC 3 shipping today, I've had a number of people ask me how to publish an MVC 3 app to Windows Azure. It's a bit more complicated than publishing an MVC 2 app, at least at this time of writing. I'll walk you through the process.

Note: You need to have installed ASP.NET MVC 3 and Windows Azure Tools 1.3 to follow this process.

0. Upgrade Your MVC 2 App
If you're starting with an MVC 2 app, you'll need to upgrade it first. I won't describe the upgrade process here, because we've already documented it in the release notes. Also, a couple of the MVC team's developers built an upgrade tool to make the process easier, so you might try that instead of manually upgrading your app's project.

If you aren't starting from an existing MVC 2 app, just create a new MVC 3 app before moving to the first step.

1. Create the Windows Azure Project
Add a new Windows Azure Project in the same solution as your MVC 3 app. When you see the New Windows Azure Project dialog (pictured below), just click OK without adding any roles.

Why didn't I have you add any roles? Because the 1.3 version of the Azure tools doesn't have support for MVC 3, and therefore it isn't in the list of available web role types. You'll need to add your MVC 3 app after-the-fact, which you'll do in the next step.

2. Add the MVC 3 App as a Web Role
To add your MVC app as an Azure web role, right-click the Roles folder in the Azure project, select Add, then select Web Role Project in solution (as pictured below).

Your MVC 3 app will be in the list of projects on the next dialog. Select it and click OK. Verify that your MVC 3 app now is listed in the Roles folder.

You might think you could publish now, but the role would fail to start because MVC 3 has dependencies that don't exist in the Azure hosted service environment. We'll work around that in the next step.

3. Prepare Assemblies for "Bin Deployment"
The Azure hosted service environment doesn't have MVC 3 installed, so you effectively need to prepare what we call a "bin deployment" for your MVC 3 app, which simply means that MVC and all of the assemblies (DLLs) upon which it depends are bundled (included) with your app, locally in the ~\bin folder.

First, make sure all of the following assemblies are added as references to your MVC 3 app (some may already be added as references to your app, but not all of them will):

  • Microsoft.Web.Infrastructure
  • System.Web.Helpers
  • System.Web.Mvc
  • System.Web.Razor
  • System.Web.WebPages
  • System.Web.WebPages.Deployment
  • System.Web.WebPages.Razor

Next, for each of those assemblies, open the Properties pane (right-click the reference, then click Properties) and change the Copy Local setting to True.

Build your solution to check for errors before moving to the next step, which is actually publishing to Azure.

4. Publish as Normal
You should now be able to publish your Windows Azure project, which includes your MVC 3 app, as you would normally with any other type of web role.

Note: I've seen an odd problem, albeit not consistently, when deploying an MVC 3 app to Azure where the role takes a very long time to reach the ready state, after cycling from starting, to stopping, to recycling, and back again several times. If you encounter this problem, you might try stopping the deployment and re-deploying; that seemed to help me.

I hope this helps.

If you have any questions or concerns about this 'blog post, or in general, please send an email to me at drewmi <at> microsoft <dot> com. I'm also on Twitter at