introducing cloud architecturedownload.microsoft.com/download/c/6/0/c60e2bd0-8a7... ·...

58

Upload: others

Post on 16-Feb-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料
Page 3: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Windows Azure Mobile Services (WAMS)

• 架構於現有的PaaS上

• 將數種服務整合成單一服務

• 資料儲存: SQL Data Services

• 使用者驗證: Authentication Services

• 訊息推播: Push Notification Services

• 為

• 行動平台的App 開發而設計

• 提供

• 直覺的管理介面

• 具高延展性, 可因應未來使用者增加的情況

Page 4: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

連線型行動平台App的架構

Accessing data

authentication

Call web service

Page 5: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

如果… 要開發一個雲端待辦事項的應用程式..

• 使用者驗證

• 您需要儲存使用者的資料

• 您需要一個伺服器提供驗證的Web Service

• 儲存待辦事項的資料

• 您需要一個伺服器來儲存這些資料

• 當伺服器移除過期的資料時, 使用者必須收到通知

• 您需要一個在背景執行, 且定時刪除過期資料的程式

• 因此

• 您需要一個Web Server

• 提供使用者驗證

• 執行定期執行移除過期資料的程式

• 提供Web Service供行動裝置呼叫

• 您需要一個資料庫伺服器

• 儲存使用者資料

• 您需要寫網頁程式來提供服務

• ASP.NET

• PHP

• …..

Page 6: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

一言以蔽之,要很多伺服器

Page 8: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

這還只是網頁或是Web Service程式而已…..

我們還沒開始談App的開發也沒談到安全性的問題…….

Page 9: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

如果使用Windows Azure Mobile Service• 提供SQL Database Service

• 可以儲存資料

• 提供背景排程服務

• 您可以撰寫程式來定期移除過期資料

• 發送推播通知

• 提供整合性的使用者驗證

• 您可以透過Facebook, Microsoft Account, Google Account或 Twitter來驗證使用者

• 您可以由這些驗證服務提供者取得使用者資料

• 因此

• 您不需要準備一個Web Server

• 您不需要準備一個Database Server

• 您不需要撰寫驗證使用者的程式

• 您不需要寫太多的程式就可以完成訊息推播

• 只要

• 申請一個Windows Azure帳戶

• 建立一個Mobile Service

• 設定資料庫及驗證方式

• 下載平台(WP,Android,iOS)需要的函式庫

• 寫很少量的程式碼(Node.js)

• 接著只要專注於您的App開發就好了

Page 11: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

讓我們開始吧

建立 Windows Azure Mobile Service

DEMO

Page 12: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

建立資料表

Page 13: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

關於權限

Can use by any user

Need application key

Authenticated users

Internal use only

Page 14: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

設定整合驗證 – Google Account

• 您可以透過 Facebook, Microsoft Account, Google Account 或 Twitter來驗證使用者

Page 15: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

關於Google Account

• 您必須設定 Authorized Redirect URIs 與 Authorized JavaScript Origins 兩個欄位

Page 16: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

設定透過Google Account進行整合驗證

Page 18: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

建立Windows Phone 應用程式

• 透過NuGet Package Manager 加入 WAMS Client Library's

Page 19: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

取得存取Azure Mobile Services所需要的Application Key

Page 20: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

存取Mobile Service

public partial class App : Application {

internal static MobileServiceClient MobileService = new MobileServiceClient("https://todo64.azure-mobile.net/", "xisXQCpUGwQfwOwUqvxcqwkVhtdiLR72");

• 建立MobileServiceClient物件

Page 21: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

資料表與類別的對應

using Microsoft.WindowsAzure.MobileServices; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace TodoApplication{

[DataTable("todos")] public class TodoDataModel{

public int Id { get; set; } public string Description { get; set; }public bool Complete { get; set; }

} }

• Windows Azure Mobile Services 會自動依據您定義的類別來建立資料表結構

Page 22: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

添加處理使用者驗證的程式碼

public partial class MainPage : PhoneApplicationPage{

private MobileServiceUser user;

private async System.Threading.Tasks.Task Authenticate() {

while (user == null) {

try{

user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Google); }

catch (InvalidOperationException) {

Dispatcher.BeginInvoke(() => {

MessageBox.Show("You must log in. Login Required"); });

} }

}

private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) {

await Authenticate(); }

Page 23: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

如何取得使用者資訊?

• 當使用者登入後• 您可以由驗證提供者端(Facebook, Microsoft Account, Google Account ,

Twitter)取得使用者資訊• 您可以取得一個唯一的使用者編號 (由WAMS提供)

• 但• 除了由WAMS提供的使用者編號外, 其餘要由驗證提供者端取得的資訊, 無法透過Client Library‘s直接取得

• 如果需要, 您可以• 撰寫Server-Side Script來取得• 或是撰寫Custom API回傳給客戶端

Page 24: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Table Triggers

