37
Eric ShangKuan (上官林傑) Technical Evangelist Microsoft

透過 Windows Azure Mobile Services 開發各平台 Apps

Embed Size (px)

DESCRIPTION

f

Citation preview

Page 1: 透過 Windows Azure Mobile Services 開發各平台 Apps

Eric ShangKuan (上官林傑)

Technical Evangelist

Microsoft

Page 2: 透過 Windows Azure Mobile Services 開發各平台 Apps

Agenda

• Windows Azure Mobile Service 平台介紹

• 各平台 App 開發實作

• 深入剖析 WAMS

Page 3: 透過 Windows Azure Mobile Services 開發各平台 Apps

現在就開始http://aka.ms/AzureMSDNTW

Page 4: 透過 Windows Azure Mobile Services 開發各平台 Apps

Mobile Services 快速建立 app 的「後端」

資料儲存、身份驗證以及推送通知

REST API

目前官方提供Windows 8, Windows Phone

8 以及 iOS SDK

Page 5: 透過 Windows Azure Mobile Services 開發各平台 Apps

自訂 Script(s)<//>

Page 6: 透過 Windows Azure Mobile Services 開發各平台 Apps

為什麼要使用 Mobile Services?• 不必撰寫維護雲端程式

專心在 app 端開發

RESTful API 不限定單一 app 平台使用

• 提供 app 開發的基礎建設彈性擴充

資料儲存、身份驗證、推播通知

官方釋出主流平台 SDK

Page 7: 透過 Windows Azure Mobile Services 開發各平台 Apps

開發一個 App…

• 開發 Web 程式處理 app 的request

• 實作資料存取的方式

Page 8: 透過 Windows Azure Mobile Services 開發各平台 Apps

開發一個 App…

• 開發 Web 程式處理身份驗證的問題

• 存取資料時的身份驗證

Page 9: 透過 Windows Azure Mobile Services 開發各平台 Apps

開發一個 App…

不使用 WAMS

• 開發伺服器應用程式

• (server-side) 定義並實作 REST API,並且使用 JSON 資料格式

• (server-side) 實作 sql/nosql 處理不同的資料儲存需求

• (server-side) 實作各身份驗證機制

• (client-side) app 開發,並且直接介接 REST API

使用 WAMS

• 在 Windows Azure 後台建立一個新的WAMS 服務

• 下載欲開發平台 SDK

• (client-side) 使用 SDK 直接處理資料儲存、身份驗證、推播通知等功能

Page 10: 透過 Windows Azure Mobile Services 開發各平台 Apps

自己寫 Server-side 及 Client-side

// POST api/todopublic HttpResponseMessage PostTodo(Todo todo){

if (ModelState.IsValid){db.Todos.Add(todo);db.SaveChanges();

HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, todo);

response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id =

todo.TodoId }));return response;

}else{return

Request.CreateResponse(HttpStatusCode.BadRequest);}

}

public static async void CreateTodo(Todo todo){

string serviceUrl = "http://example.com/path/to/todo";

using (HttpClient client = new HttpClient()){DataContractJsonSerializer serializer = new

DataContractJsonSerializer(typeof(Todo));

using (MemoryStream stream = new MemoryStream()){

serializer.WriteObject(stream, todo);stream.Seek(0, SeekOrigin.Begin);

var json = new StreamReader(stream).ReadToEnd();

var response = await client.PostAsync(serviceUrl as string, new StringContent(json, Encoding.UTF8, "application/json"));

response.EnsureSuccessStatusCode();}

}}

Server-side 處理 POST Client-side 寫介接程式碼

Page 11: 透過 Windows Azure Mobile Services 開發各平台 Apps

使用 Windows Azure Mobile Services

public static MobileServiceClient service = new MobileServiceClient("https://你的WAMS.azure-mobile.net/","這裡放上你的 Key"

);

private IMobileServiceTable<Todo> table = service.GetTable<Todo>();

private async void InsertTodoItem(Todo todo){

await table.InsertAsync(todo);}

在管理後台設定 Client-side 使用 SDK 寫

Page 12: 透過 Windows Azure Mobile Services 開發各平台 Apps

診斷、記錄和擴充

診斷API 呼叫CPU 時間資料輸出

記錄console.error(…)console.log(…)

擴充 -運算向外擴充執行個體計數向上擴充 VM 大小

擴充 -儲存體將行動服務租用戶向外擴充到專用的 SQL DB

