如何使用azure mobile service

161
如如如如 Azure Mobile Services - Windows Phone 如如 Pou 2013.11.30 http://www.windowsazure.com/en-us/develop/ mobile/?fb=zh-tw

Upload: pou-mason

Post on 26-May-2015

693 views

Category:

Self Improvement


7 download

DESCRIPTION

介紹有關Azure Mobile Service的操作與應用。

TRANSCRIPT

Page 1: 如何使用Azure mobile service

如何使用 Azure Mobile Services- Windows Phone 為例

Pou2013.11.30

http://www.windowsazure.com/en-us/develop/mobile/?fb=zh-tw

Page 2: 如何使用Azure mobile service

大綱

• Mobile Service 概述• 功能說明與練習

• Data• Push Notification• User Auth• Server Script• Custom API• …

Page 3: 如何使用Azure mobile service

Mobile Service 概述以下簡寫為 WAMS 或 Mobile Service

Page 4: 如何使用Azure mobile service

Concept

Page 5: 如何使用Azure mobile service

Functions

Page 6: 如何使用Azure mobile service

Mobile Service 在做什麼

• 為 Windows 、 Android 及 iOS 應用程式• 建立可擴充的安全後端

• 在雲端儲存資料• 輕鬆驗證使用者• 傳送推播通知• 取用偏好的服務• 監視、警示及自動擴充 ( 預覽 )

Page 7: 如何使用Azure mobile service

Mobile Service 的好處

• 減少開撰寫維護雲端程式• 專心在 App 端開發• RESTful API 支持多個平台使用

• Windows Store/Phone 、 Android 、 iOS 、 HTML

• 提供 App 開發的基礎建設• 彈性擴充• 資料儲存、身份驗證、推播通知• 官方提供了主流平台的 SDK

Page 8: 如何使用Azure mobile service

Mobile Service 重點功能

• DATA• User Authorization• Push Notification• Server Script• Scale 、 Logging 、 Diagnastics

Page 9: 如何使用Azure mobile service

DATA

• 提供存取 Windows Azure 的儲存體• SQL 資料庫• Blob 儲存體• 資料表儲存體• 協力廠商資料服務,如 Mongo DB 等

• Mobile Service 和 Service Bus Transfer• 提供了連接到內部部署資料庫的選項• 敏感性資料未儲存在雲端亦可供行動裝置存取

Page 10: 如何使用Azure mobile service

USER AUTH

• 提供第三方驗證服務• Facebook 、 Twitter 、 Microsoft 、 Google• 得到被賦予的應用程式識別碼和機密

• 免除自行開發、設定和測試驗證系統的需求

• 只需撰寫幾行程式碼就可以驗證您的使用

Page 11: 如何使用Azure mobile service

PUSH NOTIFICATION

• 支援 Windows Store / Windows Phone• 更支持

• iOS• 僅需上傳特定的開發人員憑證

• Android• 登入 GCM 識別碼

• 輸入簡單指令即可完成• push.apns.send (iOS 平台 )• push.gcm.send (Android 平台 )

Page 12: 如何使用Azure mobile service

SCRIPTS

• 撰寫簡單的伺服器端程式碼• 能以自訂邏輯強化資料作業

• 自定排程、資料彙整、圖像處理• 公開 API 支持整合其他服務

• 推播通知、簡訊或電子郵件• 連接至其他 Windows Azure 服務• Azure 市集提供的附加元件• 任何協力廠商服務

Page 13: 如何使用Azure mobile service

監視、警示及自動擴充

• 健全狀況計量儀表板• 監視應用程式的健全狀況與可用性

• 設定警示規則• 可用性偏低時您便會接獲通知

• 自動擴充規則• 依照當前的需求自動擴充或縮編

• 以盡量降低成本• 高可用性服務等級協定的安心保障

Page 14: 如何使用Azure mobile service

定價與限制 – 隨用隨付

Page 15: 如何使用Azure mobile service

定價與限制 – 6 個月或 12 個月

