65.9K
CodeProject 正在变化。 阅读更多。
Home

面向初学者的 MVC on Azure

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.88/5 (11投票s)

2011年12月4日

CPOL

5分钟阅读

viewsIcon

83028

本文将介绍如何在 Windows Azure 平台开发和部署 ASP.NET MVC 网站应用程序。

引言

Windows Azure 平台是微软的云解决方案。与其他的云解决方案相比,它最大的优势在于能够无缝集成 Microsoft .NET Framework 和开发环境,因此,普通的 .NET 应用程序可以轻松地迁移到 Azure。本文将介绍如何在 Windows Azure 平台开发和部署 ASP.NET MVC 网站应用程序。

使用 Azure SDK 开发 MVC 应用程序

为了开发可以在 Windows Azure 上运行的应用程序,您必须在计算机上安装 Windows Azure SDK。Azure SDK 可以从 Microsoft Windows Azure 门户网站下载。

Windows Azure Site

安装 Windows Azure SDK 后,您可以打开 Visual Studio 开始一个新项目。在您的 Visual Studio 项目模板中,有一个新的项目类别“云”。它只有一个项目模板:“Windows Azure 项目”。

Windows Azure Solution

选择“Windows Azure 项目”后,您可以选择不同的角色。角色代表运行在 Windows Azure 平台上的应用程序。Windows Azure 平台定义了三种类型的角色:Web 角色、Windows 角色和 VM 角色。Web 角色代表网站应用程序,Windows 角色代表 Windows 服务应用程序,VM 角色代表独立的 Windows 环境。在 Visual Studio 中,您可以创建 ASP.NET Web 角色、ASP.NET MVC 3 Web 角色、ASP.NET MVC 2 Web 角色和 WCF 服务 Web 角色。并非所有应用程序都适合在 Windows Azure 中运行。

New Windows Azure Project

如果您选择 ASP.NET MVC 3 Web 角色项目,可以按照项目向导创建网站应用程序。该项目向导与普通的 ASP.NET MVC 3 项目向导相同。下面是您的解决方案的样子:

Project Detail

将此解决方案与普通的 ASP.NET MVC 解决方案进行比较,您会发现其中多了一个 Azure 项目。它包含了我们 Windows Azure 项目的配置。有两种配置文件:服务定义文件 *ServiceDefinition.csdef* 和服务配置文件 *ServiceConfiguration.cscfg*。

服务定义文件定义了您解决方案中的所有角色及其信息,包括:

  1. 终结点信息。例如,Web 角色使用 HTTP。
  2. 存储信息。例如,本地存储或 Azure 存储。
  3. 任何自定义配置信息。

以下是我 Azure 项目中 *ServiceDefinition.csdef* 文件的样子:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MVConAzure" 
  xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="Demo.MvcWebRole" vmsize="Small">
<Sites>
<Site name="Web">
<Bindings>
<Binding name="Endpoint1" endpointName="Endpoint1" />
</Bindings>
</Site>
</Sites>
<Endpoints>
<InputEndpoint name="Endpoint1" 
    protocol="http" port="80" />
</Endpoints>
<Imports>
<Import moduleName="Diagnostics" />
</Imports>
</WebRole>
</ServiceDefinition>

服务定义在运行时无法更改。任何更改都需要重新部署您的服务。您的服务仅限于使用在此文件中定义网络终结点和资源。您可以将此配置视为定义您的服务的架构以及各部分如何协同工作。

服务配置文件包含您服务中角色实例所需的所有配置。每个角色都有自己的配置。配置文件内容可以在运行时更改,这可以避免在某些角色配置更改时重新部署应用程序。您还可以以类似于读取 ASP.NET 应用程序的 *web.config* 文件的方式在代码中访问配置。

以下是我 Azure 项目中 `ServiceConfiguration` 的样子:

<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="MVConAzure" 
    xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" 
    osFamily="1" osVersion="*">
<Role name="Demo.MvcWebRole">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" 
        value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>

您现在可以编译并运行应用程序了。应用程序不会像普通的 ASP.NET MVC 应用程序那样在 Visual Studio Cassini 本地 Web 服务器上运行。它将在 Windows Azure 模拟器中运行。Windows Azure 平台包含三个主要部分:Windows Azure、SQL Azure 和 Windows Azure AppFabric。模拟器非常类似于 Windows Azure 平台。它允许开发人员在不部署到公共云的情况下运行和调试应用程序。注意:有时您会遇到此错误:

Windows Azure Tools: Failed to initialize Windows Azure storage emulator. 
  Unable to start Development Storage. Failed to start Storage Emulator: the SQL Server instance 
  ‘localhost\SQLExpress’ could not be found. Please configure the SQL Server instance 
  for Storage Emulator using the ‘DSInit’ utility in the Windows Azure SDK.

这是因为 Windows Azure 存储模拟器默认使用 SQLExpress 作为后端数据库。如果您没有安装 SQLExpress 数据库,或者没有以默认名称安装,那么存储模拟器将找不到它。要修复此错误,您可以使用 DSInit 将存储模拟器指向您的本地 SQL Server 数据库。

将应用程序部署到 Windows Azure 公共云

要将应用程序部署到 Windows Azure 平台,您首先需要拥有一个 Windows Azure 帐户。您可以注册一个 90 天免费试用的 Windows Azure 帐户。但是,您需要提供您的信用卡信息,以便 Microsoft 在您超出使用限制时向您的信用卡收费。部署非常简单。您可以直接在 Visual Studio 中将基于 Windows Azure 的应用程序发布到 Windows Azure 公共云。

Publish

右键单击 Azure 项目,然后选择“发布”菜单项。这将打开“发布 Windows Azure 应用程序”窗口。

  1. 选择您的订阅。
  2. 选择您的设置。为托管服务指定一个名称。此名称将用作您的子域名。
  3. 注意:如果您是第一次将应用程序发布到 Windows Azure 平台,系统会提示您添加 Windows Azure 身份验证设置。

  4. 单击“发布”按钮开始发布过程。Visual Studio 将首先编译您的应用程序,然后使用您的帐户信息连接到 Windows Azure 平台开始部署。您的 Visual Studio IDE 中会显示一个新选项卡“Windows Azure 活动日志”,其中显示部署进度。整个部署可能需要几分钟时间。

Publish Status in Visual Studio

您也可以在 Azure 控制面板中查看部署状态。

Publish Status in Azure

部署完成后,“Windows Azure 活动日志”窗口将显示“已完成”状态以及所有部署信息。

Windows Azure Activity Log

您可以使用分配的 URL,在此处是 *http://mvconazure.cloudapp.net*,来查看已部署在 Windows Azure 公共云中的 MVC 网站应用程序。

Web Page

摘要

云计算因其成本节省、可伸缩性和易于实施等优点而日益普及。Windows Azure 平台是 .NET 应用程序开发者的首选云服务,因为它使我们能够利用我们所有的 .NET 开发知识加入这一新趋势。

© . All rights reserved.