向上擴充 SQL 資料庫

Page 13: 透過 Windows Azure Mobile Services 開發各平台 Apps

預覽時期的免費額度

在共用執行個體上執行

10個行動服務 1GB SQL

資料庫

預覽期間無 SLA。只能使用小型執行個體。在預覽期間,可以 33% 的折扣從標準隨用隨付費率升級到保留執行個體。

從W

A 免

費試用

Page 14: 透過 Windows Azure Mobile Services 開發各平台 Apps

服務價格

預覽階段無 SLA

公開上市99.9%

共用執行個體(多租用戶環境)

只提供小型執行個體,可以購買多個執行個體

公開預覽上市時免費

最多 10 個應用程式

保留執行個體(私人 VM)

只提供小型執行個體,可以購買多個執行個體

預覽收費將會使用與網站相同的減價費率

最多 10 個應用程式

Page 15: 透過 Windows Azure Mobile Services 開發各平台 Apps
Page 16: 透過 Windows Azure Mobile Services 開發各平台 Apps

MobileService SDK for Windows 市集應用程式

使用 JavaScript使用 C#

• 專案中加入 Windows Azure Mobile

Services Managed Client 的參考• Microsoft.WindowsAzure.MobileSer

vices 的 namespace

• API 參考文件: http://msdn.microsoft.com/en-

us/library/windowsazure/jj589724.aspx

• 專案中加入 Windows Azure Mobile

Services JavaScript Client 的參考

• 引入/MobileServicesJavaScriptClient/Mob

ileServices.js 檔案

• API 參考文件: http://msdn.microsoft.com/en-us/library/windowsazure/jj554207.aspx

Page 17: 透過 Windows Azure Mobile Services 開發各平台 Apps

存取資料使用 JavaScript使用 C#

// 建立連接 Mobile Service 物件public MobileServiceClient MobileService = newMobileServiceClient(

"https://你的服務名稱.azure-mobile.net/","你服務的存取金鑰"

);

// 取得資料儲存的 table,並且指名是 TodoItem 的資料結果private IMobileServiceTable<TodoItem> todoTable = MobileService.GetTable<TodoItem>();

// 插入資料,並且符合 .net 4.5 的非同步程式撰寫模式await todoTable.InsertAsync(todoItem);

// 讀取資料,並且附上過濾條件items = todoTable

.Where(todoItem => todoItem.Complete == false)

.ToCollectionView();

// 建立 MobileService 的 client 物件var client = newMicrosoft.WindowsAzure.MobileServices.MobileServiceClient(

"https://你的服務名稱.azure-mobile.net/","你服務的存取金鑰"

);

// 取得儲存資料的 tablevar todoTable = client.getTable('TodoItem');

// 呼叫 insert 方法將 JavaScript 物件儲存到 table 中,// 會根據物件的欄位來存放資料。並且符合 Windows Store App // 中的非同步呼叫程式撰寫模式 可結合後續操作todoTable.insert(todoItem).done(function (item) {

// 完成呼叫後做的事});

// 讀取資料並且附上 query 的條件todoTable.where({ complete: false })

.read()

.done(function (results) {// 完成呼叫後做的事

});

Page 18: 透過 Windows Azure Mobile Services 開發各平台 Apps

身份驗證服務• 簡單完成 Microsoft Account, Facebook, Google 以及 twitter 的

身份驗證機制

• 提供 Callback URL 完成OAuth 2.0 的驗證流程

不必自行處理 Web Server

• 僅可取得各服務的 User Id (類似流水號),需要取得更詳細的內容或權限還是要自行實作各服務的API

可用來配合資料儲存作區別

Page 19: 透過 Windows Azure Mobile Services 開發各平台 Apps

身份驗證使用 JavaScript使用 C#

MobileServiceUser user;private async void Auth(){

while (user == null){

try{

user = await MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook);

// 登入或授權成功}catch (InvalidOperationException){

// 登入或授權失敗}

}}

var userId = null;var login = function () {return new WinJS.Promise(function (complete) {

// 呼叫 login 開始登入及授權流程client.login('facebook').done(function (results) {

userId = results.userId;// 登入且授權完成

}, function (errors) {// 登入但不授權

});});

};

var auth = function () {login().then(function () {

if (userId === null) {// 登入或授權失敗auth(); // 再試一次

}});

};

auth();

Ref: http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started-with-users-dotnet/