Page 16: 如何使用Azure mobile service

實作說明

Page 17: 如何使用Azure mobile service

實作說明

• Get Start• 操作 Data (Server & Client)• 操作 Push Notification• 操作 User Authorization• 熟悉 Server Scripts

Page 18: 如何使用Azure mobile service

Get Start

• 登入Azure入口網址• Windows Live ID

• 建立 Mobile Service• 建立一個 Data Table• 下載 SDK 或自動產生的專案• 利用 App 操作 Mobile Service 更新資料

http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started-with-data-dotnet/?fb=zh-tw

Page 19: 如何使用Azure mobile service

登入 Azure 入口網站

Page 20: 如何使用Azure mobile service

建立 Mobile Service – 1/5

Page 21: 如何使用Azure mobile service

建立 Mobile Service – 2/5

輸入想要的名字

選擇東亞

Page 22: 如何使用Azure mobile service

建立 Mobile Service – 3/5

設定登入的帳密

Page 23: 如何使用Azure mobile service

建立 Mobile Service – 4/5

輸入想要的定序

http://www.dotblogs.com.tw/jimmyyu/archive/2009/08/30/10320.aspx

Page 24: 如何使用Azure mobile service

建立 Mobile Service – 5/5

Mobile Service

Page 25: 如何使用Azure mobile service

建立資料表 – 1/3

建立資料表

Page 26: 如何使用Azure mobile service

建立資料表 – 2/3

設定可以存取該資料表的權限

輸入資料名字: TodoItem

Page 27: 如何使用Azure mobile service

建立資料表 – 3/3

Page 28: 如何使用Azure mobile service

加入程式碼 – 1/2

• 直接使用範例程式下載

Page 29: 如何使用Azure mobile service

加入程式碼 – 2/2

• 加入既有專案

更換自行建立的 Mobile Service

Page 30: 如何使用Azure mobile service

執行結果

todoTable.InsertAsync(todoItem)

await todoTable.UpdateAsync(item)

items = await todoTable .Where(todoItem => todoItem.Complete == false) .ToCollectionAsync();

Page 31: 如何使用Azure mobile service

重點提醒

• 程式端必要元素• Microsoft.WindowsAzure.MobileServices• MobileServiceClient

• 請求與交易Mobile Service 的處理器• 設定 Mobile Service位置• 授權 Key

• IMobileServiceTable• 物件化操作 Mobile Service 資料表資料

• MobileServiceCollection<TTable, TCol>• 作用於 databinding to list

Page 32: 如何使用Azure mobile service

Handle Data By Server Script

Page 33: 如何使用Azure mobile service

Validate and modify data • Server Script增加字串長度限制• 更新 Client 端來支持驗證• Server script增加 timestamp 完成新增• 利用 timestamp 更新 Client 端呈現的資料

Page 34: 如何使用Azure mobile service

增加字串長度限制 – 1/2

Page 35: 如何使用Azure mobile service

增加字串長度限制 – 2/2

Page 36: 如何使用Azure mobile service

修改 Client 端程式碼

• MobileServiceInvalidOperationException• Sever Script 增加的驗證條件• 既有的 Client 執行會遇到 Exception

Page 37: 如何使用Azure mobile service

增加 timestamp 完成新增

Page 38: 如何使用Azure mobile service

更新 Client 端 timestamp呈現 – 1/3

Page 39: 如何使用Azure mobile service

更新 Client 端 timestamp呈現 – 2/3

Page 40: 如何使用Azure mobile service

更新 Client 端 timestamp呈現 – 3/3

Page 41: 如何使用Azure mobile service

執行結果

處理MobileServiceInvalidOperationException

呈現新欄位 CreatedAt

Page 42: 如何使用Azure mobile service

Refine Mobile Services queries with paging

Page 43: 如何使用Azure mobile service

Paging

• Client 指定擷取的資料數• Client 指定資料區間

Page 44: 如何使用Azure mobile service

