SlideShare a Scribd company logo
GWAB Lab-雲端專案
本手作實驗 (Hands-on Lab) 為 GWAB 2013 Taiwan 的手作實驗之一, 本手作實驗將會建
立兩個雲端專案,一個是 Web Role,用於展示將 Web 應用程式掛上 Windows Azure 的
流程;另一個是 Worker Role,用以展示將 ASP.NET Web API Self-hosting 服務掛載到
Windows Azure 以供應雲端上的服務,最後會使用 SQL Database 在兩個不同的執行個體
之間維護 Session 工作狀態。
本實作的學習目標
當實作完成時,學員應該會:
 使用 Visual Studio 2012 建立雲端專案。
 部署 ASP.NET MVC 4.0 Web 應用程式到雲端。
 部署 ASP.NET Web API 服務到雲端。
 在不同執行個體之間共享 Session 工作階段資料。
本實作的前置需求
1. Visual Studio 2012 或是 Visual Studio 2012 Express for Web。
2. Windows Azure SDK for .NET 與 Visual Studio Tools for Windows Azure v1.8
3. SQL Server 2012 或 SQL Server Express 2012 或 SQL Server Express 2012 LocalDB
4. 有效的 Windows Azure 訂閱或申請 90 天試用訂閱。
本實作的估計時間
大約 45-60 分鐘
實作的步驟
練習 1:建立與部署雲端專案
練習 2:建立與部署雲端上的 Web API 服務
練習 3:在執行個體之間共用 Session 工作狀態資料
練習 1. 建立與部署雲端專案
1. 請開啟 Visual Studio 2012,並選擇【檔案 -> 新增 -> 專案】,這時應會看到新增專
案的對話盒,請選擇 Visual C# -> Cloud -> Windows Azure 雲端服務。
2. 將專案名稱命名為 FirstCloudService,並按確定,此時會出現新增 Windows Azure 雲
端服務的對話盒,請選擇 ASP.NET MVC 4 Web 角色,並按中間的 “>” 將專案加到方
案內,然後按確定。
3. 在 MVC 專案類型對話盒出現時,請選擇網際網路應用程式專案,然後按確定。Visual
Studio 會將雲端專案與 MVC 應用程式專案產生出來。
4. 在 Visual Studio 完成專案建置後,請按 F5 來執行專案,當應用程式啟動完成時,您
會看到下列畫面。
5. 請將除錯器停止後,在 Visual Studio 2012 的雲端專案上按下右鍵,並選擇發行。
6. 此時發行 Windows Azure 應用程式精靈會出現,請選擇『登入以下載憑證』,進入
Windows Azure 管理介面1
,這時會自動下載一份發行設定檔。
1
會需要登入。
7. 請回到 Windows Azure 發行精靈,按下匯入按鈕,並選擇前一步下載的發行檔案,並
按開啟,即可在訂用帳戶中看到自己的訂閱。
8. 確認要使用的訂閱後,按下一步,然後在雲端服務的下拉選單中選擇建立新項目。
9. 在建立 Windows Azure 服務對話盒出現時,於名稱給定一個獨一無二的名稱,位置請
選擇 East Asia (東亞),然後按確定。
若名稱重覆時會出現錯誤訊息:
NOTE:
您也可以先在 Windows Azure 管理介面中建立新的雲端服務後,再開啟 Windows Azure
發行精靈,選擇已建立的雲端服務。
10. 請選擇進階設定頁籤,並在儲存體帳戶的下拉選單中選擇建立新項目,並給予一個獨
一無二的名稱,位置一樣選擇 East Asia,然後按確定。完成這個步驟之後,按下一
步。
NOTE:
在這裡所建立的儲存帳戶是 Visual Studio 為了要暫存發行檔案所需要的。
11. 於發行摘要頁面中,確認發行的組態後,按下發行按鈕。
12. Visual Studio 會開始發行雲端服務專案,並且開啟 Windows Azure 活動記錄檔視窗,
您可以在這裡看到部署的進度。
13. 當 Visual Studio 發行完成後,網站 URL 即可使用,請直接按下該連結,會自動開啟
瀏覽器,您應該會看到網站已正常的執行在 Windows Azure 環境。
NOTE:
雲端服務的 URL 是[cloudservicename].cloudapp.net,您可以使用 DNS CNAME 來設定成
您想要的 DNS 名稱。
練習 2. 建立與部署雲端上的 Web API 服務
1. 在前一個練習中所建立的雲端專案上的角色資料夾按右鍵,選擇加入 > 新增背景工作
角色專案,然後在加入新的.NET Framework 4.5 角色專案中,選擇背景工作角色,將
專案命名為 WebApiService,然後按確定。
2. 在 Visual Studio 完成加入 Worker Role 到專案的工作後,請到 WebApiService 專案
上,於參考資料夾上按右鍵,選擇管理 NuGet 套件:
3. 當管理 NuGet 套件對話盒出現時,請確認左側是選擇線上,然後在搜尋的對話中輸入
Web API Self Host,NuGet 會搜尋目前可用的套件,請確認有找到 Microsoft
ASP.NET Web API Self Host,然後按安裝。當授權合約出現時,請選擇我接受,即可
繼續進行安裝。當完成安裝後,請關閉此對話盒。
4. 請於參考資料夾上按右鍵,選擇加入參考,並在參考管理員中,於組件>架構選擇
System.Net.Http 組件後,按確定。
5. 請在 WebApiService 內加入一個新的類別,名稱為 ValuesController,然後在
ValuesController.cs 中加入下列程式碼,完成後將它儲存:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
namespace WebApiService
{
public class ValuesController : ApiController
{
public IEnumerable<string> Get()
{
return new string[] { "Value1", "Value2" };
}
}
}
6. 打開 WorkerRole.cs 檔案,在命名空間宣告中加入下列宣告:
using System.Web.Http;
using System.Web.Http.SelfHost;
7. 於 WorkerRole.cs 檔案中,宣告下列類別層次的變數:
public class WorkerRole : RoleEntryPoint
{
private HttpSelfHostServer _httpServer = null;
private HttpSelfHostConfiguration _httpConfiguration = null;
...
}
8. 請於 WorkerRole.cs 檔案中的 OnStart 方法中,加入下列紅字部份的程式碼:
public override bool OnStart()
{
ServicePointManager.DefaultConnectionLimit = 12;
this._httpConfiguration = new
HttpSelfHostConfiguration("http://[cloudservicename].cloudapp.ne
t:8080");
this._httpConfiguration.Routes.MapHttpRoute(
name: "ValueApi",
routeTemplate: "api/{controller}",
defaults: new { controller = new ValuesController() });
this._httpServer = new
HttpSelfHostServer(this._httpConfiguration);
return base.OnStart();
}
NOTE:
[cloudservicename]即是您於前面所建置的雲端服務名稱。
9. 請到 OnRun 方法中,加入下列紅字部份的程式碼:
public override void Run()
{
this._httpServer.OpenAsync().Wait();
while (true)
{
Thread.Sleep(10000);
}
}
10. 請在 WorkerRole 類別中,加入一個新的 OnStop 覆寫方法:
public override void OnStop()
{
this._httpServer.CloseAsync().Wait();
base.OnStop();
}
11. 請在雲端專案內的角色資料夾中,找到 WebApiService 並於其上按右鍵,以打開雲端
專案屬性:
12. 於專案屬性視窗中,於左邊選擇端點,並加入一個新的端點,類型為 Input,通訊協
定為 Http,公用連接埠為 8080,私人連接埠也是 8080。
NOTE:
公用連接埠因為前一個練習所建立的 Web Role 佔用了 Port 80,因此這裡不能使用 Port
80,所以改用 8080。私人連接埠則要和服務所用的連接埠對應,由於程式中使用的是
8080,因此私人連接埠也要使用 8080,否則會連接不上。
13. 請打開雲端專案中的 ServiceDefinition.csdef 檔案,找到 WorkerRole 的設定,並加入
要求執行期必須得到核准權限的設定,否則會在初始化時發生死亡迴圈 (dead loop)
問題,因為 Self-hosting 無法取得正確的權限以附掛通訊埠。
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="FirstCloudService"
xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/Servi
ceDefinition" schemaVersion="2012-10.1.8">
<WebRole name="MvcWebRole1" vmsize="ExtraSmall">
...
</WebRole>
<WorkerRole name="WebApiService" vmsize="ExtraSmall">
<Runtime executionContext="elevated" />
...
</WorkerRole>
</ServiceDefinition>
14. 請依練習一的作法進行專案的發行,當發行完成後,請使用瀏覽器瀏覽
http://[cloudservicename].cloudapp.net/api/values,應該可以看到下圖的畫面,表示
服務己經成功運作。
NOTE:
[cloudservicename]即是您於前面所建置的雲端服務名稱。
練習 3. 在執行個體之間共用 Session 工作狀態資料
本練習將會使用 SQL Database 結合 ASP.NET 4.0 新增的 Universal Providers 將
SessionStateProvider 與 SQL Database 相連,以達到在不同虛擬機器間共享工作狀態資
料的功能。首先,我們必須要先準備好 SQL Database,請到 Windows Azure 管理介面
內,新增一個 SQL Database 命名為 SessionStateLab,以作為實驗所需的資料庫。請在
Windows Azure 管理介面的新增功能表中指向【資料服務->SQL Database->快速建立】
來同時建立資料庫與伺服器,如下圖所示。
1. 請進入練習一所建立的 MVC Web 應用程式,在參考資料夾上按右鍵,選擇管理
NuGet 套件,並在管理 NuGet 套件對話盒中,於搜尋文字方塊中輸入 ASP.NET
Universal Provider,NuGet 會列出符合條件的套件,請安裝 Microsoft ASP.NET
Universal Providers Core。
NOTE:
本練習所用到的 Microsoft ASP.NET Universal Provider Core 是 ASP.NET 4.0 發表的
Universal Provider 的更新版,內含 Entity Framework 5.0,因此具備 Code First 的資料存
取能力,不需要先準備資料庫結構 (schema) 即可享有於資料庫間共用 Session 資料的能
力。
2. 請打開 Web.config,加入 Session State 的設定如下:
<sessionState mode="Custom"
customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider"
type="System.Web.Providers.DefaultSessionStateProvider,
System.Web.Providers, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"
connectionStringName="DefaultConnection" />
</providers>
</sessionState>
NOTE:
上述的指令可到這個網站取得:http://tinyurl.com/blguwgh
3. 修改 Web.config,將預設的資料庫連線 (DefaultConnection) 的字串設定為連接到
SQL Database 的連線字串:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data
Source=[servername].database.windows.net;Initial
Catalog=SessionStateLab;User
ID=[username]@[servername];Password=[password];Encrypt=true;Trus
ted_Connection=false;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
NOTE:
[servername] 為 SQL Database 的伺服器名稱,[username] 為 SQL Database 的帳戶名
稱,[password] 為 SQL Database 帳戶密碼。
4. 請打開 ControllersHomeController.cs,加入 Windows Azure Service Runtime 的命名
空間參考:
using Microsoft.WindowsAzure.ServiceRuntime;
5. 於 HomeController.cs 中,修改原有的 Index 方法,加入下列程式:
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your
ASP.NET MVC application.";
if (Session["myData"] == null)
Session["myData"] = (new Random()).Next(100000, 999999);
TempData["view"] = Session["myData"];
TempData["role"] = RoleEnvironment.CurrentRoleInstance.Id;
return View();
}
6. 於 ViewsHome 中找到 Index.cshtml,打開它,並加入下列指令:
@section featured {
<section class="featured">
...
</section>
}
<h3>Session Information:</h3>
<ul>
<li>myData: @TempData["view"].ToString()</li>
<li>RoleName: @TempData["role"].ToString()</li>
</ul>
<h3>We suggest the following:</h3>
<ol class="round">
<li class="one">
...
7. 請在雲端專案的角色資料夾中在 Web Role 專案上按右鍵,並在組態設定中將執行個
體計數設定在 2 (大於 2 也可以,最少要兩個)。
8. 完成後請建置此專案,並參考練習一來發行這個專案,當發行完成後,請用瀏覽器打
開專案的 URL,請記下第一次產生的 myData 編號,然後重覆重新整理,您會發現
RoleName 會變化,但 Session 的 myData 不會變化,表示 Session 已經成功在兩個
(或以上) 的執行個體中共享。
相關的 Windows Azure Training Kit 參考實作
簡報檔:
 PRESENTATION-WindowsAzureOverview
 PRESENTATION-WindowsAzureCloudServices
 PRESENTATION-CloudServiceLifecycle