• 開發者可以自訂CRUD 的個別行為• 以Node.js語法撰寫, 未來會提供.NET Language(像是C#等等)

Page 25: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Storing User ID

• 我們需要分辨待辦項目是屬於哪個使用者的• 因此必須自訂Insert的行為, 新增時存入使用者ID

• 順便儲存建立此項目的日期

• createDate, userId 並未定義於先前的對應類別• WAMS會自動添加這些欄位, 而客戶端可以不知道這些欄位的存在

Page 26: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

UI的設計與資料的取得

• 此時並沒有任何資料, 所以先測試增加一筆資料, 方便後面的UI設計

• 取得資料表物件

• 新增資料

Page 27: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Test And Run

Page 28: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Designing UI and Get data

Todo Item record

ListBox

Page 29: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

取得資料

• 這行程式會回傳所有資料• 通常, 我們只希望回傳該使用者的資料• 透過自訂Read 可以達到這個需求

Page 30: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

實作 CUD

• 更新資料

• 刪除資料

• 查詢資料

Page 31: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

以Custom API實作查詢

• 雖然直接由客戶端進行查詢很酷• 但

• 在WAMS中, 有更具效率的做法

Page 32: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

以Custom API實作查詢

客戶端呼叫API的程式碼

Page 33: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

背景排程執行的工作及Push Notification

• 建立 channels 資料表• 大多數的 push notification services (WP, iOS, Android) 都是基於

channel的觀念• Push Notification Services會分配一個ID給裝置

• 因此我們需要一個可以儲存push uri的資料表

Page 34: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

建立新增 channel 資料的API

Page 35: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

建立排程

Page 36: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

定時執行的Script

Page 37: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Test And Run

Demo

Page 38: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

需要用到更多的SQL Server功能嗎?

Page 39: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Android Version

Page 40: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

您需要什麼?

• 下載client library's for android

• http://go.microsoft.com/fwlink/p/?linkid=280126&clcid=0x409

• 將所有檔案放到android project中 (libs/)

Page 41: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

撰寫程式

• 建立 mobile service client 物件

• 設計UI

_client = new MobileServiceClient("https://todo64.azure-mobile.net/", "WGoPUayxXeaWRsOiHrzGfcotvffFfM31", this);

Page 42: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

回傳資料_table.execute(new TableQueryCallback<Todo>() {

@Override public void onCompleted(List<Todo> result, int count,

Exception exception, ServiceFilterResponse response) { if(exception == null){

_adapter.clear(); for (Todo item : result) {

_adapter.add(item); }

} }

});

Page 43: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

實作CUD

• 新增

• 刪除

• 修改

_table.insert(item, new TableOperationCallback<Todo>());

_table.delete(item, new TableOperationCallback<Todo>());

_table.update(item, new TableOperationCallback<Todo>());

Page 44: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Integrate Google Cloud Message

• 下載 Google Cloud Message Library• 目前已併到Google Play Services API了

Page 45: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

呼叫CUSTOM API

ArrayList<Pair<String, String>> parameters = new ArrayList<Pair<String, String>>(); parameters.add(new Pair<String, String>("device", "android")); parameters.add(new Pair<String, String>("uri", regID));

_client.invokeApi("addchannel", "POST", parameters, new ApiJsonOperationCallback()

{

@Override public void onCompleted(JsonElement jsonObject,

Exception exception, ServiceFilterResponse response) { int i = 0; i = i++;

} });

Page 46: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Test And Run

Demo

Page 47: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

IOS

Page 48: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

What’s you need?

• 下載Client Library's

• https://go.microsoft.com/fwLink/p/?LinkID=266533

• 解開後將兩個目錄複製到iOS Project中

Page 49: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

撰寫程式

• 建立 mobile service client 物件

• 設計UI

//in .h @property (nonatomic, strong) MSClient *client; // in .m self.client = [MSClient clientWithApplicationURLString:@"https://todo64.azure-mobile.net/"applicationKey:@"<app key><your key="">"];

Page 50: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

回傳資料

self.table = [self.client tableWithName:@todos"]; NSPredicate * predicate = [NSPredicate predicateWithFormat:@“Complete == NO"];

[self.table readWithPredicate:predicate completion:^(NSArray *results, NSInteger totalCount, NSError *error)

{ self.items = [results mutableCopy]; [self.tableView reloadData];

}];

Page 51: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Implement CUD Operations

• 新增、修改、刪除等動作則是透過MSTable的insert、update、delete函式,跟Windows Phone、Android不同的是iOS並沒有所謂的Customers類別,而是用NSDictionary來呈現資料列。

NSDictionary *item = @{ @”Description" : “buy computer” };

Page 52: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

More Real World Application Demo

Page 53: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Introducing Remote Photo Application

• 一個簡易的遠端監視App

• 應用second device的觀念

• 運作流程

• 使用者可將一支手機放置於要監視的房間

• Remote Photo 應用程式每隔5分鐘會拍一次照, 並上傳到WAMS

• 另一隻手機可以用同一個帳號登入WAMS, 接著就能看到照片

• 特殊需求

• WAMS端只保留一小時內的照片

Page 54: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

Remote Photo Application

DEMO

Page 55: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

參考資料

• Windows Azure Mobile Document

• http://www.windowsazure.com/en-us/develop/mobile/reference/

• My Blog

• http://www.dotblogs.com.tw/code6421

• Windows Azure Mobile Service SDK for Windows Phone 7

• https://github.com/zaxy78/azure-mobile-wp7-sdk

• (unofficial)

Page 56: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

如何獲得百萬跑車?2013/09/30前,只要依照規定啓用MSDN訂閱隨附的Windows Azure 權益,即有機會將北美預估

零售價 118,600 美元的 Aston Martin V8 Vantage Coupe 開回家.

詳情請參閱Windows Azure 網站 : http://www.microsoft.com/taiwan/windowsazure/

Page 57: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料

start now.

http://WindowsAzure.com

Page 58: Introducing Cloud Architecturedownload.microsoft.com/download/C/6/0/C60E2BD0-8A7... · 如果使用Windows Azure Mobile Service • 提供SQL Database Service • 可以儲存資料