Client 指定擷取的資料數

• 定義取得前 3筆資料• 在 RESTful API 的 QueryString 為 $top=3

Page 45: 如何使用Azure mobile service

Client 指定資料區間

• 指定跳過前 3筆,往後捉 3筆

Page 46: 如何使用Azure mobile service

重點提醒

• Server Scripts• CRUD過程裡程序化自訂邏輯加強功能

• 動態 Schemas• Mobile Service 預設開啟• Server Script 與 Client 端均可動態調整

• 建議二邊一致• Client

• 熟悉 IMobileServiceTable物件的操作

Page 47: 如何使用Azure mobile service

Push Notification

Page 48: 如何使用Azure mobile service

Push Notification

• Windows Store• Windows Push Notification Services (WNS)

• Windows Phone• Microsoft Push Notification Service (MPNS)

• Google Cloud Messaging (GCM)• Android

• Apple Push Notifications (APNs)• iOS

Page 49: 如何使用Azure mobile service

Windows Push Notification• Concept

channel

channel

Page 50: 如何使用Azure mobile service

Microsoft Push Notification Service• MPNS

•每天的推播數超過 500 個訊息• 需註冊MPNS

channel

channel

Page 51: 如何使用Azure mobile service

Google Cloud Messaging• Need google account to Enable GCM• App get GCM channel• App Server send message to GCM by the

channel

Page 52: 如何使用Azure mobile service

Apple Push Notifications• Provider keep device token of the app

• Send message to APNs by the device token

Page 53: 如何使用Azure mobile service

Push Notification - Mobile Service• Concept

ProviderSend message

Forward to target devices

WNSMPNS GCM

APNs

Page 54: 如何使用Azure mobile service

Get Start for Push Notification• Server Script 接收新增用戶時一併發送推

送• 以 Windows Phone 為例•同時發送給多個設備

• 增加新 table• Client 端增加新的類別• Client 取得 channel 加入新 table• Server Script

• 在新 table 加入新 channel 時加上檢查• 新增項目時,加上發送推送

Page 55: 如何使用Azure mobile service

Server Script 新增並推送 – 1/3

Page 56: 如何使用Azure mobile service

Server Script 新增並推送 – 2/3• For Windows Phone Tile update

Page 57: 如何使用Azure mobile service

Server Script 新增並推送 – 3/3• For GCM message

Page 58: 如何使用Azure mobile service

Windows Phone 取得 / 註冊Channel -1/3

Page 59: 如何使用Azure mobile service

Windows Phone 取得 / 註冊Channel -2/3

Page 60: 如何使用Azure mobile service

Windows Phone 取得 / 註冊Channel -3/3

Page 62: 如何使用Azure mobile service

Mobile Service 推送其他平台 – 2/2

Page 63: 如何使用Azure mobile service

同時推送給多個設備 – 1/5

• 建立新的 Table : Channel

Page 64: 如何使用Azure mobile service

同時推送給多個設備 – 2/5

• Client增加對應 Channel 的類別

• Id• 建立新 Table 後自動產生的識別欄位

Page 65: 如何使用Azure mobile service

同時推送給多個設備 – 3/5

• Client 取得 Chanel ,寫回 Channel 資料表

Page 66: 如何使用Azure mobile service

同時推送給多個設備 – 4/5

• Server Script增加 Channel 新增時的檢查

•執行 insertChannelIfNotFound•參數有值代表重覆;無值直接新增

Page 67: 如何使用Azure mobile service

同時推送給多個設備 – 5/5

todoitem 新增項目,並呼叫發推送

取得 Channel 資料表中所有的 Channel ,逐一

發送訊息

Page 68: 如何使用Azure mobile service

重點提醒

• Sever Script•針對發送平台具有不同指令•註冊 Channel 需做重覆檢查

• Android / iOS獨立需具備的要素• 憑證、 Google Console API

• MPNs 推送類型有 Tile 、 Toast

Page 69: 如何使用Azure mobile service