手作實驗:
 HOL-IntroToCloudServices
 HOL-AdvancedWebWorkerRoles
 HOL-MigratingAspNetApps-VS2012
 HOL-DebuggingCloudServices-VS2012
 HOL-DeployingCloudServices-VS2012

More Related Content

What's hot

twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
twMVC
 
Spring boot 简介
Spring boot 简介Spring boot 简介
Spring boot 简介
宇帆 盛
 
React JS
React JSReact JS
React JS
漢麟 王
 
專案分層架構 twMVC#18
專案分層架構 twMVC#18專案分層架構 twMVC#18
專案分層架構 twMVC#18
twMVC
 
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)ASP.NET 5 快速入門 (Getting Started ASP.NET 5)
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)
Jeff Chu
 
twMVC#19 | opserver監控服務的解決
twMVC#19 | opserver監控服務的解決twMVC#19 | opserver監控服務的解決
twMVC#19 | opserver監控服務的解決
twMVC
 
DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略
Will Huang
 
twMVC#29 | 當.Net Core 遇到AWS Lambda
twMVC#29 | 當.Net Core 遇到AWS LambdatwMVC#29 | 當.Net Core 遇到AWS Lambda
twMVC#29 | 當.Net Core 遇到AWS Lambda
twMVC
 
動手打造 application framework-twMVC#15
動手打造 application framework-twMVC#15動手打造 application framework-twMVC#15
動手打造 application framework-twMVC#15
twMVC
 
