次の方法で共有


ASP.NET MVC and Windows Azure (November 2009 edition)

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”.

clip_image002

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.

clip_image004

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.

clip_image006

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.

clip_image008

Right click on the Roles node in the Cloud Service project and select Add | Web Role Project in Solution…

clip_image010

The select the project you just added.

clip_image012

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: https://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: https://blogs.msdn.com/jnak/archive/2009/11/24/asp-net-provider-scripts-for-sql-azure.aspx]

Known Issues

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=4.0.0.0, 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.

Comments

  • Anonymous
    December 08, 2009
    I figured out the script to use, to create a compatible AspNet_regsql installation in SQL Azure, if you are interested in it.  I also posted it on my blog http://www.longcloud.co.nz/Blogs/archive/2009/12/09/aspnet_regsql-like-script-for-usage-with-sqlazure.aspx

  • Anonymous
    December 29, 2009
    I am not able to find the location where to update the trace listener in the web.config. Can you suggest me the code on how and where to update. Thanks,

  • Anonymous
    December 30, 2009
    @hintzen -- we've released the scripts now, they are here:  http://blogs.msdn.com/jnak/archive/2009/11/24/asp-net-provider-scripts-for-sql-azure.aspx

  • Anonymous
    December 30, 2009
    @dasari, we add the trace listener to the default ASP.NET Web Role template so you can always look in that web.config.  Here's the section we add:  <configuration>    <system.diagnostics>        <trace>            <listeners>                <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"                    name="AzureDiagnostics">                    <filter type="" />                </add>            </listeners>        </trace>    </system.diagnostics>

  • Anonymous
    December 31, 2009
    thanks for the code. it helped -Rama charan

  • Anonymous
    January 02, 2010
    using System.Linq; using Microsoft.WindowsAzure.Diagnostics; using Microsoft.WindowsAzure.ServiceRuntime; public class WebRole : RoleEntryPoint    {        public override bool OnStart()        {            DiagnosticMonitor.Start("DiagnosticsConnectionString");            // Restart the role upon all configuration changes            // Note: To customize the handling of configuration changes,            // remove this line and register custom event handlers instead.            // See the MSDN topic on “Managing Configuration Changes” for further details            // (http://go.microsoft.com/fwlink/?LinkId=166357).            RoleEnvironment.Changing += RoleEnvironmentChanging;            Microsoft.WindowsAzure.CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>            {                configSetter(Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(configName));            });            return base.OnStart();        }        private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)        {            if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))                e.Cancel = true;        }    }