User AUTHhow to authenticate users in Windows Azure Mobile Services from your app

Page 70: 如何使用Azure mobile service

User Auth 原理 – 1/3

• 整合各種驗證用戶機制• Microsoft Account• Facebook login• Twitter login• Google login

• 減少需獨立開發帳號管理• 減少個資外洩•無需負擔帳號成本

Page 71: 如何使用Azure mobile service

User Auth 原理 – 2/3

Microsoft Account

Facebook login

Twitter login

Google logic

Client Application

Request authorization

auth

auth

auth

auth

tokenReturn token

Transaction by the token

Page 72: 如何使用Azure mobile service

User Auth 原理 – 3/3

https://www.ibm.com/developerworks/community/blogs/48a78681-82cc-434f-9c78-3e9117bfd466/resource/BLOGS_UPLOADED_IMAGES/Oauth_Basic_Flow.JPG?lang=en

Page 74: 如何使用Azure mobile service

事前準備

取得註冊驗證的 URL

Page 75: 如何使用Azure mobile service

Facebook login – 1/5

•進入 Facebook developers•註冊為開發者帳號• 建立新的應用程式

• 輸入該應用程式的唯一名稱• 取得應用程式重要資料

• App ID (勿外流 )• App Secret (勿外流 )

•註冊要採用 Facebook login 的 URL

http://www.windowsazure.com/en-us/develop/mobile/how-to-guides/register-for-facebook-authentication/?fb=zh-tw

Page 76: 如何使用Azure mobile service

Facebook login – 2/5

Page 77: 如何使用Azure mobile service

Facebook login – 3/5

Page 78: 如何使用Azure mobile service

Facebook login – 4/5

貼上 Mobile Service所產生的 URL

記下 APP ID 與 APP Secret

需回填至 Mobile Service

Page 79: 如何使用Azure mobile service

Facebook login – 5/5

填入 APP ID 與 APP Secret

Page 80: 如何使用Azure mobile service

設定操作資料表需要驗證

設定 todoitem 資料權限

僅限驗證的使用者

Page 81: 如何使用Azure mobile service

修改 Client 端程式碼 – 1/2

• 直接執行會收到 Exception• status code of 401 (Unauthorized)

•增加需進行身份驗證•獨立變數保存用戶登入狀態• MobileServiceUser

•因需要用戶輸入,建議放在第一個畫面

Page 82: 如何使用Azure mobile service

修改 Client 端程式碼 – 2/2

宣告MobileServiceUser保存登入狀態

指定驗證類型為Facebook

啟動驗證

Page 83: 如何使用Azure mobile service

Google login – 1/4

• 登入 Google apis • 選擇 APIs & auth

• 選擇 Registered apps• 輸入建立 Name

• 設定為 Web application• 輸入 Mobile Service所產生的網址

• 取得建立的• Client ID 、 Client Secret

Page 84: 如何使用Azure mobile service

Google login – 2/4

Page 85: 如何使用Azure mobile service

Google login – 3/4

Page 86: 如何使用Azure mobile service

Google login – 3/4

貼上 Mobile Service所產生的 URL

記下 APP ID 與 APP Secret

需回填至 Mobile Service

Page 87: 如何使用Azure mobile service

Google login – 4/4

Page 88: 如何使用Azure mobile service

執行結果

Page 89: 如何使用Azure mobile service

重點提醒

• 依驗證身份提供者類型•將Mobile Service 的 URL 加以註冊• 取得提供者 API Key

•將提供者提供的 Key 加入 Mobile Service•調整資料表需有通過驗證才可以存取• Client增加呼叫驗證的方式

• 需保存用戶資訊

Page 90: 如何使用Azure mobile service

Server Script

http://www.windowsazure.com/en-us/develop/mobile/how-to-guides/work-with-server-scripts/?fb=zh-tw

Page 91: 如何使用Azure mobile service

Server Script – 1/3

• 支援自訂邏輯,儲存於 Server•類似 JavaScript語法• 功能