Page 20: 透過 Windows Azure Mobile Services 開發各平台 Apps

推播通知服務架構

Page 21: 透過 Windows Azure Mobile Services 開發各平台 Apps

使用 WAMS 來送推播通知

Page 22: 透過 Windows Azure Mobile Services 開發各平台 Apps

推播通知 (push notification)使用 JavaScript使用 C#

CurrentChannel = awaitPushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

var channel;var channelOperation = Windows.Networking.PushNotifications.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync()

.then(function (newChannel) {channel = newChannel;

// channel.uri 即可送給 mobile service 作為送推播通知的路徑}, function (error) { console.log(error.message); });

Ref: http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started-with-push-

dotnet/

Page 23: 透過 Windows Azure Mobile Services 開發各平台 Apps
Page 24: 透過 Windows Azure Mobile Services 開發各平台 Apps

在 Windows Phone 8 App 上新增資料與 Windows Store App 使用 C# 完全相同

Page 25: 透過 Windows Azure Mobile Services 開發各平台 Apps
Page 26: 透過 Windows Azure Mobile Services 開發各平台 Apps

REST API

Page 27: 透過 Windows Azure Mobile Services 開發各平台 Apps

REST API• 在官方尚未釋出該平台或語言的 SDK 時,可直接以呼叫

REST APIs 的形式來使用 WAMS 的服務

任何應用程式 ( web、desktop、…) 都可以運用• 提供彈性可自行撰寫資料匯入、匯出或是管理界面

• 參考官方文件以瞭解目前的支援:http://msdn.microsoft.com/library/windowsazure/jj710108.aspx

Page 28: 透過 Windows Azure Mobile Services 開發各平台 Apps

REST API 使用範例 –新增資料POST /tables/TodoItem HTTP/1.1

Host: example.azure-mobile.net

Accept: application/json

Content-Type: application/json

Content-Length: 16

X-ZUMO-APPLICATION: THE_APPLICATION_KEY

{"text":"hello"}

200 OK

Content-Type: application/json

Content-Length: 23

{"id":1,"text":"hello"}

Page 29: 透過 Windows Azure Mobile Services 開發各平台 Apps
Page 30: 透過 Windows Azure Mobile Services 開發各平台 Apps

Android 寫入資料範例URL url = new URL("https://你服務的名稱.azure-mobile.net/tables/表格名稱");

HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

urlConn.setDoInput(true);

urlConn.setDoOutput(true);

urlConn.setRequestMethod("POST");

urlConn.addRequestProperty("Content-Type", "application/json");

urlConn.addRequestProperty("ACCEPT", "application/json");

// 在 Request Header 填入服務金鑰

urlConn.addRequestProperty("X-ZUMO-APPLICATION", "你的服務金鑰");

DataOutputStream wr = new DataOutputStream(urlConn.getOutputStream());

// 寫入 JSON 格式的資料

wr.writeBytes("{\"text\":\"Hello\"}");

wr.flush();

wr.close();

Page 31: 透過 Windows Azure Mobile Services 開發各平台 Apps

Server Script

Page 32: 透過 Windows Azure Mobile Services 開發各平台 Apps

使用 Scripts 管理資料

• JavaScript-based (ECMAScript5)

• 客製資料在 insert, update, delete, read 時的程序

• 在管理後台上直接修改

• Windows Azure 提供模組及物件來完成複雜的操作

Page 33: 透過 Windows Azure Mobile Services 開發各平台 Apps

Server Script 使用情境• 在 CRUD 時要修改欄位,或是其它的特殊處理

• 完成 CRUD 操作後,修改回應的內容

• 自訂 SQL 查詢語法 (透過 mssql.*模組)

• 發送推播通知 (透過 push.wns.*模組)

• …

Page 34: 透過 Windows Azure Mobile Services 開發各平台 Apps
Page 35: 透過 Windows Azure Mobile Services 開發各平台 Apps

現在就開始http://aka.ms/AzureMSDNTW

Page 36: 透過 Windows Azure Mobile Services 開發各平台 Apps

https://www.windowsazure.com/en-us/develop/mobile/http://msdn.microsoft.com/en-us/library/windowsazure/jj554228.aspx

http://github.com/WindowsAzure-TrainingKithttp://github.com/WindowsAzure/azure-mobile-services

http://social.msdn.microsoft.com/Forums/zh-tw/category/windowsazuretw

Page 37: 透過 Windows Azure Mobile Services 開發各平台 Apps