Top Banner
52
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Windows Azure Developer Day - WAMS
Page 2: Windows Azure Developer Day - WAMS

Introducing Cloud Architecture

• The Cloud Architecture

• IaaS

• PaaS

• Next Generation Cloud Service Architecture

• 架構於現有的PaaS/IaaS基礎上

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

• 提供直覺且簡單的服務管理介面

• 是針對特定型別的應用程式而設計的服務

Page 3: Windows Azure Developer Day - WAMS

Windows Azure Mobile Services (WAMS)

• 架構於現有的PaaS上

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

• 資料儲存: SQL Database Services

• 使用者驗證: Authentication Services

• 訊息推播: Push Notification Services

• 為

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

• 提供

• 直覺的管理介面

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

Page 4: Windows Azure Developer Day - WAMS

連線型行動平台App的架構

Accessing data

authentication

Call web service

Page 5: Windows Azure Developer Day - WAMS

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

• 使用者驗證

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

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

• 儲存待辦事項的資料

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

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

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

• 因此

• 您需要一個Web Server

• 提供使用者驗證

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

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

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

• 儲存使用者資料

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

• ASP.NET

• PHP

• …..

Page 6: Windows Azure Developer Day - WAMS

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

Page 8: Windows Azure Developer Day - WAMS

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

我們還沒開始談App的開發

也沒談到安全性的問題…….

Page 9: Windows Azure Developer Day - WAMS

如果使用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: Windows Azure Developer Day - WAMS

讓我們開始吧

建立 Windows Azure Mobile Service

DEMO

Page 12: Windows Azure Developer Day - WAMS

建立資料表

Page 13: Windows Azure Developer Day - WAMS

關於權限

Can use by any user

Need application key

Authenticated users

Internal use only

Page 14: Windows Azure Developer Day - WAMS

設定整合驗證 – Google Account

• 您可以透過 Facebook, Microsoft Account, Google Account 或

Twitter來驗證使用者

Page 15: Windows Azure Developer Day - WAMS

關於Google Account

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

Page 16: Windows Azure Developer Day - WAMS

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

Page 18: Windows Azure Developer Day - WAMS

建立 Windows Phone 應用程式

• 透過NuGet Package Manager 加入 WAMS

Client Library's

Page 19: Windows Azure Developer Day - WAMS

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

Page 20: Windows Azure Developer Day - WAMS

存取 Mobile Service

public partial class App : Application { internal static MobileServiceClient MobileService = new MobileServiceClient("https://todo64.azure-mobile.net/", "xisXQCpUGwQfwOwUqvxcqwkVhtdiLR72");

• 建立MobileServiceClient物件

Page 21: Windows Azure Developer Day - WAMS

資料表與類別的對應

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: Windows Azure Developer Day - WAMS

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

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: Windows Azure Developer Day - WAMS

如何取得使用者資訊?

• 當使用者登入後

• 您可以由驗證提供者端(Facebook, Microsoft Account,

Google Account , Twitter)取得使用者資訊

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

• 但

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

• 如果需要, 您可以

• 撰寫Server-Side Script來取得

• 或是撰寫Custom API回傳給客戶端

Page 24: Windows Azure Developer Day - WAMS

Table Triggers

• 開發者可以自訂CRUD 的個別行為

• 以Node.js語法撰寫, 未來會提供.NET Language(像是C#

等等)

Page 25: Windows Azure Developer Day - WAMS

Storing User ID

• 我們需要分辨待辦項目是屬於哪個使用者的

• 因此必須自訂Insert的行為, 新增時存入使用者ID

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

• createDate, userId 並未定義於先前的對應類別

• WAMS會自動添加這些欄位, 而客戶端可以不知道這些欄位的存在

Page 26: Windows Azure Developer Day - WAMS

UI的設計與資料的取得

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

• 取得資料表物件

• 新增資料

Page 27: Windows Azure Developer Day - WAMS

Test And Run

Page 28: Windows Azure Developer Day - WAMS

Designing UI and Get data

Todo Item record

ListBox

Page 29: Windows Azure Developer Day - WAMS

取得資料

• 這行程式會回傳所有資料

• 通常, 我們只希望回傳該使用者的資料

• 透過自訂Read 可以達到這個需求

Page 30: Windows Azure Developer Day - WAMS

實作 CUD

• 更新資料

• 刪除資料

• 查詢資料

Page 31: Windows Azure Developer Day - WAMS

以Custom API實作查詢

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

• 但

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

Page 32: Windows Azure Developer Day - WAMS

以Custom API實作查詢

客戶端呼叫API的程式碼

Page 33: Windows Azure Developer Day - WAMS

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

• 建立 channels 資料表

• 大多數的 push notification services (WP, iOS,

Android) 都是基於channel的觀念

• Push Notification Services會分配一個ID給裝置

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

Page 34: Windows Azure Developer Day - WAMS

建立 新增 channel 資料的API

Page 35: Windows Azure Developer Day - WAMS

建立排程

Page 36: Windows Azure Developer Day - WAMS

定時執行的Script

Page 37: Windows Azure Developer Day - WAMS

Test And Run

Demo

Page 38: Windows Azure Developer Day - WAMS

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

Page 39: Windows Azure Developer Day - WAMS

Android Version

Page 40: Windows Azure Developer Day - WAMS

您需要什麼?

• 下載client library's for android

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

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

Page 41: Windows Azure Developer Day - WAMS

撰寫程式

• 建立 mobile service client 物件

• 設計UI

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

Page 42: Windows Azure Developer Day - WAMS

回傳資料

_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: Windows Azure Developer Day - WAMS

實作CUD

• 新增

• 刪除

• 修改

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

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

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

Page 44: Windows Azure Developer Day - WAMS

Integrate Google Cloud Message

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

Page 45: Windows Azure Developer Day - WAMS

呼叫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: Windows Azure Developer Day - WAMS

Test And Run

Demo

Page 47: Windows Azure Developer Day - WAMS

IOS

Page 48: Windows Azure Developer Day - WAMS

What’s you need?

• 下載Client Library's

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

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

Page 49: Windows Azure Developer Day - WAMS

撰寫程式

• 建立 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: Windows Azure Developer Day - WAMS

回傳資料

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: Windows Azure Developer Day - WAMS

Implement CUD Operations

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

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

Page 52: Windows Azure Developer Day - WAMS

參考資料

• 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)