• CRUD 、 Schedule Jobs 、 HTTP methods define in a custom API

• 基於 REST APIs 為底• Script不保存執行狀態•每一次請求均是新的開始,狀態均被初始化• 如需保存,請另寫入 tables

http://msdn.microsoft.com/en-us/library/windowsazure/jj554226.aspx ( 完整指令教學 )

Page 92: 如何使用Azure mobile service

Server Script – 2/3

Scriptapns object request object gcm object response object push object service object mpns object statusCodes object mssql object table object update function user object delete function wns object insert function query object read function console object

Page 93: 如何使用Azure mobile service

Server Script – 3/3

• Table operations• Custom API• Job Scheduler• Using Command line tool (教學)• Working with tables• Debugging and troubleshooting

Page 94: 如何使用Azure mobile service

Table Operations

Page 95: 如何使用Azure mobile service

Table operations – 1/4

• 專註冊於處理 tables 上的 CRUD• 其方法名稱需對應 CRUD運作的一種

• insert• update• delete• read

• 一個 table 僅能註冊一個 operations script•經由每次 REST request 時,被執行一次

Page 96: 如何使用Azure mobile service

Table operations 範例

• 自定義邏輯•重要參數類型

Page 97: 如何使用Azure mobile service

Table operations – 2/4

•固定三個參數• first

• insert/update• item object ,屬 JSON物件,代表一個 Row ,透過 Name來操作,例如: item.Owner

• delete• the ID of the record to delete

• read• query object ,代表一個 row set

• second• user object

• third• request object

• 控制 request 的執行與 response 至 client 的內容

Page 98: 如何使用Azure mobile service

Table operations – 3/4

•重點• Insert: function insert (item, user, request) { … }• Update: function update (item, user, request) { … }• Delete: function del (id, user, request) { … }• Read: function read (query, user, request) { … }

Page 99: 如何使用Azure mobile service

Table operations – 4/4

• Register for table operations• Override the default response• Override the execute success• Override default error handling• Add custom parameters• Work with table users

Page 100: 如何使用Azure mobile service

Register for table operations – 1/2• Azure Management Portal

• 定義Mobile Service每個 tables 的 opertaions• Source Control

• 建立 <table>.<opertaion>.js•放置 .\server\table 的子目錄•執行時,自動取得該 js進行

• Common line tool

Page 101: 如何使用Azure mobile service

Register for table operations – 2/2• Table operation script重點二個 function

• execute function• request已被 operation 完成• 回傳一個標準的 response

• respond function• 自訂 response

For more examples, see Read and write data, Modify the request and Validate data.

Page 102: 如何使用Azure mobile service

Override the default response•實作自訂邏輯,修改成功回傳內容

• 例如:檢查值、確認符合條件等

For more examples, see Modify the response.

Page 103: 如何使用Azure mobile service

Override execute success• 預設 table operation呼叫 execute()

• 自動回傳 response• Override

• 可傳入二個參數,定義 success 或 error

Page 104: 如何使用Azure mobile service

Override default error handling•實作自訂邏輯,修改失敗回傳內容

• 例如:儲存失敗、指令錯誤等• 預設

• server script 產生 log• 產生 error result 至 response

• 自訂

Page 105: 如何使用Azure mobile service

Access custom parameters – 1/2•請求 Mobile Service 可夾帶其他參數

• 利用 URI 的 Query String : URI?key=value• 例如

• 傳遞的參數• duplicateText = false

• Custom parameters•轉換成 JSON物件後,放置 request object 的「 parameters」屬性

Page 106: 如何使用Azure mobile service

Access custom parameters – 2/2

利用 Request object的 parameters屬性

對 table進行 where查詢,成功指定給定義好的

function

Page 107: 如何使用Azure mobile service

Work with users

• Mobile Service 提供身份驗證功能• 當驗證用戶執行 table operations 時

• user object• 出現於 operations 的參數之中

• user object.userid• userid屬性可用於儲存與擷取用戶個別設定資訊