使用 ASP.NET 5 實戰開發雲端應用程式
使用 ASP.NET 5 實戰開發雲端應用程式使用 ASP.NET 5 實戰開發雲端應用程式
使用 ASP.NET 5 實戰開發雲端應用程式
Will Huang
 
Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹
Alan Tsai
 
Angular js twmvc#17
Angular js twmvc#17Angular js twmvc#17
Angular js twmvc#17
twMVC
 
项目自动化实施
项目自动化实施项目自动化实施
项目自动化实施
dhlzj
 
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
Will Huang
 
项目开发实践No.1
项目开发实践No.1项目开发实践No.1
项目开发实践No.1pepsixp
 
React基礎教學
React基礎教學React基礎教學
React基礎教學
昇倫 蔡
 
ASP.NET MVC 5 新功能探索
ASP.NET MVC 5 新功能探索ASP.NET MVC 5 新功能探索
ASP.NET MVC 5 新功能探索
Will Huang
 
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
Kuo-Chun Su
 
在雲端上啜飲爪哇
在雲端上啜飲爪哇在雲端上啜飲爪哇
在雲端上啜飲爪哇
建興 王
 

What's hot (20)

行動技術開發概論
行動技術開發概論行動技術開發概論
行動技術開發概論
 
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
 
Spring boot 简介
Spring boot 简介Spring boot 简介
Spring boot 简介
 
React JS
React JSReact JS
React JS
 
專案分層架構 twMVC#18
專案分層架構 twMVC#18專案分層架構 twMVC#18
專案分層架構 twMVC#18
 
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)ASP.NET 5 快速入門 (Getting Started ASP.NET 5)
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)
 
twMVC#19 | opserver監控服務的解決
twMVC#19 | opserver監控服務的解決twMVC#19 | opserver監控服務的解決
twMVC#19 | opserver監控服務的解決
 
DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略
 
twMVC#29 | 當.Net Core 遇到AWS Lambda
twMVC#29 | 當.Net Core 遇到AWS LambdatwMVC#29 | 當.Net Core 遇到AWS Lambda
twMVC#29 | 當.Net Core 遇到AWS Lambda
 
動手打造 application framework-twMVC#15
動手打造 application framework-twMVC#15動手打造 application framework-twMVC#15
動手打造 application framework-twMVC#15
 
使用 ASP.NET 5 實戰開發雲端應用程式
使用 ASP.NET 5 實戰開發雲端應用程式使用 ASP.NET 5 實戰開發雲端應用程式
使用 ASP.NET 5 實戰開發雲端應用程式
 
Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹
 
Angular js twmvc#17
Angular js twmvc#17Angular js twmvc#17
Angular js twmvc#17
 
项目自动化实施
项目自动化实施项目自动化实施
项目自动化实施
 
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
 
项目开发实践No.1
项目开发实践No.1项目开发实践No.1
项目开发实践No.1
 
