ASP.Net MVC on Windows Azure | ASP.Net MVC Web Role

ASP.Net MVC on Windows Azure | ASP.Net MVC Web Role

ASP.Net MVC Web Role Windows AzureWhen you install Windows Azure Tools for Visual Studio, you only get a project template for an ASP.Net Web Role. In this post I’ll talk about how to create a new ASP.Net MVC Web Role or move an existing ASP.Net MVC Application to Windows Azure.

There are 2 ways to do this:

  • ASP.Net MVC Web Role Windows AzureManually adding an ASP.Net MVC application as a Web Role (suitable both for a new ASP.Net MVC application and for an existing one).
  • Use a Project Template to simply create a new ASP.Net MVC Web Role (new ASP.Net MVC application only).

Creating a New ASP.Net MVC Web Role

The following steps apply both for creating a new ASP.Net MVC application and to moving an existing ASP.Net MVC application to Windows Azure.

1. Create an empty ASP.Net MVC Application (with or without a test project), or make sure you have an existing ASP.Net MVC application that you want to move to the Windows Azure.

ASP.Net MVC Web Role Windows Azure

2. Close this solution and create a new Blank Cloud Service. Make sure you start Visual Studio as an administrator because working with the Development Fabric requires that.

ASP.Net MVC Web Role Windows Azure

3. Add an existing project to the cloud service and select the ASP.Net MVC Application you have created earlier.

ASP.Net MVC Web Role Windows Azure

4. Right click the ASP.Net MVC project and select Unload Project.

ASP.Net MVC Web Role Windows Azure

5. When the project is unloaded, right click it and edit the project file.

ASP.Net MVC Web Role Windows Azure

6. The project file opens in the XML Editor.

<Project ToolsVersion="3.5" DefaultTargets="Build" ...>

  <PropertyGroup>

    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>

    ...

  </PropertyGroup>

  ...
</Project>

Inside the PropertyGroup Element, add the RoleType element and mark this ASP.Net MVC project as a Web Role.

<Project ToolsVersion="3.5" DefaultTargets="Build" ...>

  <PropertyGroup>

    <RoleType>Web</RoleType>

    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>

    ...
  </PropertyGroup>

  ...

</Project>

7. Close this project file, and reload the project.

ASP.Net MVC Web Role Windows Azure

8. Since the ASP.Net MVC now runs on the Windows Azure Service Hosting environment and will probably use its services – add a reference to Microsoft.ServiceHosting.ServiceRuntime.dll.

ASP.Net MVC Web Role Windows Azure

9. Right click the Roles node in the project tree, and add a Web Role Project in the solution. Select the ASP.Net MVC project from the project list.

ASP.Net MVC Web Role Windows Azure

Now the ASP.Net MVC project is shown as a Web Role.

ASP.Net MVC Web Role Windows Azure

10. Since the Windows Azure provides us only with the default .Net Framework 3.5 SP1 assemblies, we must make sure we also copy the ASP.Net MVC Assemblies to the cloud. To do this, right click on each of the following references of the ASP.Net MVC application, and set the Copy Local property to True:

  • System.Web.Abstractions
  • System.Web.Mvc
  • System.Web.Routing

ASP.Net MVC Web Role Windows Azure 

That’s it. You can run the application to make sure that it is running on the Development Fabric.

ASP.Net MVC Web Role Windows Azure

Creating an ASP.Net MVC Web Role using a Project Template

Thanks to a great word done by the community, we can use a project template to do all the above steps for us. Here’s how to do this:

1. Download the project template from Codeplex at https://c4mvc.codeplex.com/.

ASP.Net MVC Web Role Windows Azure

2. Create a new project and select the Azure ASP.Net MVC template in the Community for MVC category.

ASP.Net MVC Web Role Windows Azure

This creates a new Cloud Service for Windows Azure with ASP.Net MVC Application as a Web Role.

ASP.Net MVC Web Role Windows Azure

Enjoy!