Tuesday, January 18, 2011

How to Deploy an ASP.NET MVC 3 App to Web Hosting with "\bin Deployment"

Already know what "/bin deployment" means, and just looking for the list of assemblies you need to include for MVC 3? Skip to the end.

Since ASP.NET MVC 3 was released last week, I've had several folks ask me how they can deploy their MVC 3 apps to a web hosting service. They asked because when they attempted to upload their app's files, they saw this error (or one similar to it):


What's the Problem?

When you install MVC 3 on your local machine, a number of assemblies are registered in the GAC. MVC 3 needs these assemblies. Unless your web hosting service has installed MVC 3 on their servers (and many haven't, yet), those assemblies won't be there, and you'll see an error similar to the one above.

What's the Solution?

As with previous versions of MVC, we suggest you solve this with what we call "\bin deployment." Bin Deployment is just a fancy term that means "include the MVC assembly (and its dependencies) in your web application's /bin folder." It's not hard to prepare your project for Bin Deployment, but there are a few more assemblies involved compared to MVC 2. I'll show you what you need to do, step by step.

Step 1: Add Explicit References for MVC and Its Dependencies

Your MVC app's project probably won't have references to all of the assemblies it needs, because they're in the GAC. So you need to add them. Here is the list (they'll all be available in the .NET tab of the Add Reference dialog):
  • Microsoft.Web.Infrastructure
  • System.Web.Helpers
  • System.Web.Mvc
  • System.Web.Razor
  • System.Web.WebPages
  • System.Web.WebPages.Deployment
  • System.Web.WebPages.Razor


Step 2: Change Each Reference's Copy Local Property to True

After adding the references, you need to set the Copy Local property for each of the references you just added to True, as pictured below.


Step 3: Re-Build and Deploy as You Normally Would

Now, when you build your app, the MVC assembly and its dependencies will be copied to the /bin directory, allowing you to deploy as you normally would.

I hope this helps.

If you have any questions or concerns about this 'blog post, or in general, please send an email to drewmi at microsoft dot com. I'm also on Twitter at http://twitter.com/anglicangeek.

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 http://twitter.com/anglicangeek.