React基礎教學
React基礎教學React基礎教學
React基礎教學
 
ASP.NET MVC 5 新功能探索
ASP.NET MVC 5 新功能探索ASP.NET MVC 5 新功能探索
ASP.NET MVC 5 新功能探索
 
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
 
在雲端上啜飲爪哇
在雲端上啜飲爪哇在雲端上啜飲爪哇
在雲端上啜飲爪哇
 

Viewers also liked

領域驅動設計
領域驅動設計領域驅動設計
領域驅動設計
Pei-Hsun Kao
 
Windows 與 Azure 的容器旅程 @ Skilltree Day
Windows 與 Azure 的容器旅程 @ Skilltree DayWindows 與 Azure 的容器旅程 @ Skilltree Day
Windows 與 Azure 的容器旅程 @ Skilltree Day
Jeff Chu
 
Machine Learning, Big Data, Insights
Machine Learning, Big Data, InsightsMachine Learning, Big Data, Insights
Machine Learning, Big Data, Insights
Jeff Chu
 
Connect() Mini 2016
Connect() Mini 2016Connect() Mini 2016
Connect() Mini 2016
Jeff Chu
 
例外處理實務
例外處理實務例外處理實務
例外處理實務
Jeff Chu
 
物件導向設計原則:SOLID + DI
物件導向設計原則:SOLID + DI物件導向設計原則:SOLID + DI
物件導向設計原則:SOLID + DI
Jeff Chu
 
領域驅動設計 (Domain Driven Design)
領域驅動設計 (Domain Driven Design)領域驅動設計 (Domain Driven Design)
領域驅動設計 (Domain Driven Design)
Jeff Chu
 
GWAB 2014 Taipei: 由本地移轉至雲端 - 構思與實踐
GWAB 2014 Taipei: 由本地移轉至雲端 - 構思與實踐GWAB 2014 Taipei: 由本地移轉至雲端 - 構思與實踐
GWAB 2014 Taipei: 由本地移轉至雲端 - 構思與實踐Jeff Chu
 

Viewers also liked (8)

領域驅動設計
領域驅動設計領域驅動設計
領域驅動設計
 
Windows 與 Azure 的容器旅程 @ Skilltree Day
Windows 與 Azure 的容器旅程 @ Skilltree DayWindows 與 Azure 的容器旅程 @ Skilltree Day
Windows 與 Azure 的容器旅程 @ Skilltree Day
 
Machine Learning, Big Data, Insights
Machine Learning, Big Data, InsightsMachine Learning, Big Data, Insights
Machine Learning, Big Data, Insights
 
Connect() Mini 2016
Connect() Mini 2016Connect() Mini 2016
Connect() Mini 2016
 
例外處理實務
例外處理實務例外處理實務
例外處理實務
 
物件導向設計原則:SOLID + DI
物件導向設計原則:SOLID + DI物件導向設計原則:SOLID + DI
物件導向設計原則:SOLID + DI
 
領域驅動設計 (Domain Driven Design)
領域驅動設計 (Domain Driven Design)領域驅動設計 (Domain Driven Design)
領域驅動設計 (Domain Driven Design)
 
GWAB 2014 Taipei: 由本地移轉至雲端 - 構思與實踐
GWAB 2014 Taipei: 由本地移轉至雲端 - 構思與實踐GWAB 2014 Taipei: 由本地移轉至雲端 - 構思與實踐
GWAB 2014 Taipei: 由本地移轉至雲端 - 構思與實踐
 

Similar to Lab01 cloud project

Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Wade Huang
 
twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有
twMVC
 
Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2
twMVC
 
twMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure Platform
twMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure PlatformtwMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure Platform
twMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure Platform
twMVC
 
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC
 
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
twMVC
 
ASP.Net MVC2 简介
ASP.Net MVC2 简介ASP.Net MVC2 简介
ASP.Net MVC2 简介Allen Lsy
 
P6 服务器安装与配置
P6 服务器安装与配置P6 服务器安装与配置
P6 服务器安装与配置epst
 
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天Gelis Wu
 
建站大业,实战ASP.NET 4
建站大业,实战ASP.NET 4建站大业,实战ASP.NET 4
建站大业,实战ASP.NET 4Cat Chen
 
輕鬆上手Asp.net web api 2.1-twMVC#14
輕鬆上手Asp.net web api 2.1-twMVC#14輕鬆上手Asp.net web api 2.1-twMVC#14
輕鬆上手Asp.net web api 2.1-twMVC#14
twMVC
 
twMVC#14 | 輕鬆上手ASP.NET Web API 2
twMVC#14 | 輕鬆上手ASP.NET Web API 2twMVC#14 | 輕鬆上手ASP.NET Web API 2
twMVC#14 | 輕鬆上手ASP.NET Web API 2
twMVC
 
前端框架發展
 前端框架發展 前端框架發展
前端框架發展
Chi-wen Sun
 
打開窗,讓大象跨進來 - Microsoft HDInsight
打開窗,讓大象跨進來 - Microsoft HDInsight打開窗,讓大象跨進來 - Microsoft HDInsight
打開窗,讓大象跨進來 - Microsoft HDInsight
Kuo-Chun Su
 
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)Jeff Chu
 
ASP.NET Core 2.1設計新思維與新發展
ASP.NET  Core 2.1設計新思維與新發展ASP.NET  Core 2.1設計新思維與新發展
ASP.NET Core 2.1設計新思維與新發展
江華 奚
 
P6 Primavera 客户端的安装和配置
P6 Primavera 客户端的安装和配置P6 Primavera 客户端的安装和配置
P6 Primavera 客户端的安装和配置epst
 
