With the November release of the Windows Azure Tools for Microsoft Visual Studio, we’ve done some things in Visual Studio 2010 to make it easier to use ASP.NET MVC and Windows Azure together.
Note: In the November release of the Tools + SDK, Windows Azure only supports .NET 3.5 SP1, .NET 4.0 projects are not supported.
Creating a New Project
The first thing you’ll notice in Visual Studio 2010 is that we now have a project template option for an ASP.NET MVC 2 Web Role. (Click on File | New | Project… | Windows Azure Cloud Service)
Note: To use ASP.NET MVC and Windows Azure together on Visual Studio 2008 – please follow the steps under “Using an Existing Project”.
This makes it easy to create a new ASP.NET MVC project in the context of a cloud service. This is available in Visual Studio 2010 and Visual Web Developer 2010 Express.
The differences between an ASP.NET MVC Web Role and a regular ASP.NET MVC project are the following:
1) Windows Azure specific references to the diagnostics library, runtime and storage client.
2) Setting the CopyLocal property of the System.Web.Mvc reference to true to ensure that it gets copied up to the cloud which will not have it by default.
3) Web.config is modified to add a Windows Azure specific TraceListener (Microsoft.WindowsAzure.Diagnostics.DiagnosticsMonitorTraceListener)
4) WebRole.cs file that includes template code to bootstrap the logging and diagnostics infrastructure as well as a default behavior for handling configuration changes.
Aside from that, the project is the same as any ASP.NET MVC project that you would create outside the context of a Windows Azure cloud service.
Using an Existing Project
To use an existing ASP.NET MVC project, simply add it to the solution as an existing project. This is also how you would use ASP.NET MVC Projects (or any existing ASP.NET Web Application project) on VS 2008.
Note: In the November release of the Tools + SDK, Windows Azure only supports .NET 3.5 SP1, .NET 4.0 projects are not supported. If you are adding a project that target .NET Framework 4.0, please change the target framework in the project properties to us the .NET Framework 3.5.
Right click on the Roles node in the Cloud Service project and select Add | Web Role Project in Solution…
The select the project you just added.
Your existing MVC is now associated as a web role. The changes that were made above automatically to the project (adding references, startup code, CopyLocal=true and web.config trace listener change) you will have to do manually to get that functionality.
Using ASP.NET Providers
The default ASP.NET MVC project template makes use of ASP.NET providers: Membership, role and profile and rely on SQL Server. You have two choices on reworking these providers, either use SQL Azure or use the Windows Azure sample providers that use Windows Azure storage.
Because ASP.NET will setup SQL Express automatically for the providers, if you don’t migrate your providers to use one of the cloud data options, you will find that the providers just work when running on the local development fabric but will then fail when running on the cloud where a local SQL Server instance is not available.
To convert your project to use the Windows Azure Storage based sample implementation of the ASP.NET providers see this post: http://bit.ly/1M1HSN
Currently the existing aspnet_regsql.exe tool and the ASP.NET functionality to generate the database for the providers does not work with SQL Azure. We have some scripts coming that will workaround the problem. I’ll update this post when those become available. [Update: these scripts are now available, please see: http://blogs.msdn.com/jnak/archive/2009/11/24/asp-net-provider-scripts-for-sql-azure.aspx]
There are two known issues with using ASP.NET MVC and Windows Azure Tools:
1) Creating a unit tests with the Visual Basic version of ASP.NET MVC results in a unit test project that fails to build. This is because the Windows Azure Tools creates the projects as .NET 3.5 projects. The workaround is to not create a unit test project. This will be resolved in the RTM version of Visual Studio. Note: this is not an issue with Visual C#.
2) Creating an ASP.NET MVC Web Role on Visual Web Developer 2010 Express results in a project that fails to build:
Error 1 The type name ‘MembershipCreateStatus’ could not be found. This type has been forwarded to assembly ‘System.Web.ApplicationServices, Version=184.108.40.206, Culture=neutral, PublicKeyToken=31bf3856ad364e35’. Consider adding a reference to that assembly. c:\users\vslab2\documents\visual studio 2010\Projects\CloudService7
The workaround is to set the Target framework of the MVC project to .NET Framework 3.5.