Page 108: 如何使用Azure mobile service

Custom API

Page 109: 如何使用Azure mobile service

Custom API – 1/2

• 在 Mobile Service 建立自訂 endpoint•藉由 standard HTTP methods

• GET 、 POST 、 PUT 、 PATCH 、 DELETE•每一個 API 可自訂於單一個 script file• Mobile service runtime收到 Client請求

•轉發給 Custom API• API中搭配 express.js library加以操作

https://<service_name>.azure-mobile.net/api/<api_name>

Page 110: 如何使用Azure mobile service

Custom API – 2/2

• Define a custom API• Implement HTTP methods• Send and receive data as XML• Work with user and headers in a custom API• Define multiple routers in a custom API

Page 111: 如何使用Azure mobile service

Define a custom API – 1/2

可依照不同的 HTTP Method 開放操作權限

Page 112: 如何使用Azure mobile service

Define a custom API – 2/2

•每一個 HTTP Method均會有 request / response

• {} 操作的回應項目,來自 express.js library• 回傳格式為 JSON

Page 113: 如何使用Azure mobile service

Implement HTTP methods• Standard HTTP Method

• The custom API endpoint cannot be called using an HTTP method that has not been implemented in the server script

• a 405 (Method Not Allowed) error response is returned

• Separate permission levels can be assigned to each support HTTP method.

exports.get = function(request, response) { ... }; exports.post = function(request, response) { ... }; exports.patch = function(request, response) { ... }; exports.put = function(request, response) { ... }; exports.delete = function(request, response) { ... };

Page 114: 如何使用Azure mobile service

Send and receive data as XML• Mobile Service 使用 JSON呈現資料給

Client• 想用 XML 的結構傳遞

•相關範例• Define a custom API that supports periodic notifi

cations

Page 115: 如何使用Azure mobile service

Work with users and headers in a custom API – 1/3• User object

• Custom API藉由 request object 取得• request.user.userId

• HTTP Headers•夾帶描述 request 、 response 的說明

• Host 、 user-agent 、 content-type… 等• 定義交易的類型• Key/value 資料型態

Page 116: 如何使用Azure mobile service

Work with users and headers in a custom API – 2/3

• 取得 user 的 table•藉由指定 user.userid來搜尋對應的資料

Page 117: 如何使用Azure mobile service

Work with users and headers in a custom API – 3/3

• 取得 request中 header• my-custom-header

•相關 request / response object 操作• http://expressjs.com/api.html#res.status

Page 118: 如何使用Azure mobile service

Define multiple routes in a custom API – 1/2• Mobile Service 支援 Custom API

• 定義multiple path / routes• 例如

• 定義一個 HTTP GET 方法: calculator• 可再定義二個 sub function

• add• subtract

https://<service>.azure-mobile.net/api/calculator/addhttps://<service>.azure-mobile.net/api/calculator/sub

Page 119: 如何使用Azure mobile service

Define multiple routes in a custom API – 2/2• 利用 exports.register 定義 multiple path

•藉由 api object註冊 get,post,put,patch,delete依照 HTTP Method 可加

以區分設定固定二個參數

First : route 名稱Second : function

Page 121: 如何使用Azure mobile service

Job Scheduler

Page 122: 如何使用Azure mobile service

Job Scheduler

• 定義週期或 on-demand 的 server script 任務

•常用於定期的任務• 例如

• 清除表格資料、批量處理•藉由 Schedule jobs 操作•註冊 schedule job 其 main function

• 為 schedule job name•非 HTTP Method•無法傳入參數,由 job 自行處理

• 可自訂子 function 加以處理

Page 123: 如何使用Azure mobile service

Define scheduled job scripts – 1/2

週期與 on-demand

Page 124: 如何使用Azure mobile service

Define scheduled job scripts – 2/2

與 job name相同的main function 為程式進

入點

Page 125: 如何使用Azure mobile service

Schedule recurring jobs in Mobile Services• 範例練習