與 Asp.net mvc 的第一次親密接觸 - twMVC#1
與 Asp.net mvc 的第一次親密接觸 - twMVC#1與 Asp.net mvc 的第一次親密接觸 - twMVC#1
與 Asp.net mvc 的第一次親密接觸 - twMVC#1
twMVC
 
zhuwenlongChinese
zhuwenlongChinesezhuwenlongChinese
zhuwenlongChineseWenlong Zhu
 

Similar to Lab01 cloud project (20)

Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有
 
twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有
 
Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2
 
twMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure Platform
twMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure PlatformtwMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure Platform
twMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure Platform
 
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
 
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
 
ASP.Net MVC2 简介
ASP.Net MVC2 简介ASP.Net MVC2 简介
ASP.Net MVC2 简介
 
P6 服务器安装与配置
P6 服务器安装与配置P6 服务器安装与配置
P6 服务器安装与配置
 
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
 
建站大业,实战ASP.NET 4
建站大业,实战ASP.NET 4建站大业,实战ASP.NET 4
建站大业,实战ASP.NET 4
 
J S教材
J S教材J S教材
J S教材
 
輕鬆上手Asp.net web api 2.1-twMVC#14
輕鬆上手Asp.net web api 2.1-twMVC#14輕鬆上手Asp.net web api 2.1-twMVC#14
輕鬆上手Asp.net web api 2.1-twMVC#14
 
twMVC#14 | 輕鬆上手ASP.NET Web API 2
twMVC#14 | 輕鬆上手ASP.NET Web API 2twMVC#14 | 輕鬆上手ASP.NET Web API 2
twMVC#14 | 輕鬆上手ASP.NET Web API 2
 
前端框架發展
 前端框架發展 前端框架發展
前端框架發展
 
打開窗,讓大象跨進來 - Microsoft HDInsight
打開窗,讓大象跨進來 - Microsoft HDInsight打開窗,讓大象跨進來 - Microsoft HDInsight
打開窗,讓大象跨進來 - Microsoft HDInsight
 
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)
 
ASP.NET Core 2.1設計新思維與新發展
ASP.NET  Core 2.1設計新思維與新發展ASP.NET  Core 2.1設計新思維與新發展
ASP.NET Core 2.1設計新思維與新發展
 
P6 Primavera 客户端的安装和配置
P6 Primavera 客户端的安装和配置P6 Primavera 客户端的安装和配置
P6 Primavera 客户端的安装和配置
 
與 Asp.net mvc 的第一次親密接觸 - twMVC#1
與 Asp.net mvc 的第一次親密接觸 - twMVC#1與 Asp.net mvc 的第一次親密接觸 - twMVC#1
與 Asp.net mvc 的第一次親密接觸 - twMVC#1
 
zhuwenlongChinese
zhuwenlongChinesezhuwenlongChinese
zhuwenlongChinese
 

More from Jeff Chu

.NET Conf 2019 高雄場 - .NET Core 3.0
.NET Conf 2019 高雄場 - .NET Core 3.0.NET Conf 2019 高雄場 - .NET Core 3.0
.NET Conf 2019 高雄場 - .NET Core 3.0
Jeff Chu
 
Developing Serverless application with Microsoft Azure and Cognitive Services
Developing Serverless application with Microsoft Azure and Cognitive ServicesDeveloping Serverless application with Microsoft Azure and Cognitive Services
Developing Serverless application with Microsoft Azure and Cognitive Services
Jeff Chu
 
.NET Innovations and Improvements
.NET Innovations and Improvements.NET Innovations and Improvements
.NET Innovations and Improvements
Jeff Chu
 
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)
Jeff Chu
 
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)
Jeff Chu
 
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)
Jeff Chu
 
Developing serverless applications with azure functions
Developing serverless applications with azure functionsDeveloping serverless applications with azure functions
Developing serverless applications with azure functions
Jeff Chu
 
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...
Jeff Chu
 
Cloud computing for manufacturing
Cloud computing for manufacturingCloud computing for manufacturing
Cloud computing for manufacturing
Jeff Chu
 
Microsoft Azure 新功能導覽 @ Build 2014
Microsoft Azure 新功能導覽 @ Build 2014Microsoft Azure 新功能導覽 @ Build 2014
Microsoft Azure 新功能導覽 @ Build 2014
Jeff Chu
 
Microsoft Azure 概觀 (2014-4-2 雲端達人班)
Microsoft Azure 概觀 (2014-4-2 雲端達人班)Microsoft Azure 概觀 (2014-4-2 雲端達人班)
Microsoft Azure 概觀 (2014-4-2 雲端達人班)Jeff Chu
 
從雲端看資訊科技的未來 (2014-03-26 國立嘉義大學資管系週會演講)
從雲端看資訊科技的未來 (2014-03-26 國立嘉義大學資管系週會演講)從雲端看資訊科技的未來 (2014-03-26 國立嘉義大學資管系週會演講)
從雲端看資訊科技的未來 (2014-03-26 國立嘉義大學資管系週會演講)
Jeff Chu
 
雲端的架構開發 (Designing Cloud Application Architecture with Windows Azure Platform)
雲端的架構開發 (Designing Cloud Application Architecture with Windows Azure Platform)雲端的架構開發 (Designing Cloud Application Architecture with Windows Azure Platform)
雲端的架構開發 (Designing Cloud Application Architecture with Windows Azure Platform)Jeff Chu
 
Windows Azure Application Architecture
Windows Azure Application ArchitectureWindows Azure Application Architecture
Windows Azure Application ArchitectureJeff Chu
 
Windows Azure Overview 20131128
Windows Azure Overview 20131128Windows Azure Overview 20131128
Windows Azure Overview 20131128Jeff Chu
 
Introduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 FeaturesIntroduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 FeaturesJeff Chu
 
Windows Azure Virtual Machine Services for Developers
Windows Azure Virtual Machine Services for DevelopersWindows Azure Virtual Machine Services for Developers
Windows Azure Virtual Machine Services for DevelopersJeff Chu
 
KSDG 4th event: Windows Azure Session
KSDG 4th event: Windows Azure SessionKSDG 4th event: Windows Azure Session
KSDG 4th event: Windows Azure SessionJeff Chu
 
MCSD and 70-487 Exam Blueprint
MCSD and 70-487 Exam BlueprintMCSD and 70-487 Exam Blueprint
MCSD and 70-487 Exam BlueprintJeff Chu
 

More from Jeff Chu (19)

.NET Conf 2019 高雄場 - .NET Core 3.0
.NET Conf 2019 高雄場 - .NET Core 3.0.NET Conf 2019 高雄場 - .NET Core 3.0
.NET Conf 2019 高雄場 - .NET Core 3.0
 
Developing Serverless application with Microsoft Azure and Cognitive Services
Developing Serverless application with Microsoft Azure and Cognitive ServicesDeveloping Serverless application with Microsoft Azure and Cognitive Services
Developing Serverless application with Microsoft Azure and Cognitive Services
 
.NET Innovations and Improvements
.NET Innovations and Improvements.NET Innovations and Improvements
.NET Innovations and Improvements
 
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)
 
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)
 
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)
 
Developing serverless applications with azure functions
Developing serverless applications with azure functionsDeveloping serverless applications with azure functions
Developing serverless applications with azure functions
 
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...
 
Cloud computing for manufacturing
Cloud computing for manufacturingCloud computing for manufacturing
Cloud computing for manufacturing
 
Microsoft Azure 新功能導覽 @ Build 2014
Microsoft Azure 新功能導覽 @ Build 2014Microsoft Azure 新功能導覽 @ Build 2014
Microsoft Azure 新功能導覽 @ Build 2014
 
Microsoft Azure 概觀 (2014-4-2 雲端達人班)
Microsoft Azure 概觀 (2014-4-2 雲端達人班)Microsoft Azure 概觀 (2014-4-2 雲端達人班)
Microsoft Azure 概觀 (2014-4-2 雲端達人班)
 
從雲端看資訊科技的未來 (2014-03-26 國立嘉義大學資管系週會演講)
從雲端看資訊科技的未來 (2014-03-26 國立嘉義大學資管系週會演講)從雲端看資訊科技的未來 (2014-03-26 國立嘉義大學資管系週會演講)
從雲端看資訊科技的未來 (2014-03-26 國立嘉義大學資管系週會演講)
 
雲端的架構開發 (Designing Cloud Application Architecture with Windows Azure Platform)
雲端的架構開發 (Designing Cloud Application Architecture with Windows Azure Platform)雲端的架構開發 (Designing Cloud Application Architecture with Windows Azure Platform)
雲端的架構開發 (Designing Cloud Application Architecture with Windows Azure Platform)
 
Windows Azure Application Architecture
Windows Azure Application ArchitectureWindows Azure Application Architecture
Windows Azure Application Architecture
 
Windows Azure Overview 20131128
Windows Azure Overview 20131128Windows Azure Overview 20131128
Windows Azure Overview 20131128
 
Introduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 FeaturesIntroduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 Features
 
Windows Azure Virtual Machine Services for Developers
Windows Azure Virtual Machine Services for DevelopersWindows Azure Virtual Machine Services for Developers
Windows Azure Virtual Machine Services for Developers
 
KSDG 4th event: Windows Azure Session
KSDG 4th event: Windows Azure SessionKSDG 4th event: Windows Azure Session
KSDG 4th event: Windows Azure Session
 
MCSD and 70-487 Exam Blueprint
MCSD and 70-487 Exam BlueprintMCSD and 70-487 Exam Blueprint
MCSD and 70-487 Exam Blueprint
 