• Schedule recurring jobs in Mobile Services• http

://www.windowsazure.com/en-us/develop/mobile/tutorials/schedule-backend-tasks/?fb=zh-tw

• 結合 Twitter進行取得更新資料

Page 126: 如何使用Azure mobile service

Source control, share code, …

Page 127: 如何使用Azure mobile service

Source control, share code, … - 1/3• Mobile Service 使用 Node.js• 自訂的 script ,直接被加入 Node.js 的

module•藉由 source control

• 自訂 module• 新增其他 Node.js 至 Service中

Page 128: 如何使用Azure mobile service

Source control, share code, … - 2/3• global require function

http://www.windowsazure.com/en-us/develop/mobile/how-to-guides/work-with-server-scripts/?fb=zh-tw#scheduler-scripts

Page 129: 如何使用Azure mobile service

Source control, share code, … -3/3• How to: load Node.js modules• Share code by using source control

• Leverage shared code and Node.js modules in your server scripts.

• Use helper functions• Work with app settings

Page 130: 如何使用Azure mobile service

Use helper functions – 1/2

• helper function• 需宣告在 main function之後• 需要的參數均要先全部定義好• 寫在同一個 script裡,屬於區域方法

Page 131: 如何使用Azure mobile service

Use helper functions – 2/2• 定義一次性與分享於其他 server script• 需定義於 export object

• .services\shared\ :分享於不同 server script

Page 132: 如何使用Azure mobile service

Work with app settings – 1/3• Mobile Service 支持安全性儲存設置值

•類別 app settings• 支持 server script runtime 時可使用

• App settings• name/value pairs• 儲存時均會加密

Page 133: 如何使用Azure mobile service

Work with app settings – 2/3• Custom API

•藉由 service object 擷取 app settings

Page 134: 如何使用Azure mobile service

Work with app settings – 3/3• 使用 configuration module

Page 135: 如何使用Azure mobile service

Working with tables

Page 136: 如何使用Azure mobile service

Working with tables – 1/2•大部分情境中的 Server Script 會操作 tables

• 例如• Mobile Service不具有狀態,如需保存則需 tables• 搭配權限控制 request 存取的能力

•二大方式連接 tables• table object

• 簡易式操作資料表資訊• mssql object

• 類似用 T-SQL 指令方式• 提供更彈性與複雜的指令

Page 137: 如何使用Azure mobile service

Working with tables – 2/2• Access tables from scripts• Perform Bulk Insert• Map JSON types to database types• Using Transact-SQL to access tables

Page 138: 如何使用Azure mobile service

Access tables from scripts – 1/3• table object

• getTable function 取得指定資料表物件• 指令於不同的模組用法不同

• table operations / schedule jobs

• custom api

• 在 shared function不可使用 table object• 要用參數傳入

Page 139: 如何使用Azure mobile service

Access tables from scripts – 2/3• 取得 table object 可以操作多次

Page 140: 如何使用Azure mobile service

Access tables from scripts – 3/3• 更新成功後,執行 help function•藉由物件新增其他資料表內容

Page 141: 如何使用Azure mobile service

Perform Bulk Insert – 1/2•情境

• 如果需要大量新增資料• Client 直接用 for 或 while來請求 insert

• 將受到 SQL connection limit 的錯誤回傳• 導向 Client無法完成請求

• 或收到 HTTP 500 Internal Server Error

•解法•採用 batch 的方式

• 例如: 100筆資料分 10筆一次來傳送

Page 142: 如何使用Azure mobile service

Perform Bulk Insert – 2/2

定義 Batch參數

定義迴圈方法

第一次啟動

Error Handler

定義成功處理下一筆

Page 143: 如何使用Azure mobile service

Map JSON types to database types – 1/2• Data類型在 Client 與 Mobile Service是不同的

• 資料轉換• Client 操作的類型為被序列化的 JSON• JSON 被 Server script 使用前先被轉換