Lab01 cloud project

  • 1. GWAB Lab-雲端專案 本手作實驗 (Hands-on Lab) 為 GWAB 2013 Taiwan 的手作實驗之一, 本手作實驗將會建 立兩個雲端專案,一個是 Web Role,用於展示將 Web 應用程式掛上 Windows Azure 的 流程;另一個是 Worker Role,用以展示將 ASP.NET Web API Self-hosting 服務掛載到 Windows Azure 以供應雲端上的服務,最後會使用 SQL Database 在兩個不同的執行個體 之間維護 Session 工作狀態。 本實作的學習目標 當實作完成時,學員應該會:  使用 Visual Studio 2012 建立雲端專案。  部署 ASP.NET MVC 4.0 Web 應用程式到雲端。  部署 ASP.NET Web API 服務到雲端。  在不同執行個體之間共享 Session 工作階段資料。 本實作的前置需求 1. Visual Studio 2012 或是 Visual Studio 2012 Express for Web。 2. Windows Azure SDK for .NET 與 Visual Studio Tools for Windows Azure v1.8 3. SQL Server 2012 或 SQL Server Express 2012 或 SQL Server Express 2012 LocalDB 4. 有效的 Windows Azure 訂閱或申請 90 天試用訂閱。 本實作的估計時間
  • 2. 大約 45-60 分鐘 實作的步驟 練習 1:建立與部署雲端專案 練習 2:建立與部署雲端上的 Web API 服務 練習 3:在執行個體之間共用 Session 工作狀態資料 練習 1. 建立與部署雲端專案 1. 請開啟 Visual Studio 2012,並選擇【檔案 -> 新增 -> 專案】,這時應會看到新增專 案的對話盒,請選擇 Visual C# -> Cloud -> Windows Azure 雲端服務。 2. 將專案名稱命名為 FirstCloudService,並按確定,此時會出現新增 Windows Azure 雲 端服務的對話盒,請選擇 ASP.NET MVC 4 Web 角色,並按中間的 “>” 將專案加到方 案內,然後按確定。
  • 3. 3. 在 MVC 專案類型對話盒出現時,請選擇網際網路應用程式專案,然後按確定。Visual Studio 會將雲端專案與 MVC 應用程式專案產生出來。 4. 在 Visual Studio 完成專案建置後,請按 F5 來執行專案,當應用程式啟動完成時,您 會看到下列畫面。
  • 4. 5. 請將除錯器停止後,在 Visual Studio 2012 的雲端專案上按下右鍵,並選擇發行。 6. 此時發行 Windows Azure 應用程式精靈會出現,請選擇『登入以下載憑證』,進入 Windows Azure 管理介面1 ,這時會自動下載一份發行設定檔。 1 會需要登入。
  • 5.
  • 6. 7. 請回到 Windows Azure 發行精靈,按下匯入按鈕,並選擇前一步下載的發行檔案,並 按開啟,即可在訂用帳戶中看到自己的訂閱。 8. 確認要使用的訂閱後,按下一步,然後在雲端服務的下拉選單中選擇建立新項目。
  • 7. 9. 在建立 Windows Azure 服務對話盒出現時,於名稱給定一個獨一無二的名稱,位置請 選擇 East Asia (東亞),然後按確定。 若名稱重覆時會出現錯誤訊息: NOTE: 您也可以先在 Windows Azure 管理介面中建立新的雲端服務後,再開啟 Windows Azure 發行精靈,選擇已建立的雲端服務。 10. 請選擇進階設定頁籤,並在儲存體帳戶的下拉選單中選擇建立新項目,並給予一個獨 一無二的名稱,位置一樣選擇 East Asia,然後按確定。完成這個步驟之後,按下一 步。
  • 8. NOTE: 在這裡所建立的儲存帳戶是 Visual Studio 為了要暫存發行檔案所需要的。 11. 於發行摘要頁面中,確認發行的組態後,按下發行按鈕。
  • 9. 12. Visual Studio 會開始發行雲端服務專案,並且開啟 Windows Azure 活動記錄檔視窗, 您可以在這裡看到部署的進度。 13. 當 Visual Studio 發行完成後,網站 URL 即可使用,請直接按下該連結,會自動開啟 瀏覽器,您應該會看到網站已正常的執行在 Windows Azure 環境。
  • 10. NOTE: 雲端服務的 URL 是[cloudservicename].cloudapp.net,您可以使用 DNS CNAME 來設定成 您想要的 DNS 名稱。
  • 11. 練習 2. 建立與部署雲端上的 Web API 服務 1. 在前一個練習中所建立的雲端專案上的角色資料夾按右鍵,選擇加入 > 新增背景工作 角色專案,然後在加入新的.NET Framework 4.5 角色專案中,選擇背景工作角色,將 專案命名為 WebApiService,然後按確定。 2. 在 Visual Studio 完成加入 Worker Role 到專案的工作後,請到 WebApiService 專案 上,於參考資料夾上按右鍵,選擇管理 NuGet 套件:
  • 12. 3. 當管理 NuGet 套件對話盒出現時,請確認左側是選擇線上,然後在搜尋的對話中輸入 Web API Self Host,NuGet 會搜尋目前可用的套件,請確認有找到 Microsoft ASP.NET Web API Self Host,然後按安裝。當授權合約出現時,請選擇我接受,即可 繼續進行安裝。當完成安裝後,請關閉此對話盒。
  • 13. 4. 請於參考資料夾上按右鍵,選擇加入參考,並在參考管理員中,於組件>架構選擇 System.Net.Http 組件後,按確定。 5. 請在 WebApiService 內加入一個新的類別,名稱為 ValuesController,然後在 ValuesController.cs 中加入下列程式碼,完成後將它儲存: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Http; using System.Web.Http.Controllers; namespace WebApiService
  • 14. { public class ValuesController : ApiController { public IEnumerable<string> Get() { return new string[] { "Value1", "Value2" }; } } } 6. 打開 WorkerRole.cs 檔案,在命名空間宣告中加入下列宣告: using System.Web.Http; using System.Web.Http.SelfHost; 7. 於 WorkerRole.cs 檔案中,宣告下列類別層次的變數: public class WorkerRole : RoleEntryPoint { private HttpSelfHostServer _httpServer = null; private HttpSelfHostConfiguration _httpConfiguration = null; ... } 8. 請於 WorkerRole.cs 檔案中的 OnStart 方法中,加入下列紅字部份的程式碼: public override bool OnStart() { ServicePointManager.DefaultConnectionLimit = 12; this._httpConfiguration = new HttpSelfHostConfiguration("http://[cloudservicename].cloudapp.ne t:8080"); this._httpConfiguration.Routes.MapHttpRoute( name: "ValueApi", routeTemplate: "api/{controller}", defaults: new { controller = new ValuesController() }); this._httpServer = new HttpSelfHostServer(this._httpConfiguration); return base.OnStart(); }
  • 15. NOTE: [cloudservicename]即是您於前面所建置的雲端服務名稱。 9. 請到 OnRun 方法中,加入下列紅字部份的程式碼: public override void Run() { this._httpServer.OpenAsync().Wait(); while (true) { Thread.Sleep(10000); } } 10. 請在 WorkerRole 類別中,加入一個新的 OnStop 覆寫方法: public override void OnStop() { this._httpServer.CloseAsync().Wait(); base.OnStop(); } 11. 請在雲端專案內的角色資料夾中,找到 WebApiService 並於其上按右鍵,以打開雲端 專案屬性:
  • 16. 12. 於專案屬性視窗中,於左邊選擇端點,並加入一個新的端點,類型為 Input,通訊協 定為 Http,公用連接埠為 8080,私人連接埠也是 8080。 NOTE: 公用連接埠因為前一個練習所建立的 Web Role 佔用了 Port 80,因此這裡不能使用 Port 80,所以改用 8080。私人連接埠則要和服務所用的連接埠對應,由於程式中使用的是 8080,因此私人連接埠也要使用 8080,否則會連接不上。 13. 請打開雲端專案中的 ServiceDefinition.csdef 檔案,找到 WorkerRole 的設定,並加入 要求執行期必須得到核准權限的設定,否則會在初始化時發生死亡迴圈 (dead loop) 問題,因為 Self-hosting 無法取得正確的權限以附掛通訊埠。 <?xml version="1.0" encoding="utf-8"?> <ServiceDefinition name="FirstCloudService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/Servi ceDefinition" schemaVersion="2012-10.1.8"> <WebRole name="MvcWebRole1" vmsize="ExtraSmall"> ... </WebRole> <WorkerRole name="WebApiService" vmsize="ExtraSmall"> <Runtime executionContext="elevated" /> ... </WorkerRole> </ServiceDefinition>
  • 17. 14. 請依練習一的作法進行專案的發行,當發行完成後,請使用瀏覽器瀏覽 http://[cloudservicename].cloudapp.net/api/values,應該可以看到下圖的畫面,表示 服務己經成功運作。 NOTE: [cloudservicename]即是您於前面所建置的雲端服務名稱。 練習 3. 在執行個體之間共用 Session 工作狀態資料 本練習將會使用 SQL Database 結合 ASP.NET 4.0 新增的 Universal Providers 將 SessionStateProvider 與 SQL Database 相連,以達到在不同虛擬機器間共享工作狀態資 料的功能。首先,我們必須要先準備好 SQL Database,請到 Windows Azure 管理介面 內,新增一個 SQL Database 命名為 SessionStateLab,以作為實驗所需的資料庫。請在 Windows Azure 管理介面的新增功能表中指向【資料服務->SQL Database->快速建立】 來同時建立資料庫與伺服器,如下圖所示。
  • 18. 1. 請進入練習一所建立的 MVC Web 應用程式,在參考資料夾上按右鍵,選擇管理 NuGet 套件,並在管理 NuGet 套件對話盒中,於搜尋文字方塊中輸入 ASP.NET Universal Provider,NuGet 會列出符合條件的套件,請安裝 Microsoft ASP.NET Universal Providers Core。
  • 19. NOTE: 本練習所用到的 Microsoft ASP.NET Universal Provider Core 是 ASP.NET 4.0 發表的 Universal Provider 的更新版,內含 Entity Framework 5.0,因此具備 Code First 的資料存 取能力,不需要先準備資料庫結構 (schema) 即可享有於資料庫間共用 Session 資料的能 力。 2. 請打開 Web.config,加入 Session State 的設定如下: <sessionState mode="Custom" customProvider="DefaultSessionProvider"> <providers> <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" /> </providers> </sessionState> NOTE: 上述的指令可到這個網站取得:http://tinyurl.com/blguwgh 3. 修改 Web.config,將預設的資料庫連線 (DefaultConnection) 的字串設定為連接到 SQL Database 的連線字串: <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=[servername].database.windows.net;Initial Catalog=SessionStateLab;User ID=[username]@[servername];Password=[password];Encrypt=true;Trus ted_Connection=false;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> </connectionStrings>
  • 20. NOTE: [servername] 為 SQL Database 的伺服器名稱,[username] 為 SQL Database 的帳戶名 稱,[password] 為 SQL Database 帳戶密碼。 4. 請打開 ControllersHomeController.cs,加入 Windows Azure Service Runtime 的命名 空間參考: using Microsoft.WindowsAzure.ServiceRuntime; 5. 於 HomeController.cs 中,修改原有的 Index 方法,加入下列程式: public ActionResult Index() { ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application."; if (Session["myData"] == null) Session["myData"] = (new Random()).Next(100000, 999999); TempData["view"] = Session["myData"]; TempData["role"] = RoleEnvironment.CurrentRoleInstance.Id; return View(); } 6. 於 ViewsHome 中找到 Index.cshtml,打開它,並加入下列指令: @section featured { <section class="featured"> ... </section> } <h3>Session Information:</h3> <ul> <li>myData: @TempData["view"].ToString()</li> <li>RoleName: @TempData["role"].ToString()</li> </ul> <h3>We suggest the following:</h3>
  • 21. <ol class="round"> <li class="one"> ... 7. 請在雲端專案的角色資料夾中在 Web Role 專案上按右鍵,並在組態設定中將執行個 體計數設定在 2 (大於 2 也可以,最少要兩個)。 8. 完成後請建置此專案,並參考練習一來發行這個專案,當發行完成後,請用瀏覽器打 開專案的 URL,請記下第一次產生的 myData 編號,然後重覆重新整理,您會發現 RoleName 會變化,但 Session 的 myData 不會變化,表示 Session 已經成功在兩個 (或以上) 的執行個體中共享。
  • 22. 相關的 Windows Azure Training Kit 參考實作 簡報檔:  PRESENTATION-WindowsAzureOverview  PRESENTATION-WindowsAzureCloudServices  PRESENTATION-CloudServiceLifecycle 手作實驗:  HOL-IntroToCloudServices  HOL-AdvancedWebWorkerRoles  HOL-MigratingAspNetApps-VS2012  HOL-DebuggingCloudServices-VS2012  HOL-DeployingCloudServices-VS2012