JavaScript• Node.js (JSON.parse) • Date格式轉換另外符合 ISO8601 的 JSON 字串

• JavaScript data types再轉換成 SQL database types ,儲存於 table object

Page 144: 如何使用Azure mobile service

Map JSON types to database types – 2/2• JavaScirpt objects 與 T-SQL type類型對應表JavaScript property T-SQL typeNumber Float(53)Boolean BitDate DateTimeOffset(3)String Nvarchar(max)Buffer Not supportedObject Not supportedArray Not supportedStream Not supported

Page 145: 如何使用Azure mobile service

Using Transact-SQL to access tables – 1/2• table object不支援比較複雜的 table運作

• 例如: JOIN 、複雜查詢、呼叫 Store Procedure• mssql object

• query• 執行查詢,經由特定 T-SQL 字串,成功回傳一個

options object• queryRaw :

• 與 query相似,回傳結果為原始資料• open :

• 用於取得 Mobile Service database 的連線• 可用於該連線進行資料庫操作,例如: transaction

Page 146: 如何使用Azure mobile service

Using Transact-SQL to access tables – 2/2• Run a static query• Run a dynamic query• Join relational tables• Run a query that return raw results• Get access to a database connection

Page 147: 如何使用Azure mobile service

Run a static query

•執行一個固定沒有傳入參數的查詢•回傳為 JSON格式

Page 148: 如何使用Azure mobile service

Run a dynamic parameterized query

•執行時夾帶參數,搭配 User Auth進行

Page 149: 如何使用Azure mobile service

Join relational tables – 1/2• 使用 mssql object進行 tables 的 join•舉例二個 table

• ToDoItem

• priority外部參考 Priority table 的 number• Priority

{ text: 'Take out the trash', complete: false, priority: 1}

{ number: 1, description: 'Critical'}

Page 150: 如何使用Azure mobile service

Join relational tables – 2/2• JOIN二個資料表

•結果{ text: 'Take out the trash', complete: false, description: 'Critical'}

Page 151: 如何使用Azure mobile service

Run a query that returns raw results – 1/2• queryRaw

•回傳查詢的結果為原始的資料• 結果資料表結構• 每一個 Row / Column 的結構

Page 152: 如何使用Azure mobile service

Run a query that returns raw results – 2/2

回傳資料表結構

Row/Column資料對應

Page 153: 如何使用Azure mobile service

Get access to a database connection• 使用 open 方法取得 database connection• 使用情境

•執行 transaction 時• connection object: close, queryRaw, query, beginTransaction, commit, and rollback.

Page 154: 如何使用Azure mobile service

Debugging and trouble shopping• Write output to the mobile service logs

•追 bug最主要的方法• 預設 server script 發生錯誤會產生 logs• 自定義邏輯利用 console object 寫入 log

• %j :代表 JSON物件• 提醒

• logs請小心使用以免超載

Page 155: 如何使用Azure mobile service

管理 Mobile Service

Page 156: 如何使用Azure mobile service

儀表板

Page 157: 如何使用Azure mobile service

Mobile Service 重點提醒

• 提供整合 Push Notification介面•結合多種 User AUTH 機制減少開發• 支持 Client 與 Server 端便利資料操作• Server Scripts 加強複雜的邏輯情境• Custom API 擴充自訂功能• Job Schedule 、 Source Control•複雜的資料表操作•管理功能

Page 158: 如何使用Azure mobile service

Q & A

Page 159: 如何使用Azure mobile service

Thank you for listening.

Page 161: 如何使用Azure mobile service

References – 2/2

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

• Windows Azure Mobile Services - The Perfect Partner

• SolidQ Windows Azure Mobile Services & Windows 8

• Introducing Azure Mobile Services – Script and Custom API

• Introducing Windows Azure Mobile Services – Authentication

• Introducing Windows Azure Mobile Services

• http://www.windowsazure.com/en-us/develop/mobile/?fb=zh-tw

• Client library for .NET

• Client library for Javascript

• REST API reference