SlideShare uma empresa Scribd logo
1 de 161
如何使用
Azure Mobile Services
- Windows Phone為例
Pou
2013.11.30

http://www.windowsazure.com/en-us/develop/mobile/?fb=zh-tw
大綱
• Mobile Service概述
• 功能說明與練習
• Data
• Push Notification
• User Auth
• Server Script
• Custom API
•…
Mobile Service概述
以下簡寫為 WAMS或Mobile Service
Concept
Functions
Mobile Service在做什麼
• 為 Windows、Android 及 iOS 應用程式
• 建立可擴充的安全後端

• 在雲端儲存資料
• 輕鬆驗證使用者
• 傳送推播通知
• 取用偏好的服務
• 監視、警示及自動擴充 (預覽)
Mobile Service的好處
• 減少開撰寫維護雲端程式

• 專心在App端開發
• RESTful API支持多個平台使用
• Windows Store/Phone、Android、iOS、HTML

• 提供App開發的基礎建設

• 彈性擴充
• 資料儲存、身份驗證、推播通知
• 官方提供了主流平台的SDK
Mobile Service重點功能
• DATA
• User Authorization
• Push Notification
• Server Script
• Scale、Logging、Diagnastics
DATA
• 提供存取 Windows Azure 的儲存體

• SQL 資料庫
• Blob 儲存體
• 資料表儲存體
• 協力廠商資料服務,如 Mongo DB 等

• Mobile Service和 Service Bus Transfer

• 提供了連接到內部部署資料庫的選項
• 敏感性資料未儲存在雲端亦可供行動裝置存取
USER AUTH
• 提供第三方驗證服務
• Facebook、Twitter、Microsoft、Google
• 得到被賦予的應用程式識別碼和機密

• 免除自行開發、設定和測試驗證系統的需求
• 只需撰寫幾行程式碼就可以驗證您的使用
PUSH NOTIFICATION
• 支援Windows Store / Windows Phone
• 更支持
• iOS

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

• Android

• 登入GCM識別碼

• 輸入簡單指令即可完成

• push.apns.send (iOS 平台)
• push.gcm.send (Android 平台)
SCRIPTS
• 撰寫簡單的伺服器端程式碼
• 能以自訂邏輯強化資料作業

• 自定排程、資料彙整、圖像處理

• 公開API支持整合其他服務

• 推播通知、簡訊或電子郵件
• 連接至其他 Windows Azure 服務
• Azure市集提供的附加元件
• 任何協力廠商服務
監視、警示及自動擴充
• 健全狀況計量儀表板

• 監視應用程式的健全狀況與可用性

• 設定警示規則

• 可用性偏低時您便會接獲通知

• 自動擴充規則

• 依照當前的需求自動擴充或縮編
• 以盡量降低成本

• 高可用性服務等級協定的安心保障
定價與限制 – 隨用隨付
定價與限制 – 6個月或12個月
實作說明
實作說明
• Get Start
• 操作Data (Server & Client)
• 操作Push Notification
• 操作User Authorization
• 熟悉Server Scripts
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
登入Azure入口網站
建立Mobile Service – 1/5
建立Mobile Service – 2/5

輸入想要的名字

選擇東亞
建立Mobile Service – 3/5

設定登入的帳密
建立Mobile Service – 4/5

輸入想要的定序

http://www.dotblogs.com.tw/jimmyyu/archive/2009/08/30/10320.aspx
建立Mobile Service – 5/5

Mobile Service
建立資料表 – 1/3
建立資料表
建立資料表 – 2/3
輸入資料名字:TodoItem
設定可以存取該資
料表的權限
建立資料表 – 3/3
加入程式碼 – 1/2
• 直接使用範例程式下載
加入程式碼 – 2/2
• 加入既有專案

更換自行建立的Mobile Service
執行結果

todoTable.InsertAsync(todoItem)

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

await todoTable.UpdateAsync(item)
重點提醒
• 程式端必要元素
• Microsoft.WindowsAzure.MobileServices
• MobileServiceClient
• 請求與交易Mobile Service的處理器
• 設定Mobile Service位置
• 授權Key

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

• MobileServiceCollection<TTable, TCol>
• 作用於databinding to list
Handle Data
By Server Script
Validate and modify data
• Server Script增加字串長度限制
• 更新Client端來支持驗證
• Server script增加timestamp完成新增
• 利用timestamp更新Client端呈現的資料
增加字串長度限制 – 1/2
增加字串長度限制 – 2/2
修改Client端程式碼
• MobileServiceInvalidOperationException
• Sever Script增加的驗證條件
• 既有的Client執行會遇到Exception
增加timestamp完成新增
更新Client端timestamp呈現 –
1/3
更新Client端timestamp呈現 –
2/3
更新Client端timestamp呈現 –
3/3
執行結果

處理
MobileServiceInvalidOperationException

呈現新欄位CreatedAt
Refine Mobile
Services queries
with paging
Paging
• Client指定擷取的資料數
• Client指定資料區間
Client指定擷取的資料數

• 定義取得前3筆資料

• 在RESTful API的QueryString為 $top=3
Client指定資料區間

• 指定跳過前3筆,往後捉3筆
重點提醒
• Server Scripts
• CRUD過程裡程序化自訂邏輯加強功能

• 動態Schemas
• Mobile Service預設開啟
• Server Script與Client端均可動態調整
• 建議二邊一致

• Client

• 熟悉IMobileServiceTable物件的操作
Push Notification
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
Windows Push Notification
• Concept
channel

channel
Microsoft Push Notification
Service
• MPNS

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

channel
channel
Google Cloud Messaging
• Need google account to Enable GCM
• App get GCM channel
• App Server send message to GCM by the
channel
Apple Push Notifications
• Provider keep device token of the app
• Send message to APNs by the device token
Push Notification - Mobile
Service
• Concept

Send message
Provider

Forward to target devices

WNS

APNs
MPNS

GCM
Get Start for Push
Notification
• Server Script接收新增用戶時一併發送推送
• 以Windows Phone為例
• 同時發送給多個設備
•
•
•
•

增加新table
Client端增加新的類別
Client取得channel加入新table
Server Script
• 在新table加入新channel時加上檢查
• 新增項目時,加上發送推送
Server Script新增並推送 – 1/3
Server Script新增並推送 – 2/3
• For Windows Phone Tile update
Server Script新增並推送 – 3/3
• For GCM message
Windows Phone取得/註冊
Channel -1/3
Windows Phone取得/註冊
Channel -2/3
Windows Phone取得/註冊
Channel -3/3
Mobile Service推送其他平台 –
1/2
• Windows Store

• http://www.windowsazure.com/enus/develop/mobile/tutorials/get-started-with-pushdotnet/?fb=zh-tw

• Android

• http://www.windowsazure.com/enus/develop/mobile/tutorials/get-started-with-pushandroid/?fb=zh-tw
• message,from,collection-key

• iOS

• http://www.windowsazure.com/enus/develop/mobile/tutorials/push-notifications-tousers-wp8/?fb=zh-tw
Mobile Service推送其他平台 –
2/2
同時推送給多個設備 – 1/5
• 建立新的Table:Channel
同時推送給多個設備 – 2/5
• Client增加對應Channel的類別

• Id

• 建立新Table後自動產生的識別欄位
同時推送給多個設備 – 3/5
• Client取得Chanel,寫回Channel資料表
同時推送給多個設備 – 4/5
• Server Script增加Channel新增時的檢查

• 執行insertChannelIfNotFound

• 參數有值代表重覆;無值直接新增
同時推送給多個設備 – 5/5
todoitem新增項目,
並呼叫發推送

取得Channel資料表中
所有的Channel,逐一
發送訊息
重點提醒
• Sever Script

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

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

• MPNs推送類型有Tile、Toast
User AUTH
how to authenticate users in Windows Azure Mobile Services
from your app
User Auth原理 – 1/3
• 整合各種驗證用戶機制
• Microsoft Account
• Facebook login
• Twitter login
• Google login

• 減少需獨立開發帳號管理
• 減少個資外洩
• 無需負擔帳號成本
User Auth原理 – 2/3
auth

Microsoft
Account

Request authorization

Client
Application

Return token

token

auth

auth
Transaction by the token

auth

Facebook
login

Twitter
login

Google
logic
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
Get started with
authentication
• Facebook login
• Google logic
• Microsoft Account
• http://www.windowsazure.com/en-us/develop/mobile/tutorials/getstarted-with-users-wp8/?fb=zh-tw
• http://www.windowsazure.com/en-us/develop/mobile/how-toguides/register-for-microsoft-authentication/?fb=zh-tw

• Twitter login
• http://www.windowsazure.com/en-us/develop/mobile/how-toguides/register-for-twitter-authentication/?fb=zh-tw
事前準備

取得註冊驗證的URL
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
Facebook login – 2/5
Facebook login – 3/5
Facebook login – 4/5
記下APP ID與APP Secret
需回填至Mobile Service

貼上Mobile Service
所產生的URL
Facebook login – 5/5

填入APP ID與APP Secret
設定操作資料表需要驗證
設定todoitem資料權限
僅限驗證的使用者
修改Client端程式碼 – 1/2
• 直接執行會收到Exception
• status code of 401 (Unauthorized)

• 增加需進行身份驗證

• 獨立變數保存用戶登入狀態
• MobileServiceUser

• 因需要用戶輸入,建議放在第一個畫面
修改Client端程式碼 – 2/2
宣告MobileServiceUser
保存登入狀態

指定驗證類型為
Facebook

啟動驗證
Google login – 1/4
• 登入Google apis
• 選擇APIs & auth

• 選擇Registered apps

• 輸入建立Name

• 設定為Web application

• 輸入Mobile Service所產生的網址

• 取得建立的
• Client ID、Client Secret
Google login – 2/4
Google login – 3/4
Google login – 3/4
記下APP ID與APP Secret
需回填至Mobile Service

貼上Mobile Service
所產生的URL
Google login – 4/4
執行結果
重點提醒
• 依驗證身份提供者類型

• 將Mobile Service的URL加以註冊
• 取得提供者API Key

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

http://www.windowsazure.com/en-us/develop/mobile/how-to-guides/work-with-server-scripts/?fb=zh-tw
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 (完整指令教學)
Server Script – 2/3
Script
apns object
gcm object
push object

request object
response object
service object

mpns object
mssql object
update function
delete function
insert function
read function

statusCodes object
table object
user object
wns object
query object
console object
Server Script – 3/3
• Table operations
• Custom API
• Job Scheduler
• Using Command line tool (教學)
• Working with tables
• Debugging and troubleshooting
Table Operations
Table operations – 1/4
• 專註冊於處理tables上的CRUD
• 其方法名稱需對應CRUD運作的一種
• insert
• update
• delete
• read

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

• 自定義邏輯
• 重要參數類型
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的內容
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) { … }
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
Register for table
operations – 1/2
• Azure Management Portal

• 定義Mobile Service每個tables的opertaions

• Source Control

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

• Common line tool
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.
Override the default
response
• 實作自訂邏輯,修改成功回傳內容
• 例如:檢查值、確認符合條件等

For more examples, see Modify the response.
Override execute success
• 預設table operation呼叫execute()
• 自動回傳response

• Override

• 可傳入二個參數,定義success或error
Override default error
handling
• 實作自訂邏輯,修改失敗回傳內容
• 例如:儲存失敗、指令錯誤等

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

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

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

• 例如
• 傳遞的參數
• duplicateText = false

• Custom parameters

• 轉換成JSON物件後,放置request object的
「parameters」屬性
Access custom
parameters – 2/2
利用Request object
的parameters屬性
對table進行where查詢,成
功指定給定義好的function
Work with users
• Mobile Service提供身份驗證功能
• 當驗證用戶執行table operations時
• user object

• 出現於operations的參數之中

• user object.userid
• userid屬性可用於儲存與擷取用戶個別設定資訊
Custom API
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>
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
Define a custom API – 1/2

可依照不同的HTTP
Method開放操作權限
Define a custom API – 2/2

• 每一個HTTP Method均會有request / response
• {}操作的回應項目,來自express.js library
• 回傳格式為JSON
Implement HTTP methods
• Standard 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) { ... };

• 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.
Send and receive data as
XML
• Mobile Service使用JSON呈現資料給Client
• 想用XML的結構傳遞

• 相關範例
• Define a custom API that supports periodic
notifications
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資料型態
Work with users and
headers in a custom API –
2/3

• 取得user的table
• 藉由指定user.userid來搜尋對應的資料
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
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/add
https://<service>.azure-mobile.net/api/calculator/sub
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
Call a custom API from the
client
• 範例練習
• http://www.windowsazure.com/enus/develop/mobile/tutorials/call-custom-apiwp8/?fb=zh-tw
Job Scheduler
Job Scheduler
• 定義週期或on-demand的server script任務
• 常用於定期的任務
• 例如

• 清除表格資料、批量處理

• 藉由 Schedule jobs 操作
• 註冊 schedule job 其main function
• 為schedule job name
• 非HTTP Method
• 無法傳入參數,由job自行處理
• 可自訂子function加以處理
Define scheduled job
scripts – 1/2

週期與on-demand
Define scheduled job
scripts – 2/2

與job name相同的main
function為程式進入點
Schedule recurring jobs in
Mobile Services
• 範例練習
• Schedule recurring jobs in Mobile Services
• http://www.windowsazure.com/enus/develop/mobile/tutorials/schedule-backendtasks/?fb=zh-tw
• 結合Twitter進行取得更新資料
Source control,
share code, …
Source control, share
code, … - 1/3
• Mobile Service使用Node.js
• 自訂的script,直接被加入Node.js的module
• 藉由source control
• 自訂module
• 新增其他Node.js至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
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
Use helper functions – 1/2

• helper function

• 需宣告在main function之後
• 需要的參數均要先全部定義好
• 寫在同一個script裡,屬於區域方法
Use helper functions – 2/2
• 定義一次性與分享於其他server script
• 需定義於 export object
• .servicesshared:分享於不同server script
Work with app settings – 1/3
• Mobile Service支持安全性儲存設置值
• 類別 app settings
• 支持server script runtime時可使用

• App settings
• name/value pairs
• 儲存時均會加密
Work with app settings – 2/3
• Custom API

• 藉由 service object 擷取app settings
Work with app settings – 3/3
• 使用configuration module
Working with tables
Working with tables – 1/2
• 大部分情境中的Server Script會操作tables
• 例如

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

• 二大方式連接tables
• table object

• 簡易式操作資料表資訊

• mssql object

• 類似用T-SQL指令方式
• 提供更彈性與複雜的指令
Working with tables – 2/2
• Access tables from scripts
• Perform Bulk Insert
• Map JSON types to database types
• Using Transact-SQL to access tables
Access tables from scripts –
1/3
• table object
• getTable function取得指定資料表物件
• 指令於不同的模組用法不同

• table operations / schedule jobs
• custom api

• 在shared function不可使用table object
• 要用參數傳入
Access tables from scripts –
2/3
• 取得table object可以操作多次
Access tables from scripts –
3/3
• 更新成功後,執行help function
• 藉由物件新增其他資料表內容
Perform Bulk Insert – 1/2
• 情境

• 如果需要大量新增資料
• Client直接用for 或 while來請求insert
• 將受到SQL connection limit 的錯誤回傳

• 導向Client無法完成請求

• 或收到HTTP 500 Internal Server Error

• 解法

• 採用batch的方式
• 例如:100筆資料分10筆一次來傳送
Perform Bulk Insert – 2/2
定義Batch參數

定義成功處理下一筆

Error Handler
定義迴圈方法

第一次啟動
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
Map JSON types to database
types – 2/2
• JavaScirpt objects與T-SQL type類型對應表
JavaScript property
Number
Boolean
Date
String
Buffer
Object
Array
Stream

T-SQL type
Float(53)
Bit
DateTimeOffset(3)
Nvarchar(max)
Not supported
Not supported
Not supported
Not supported
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
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
Run a static query

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

• 執行時夾帶參數,搭配User Auth進行
Join relational tables – 1/2
• 使用mssql object進行tables的join
• 舉例二個table
• ToDoItem
{ text: 'Take out the trash', complete: false, priority: 1}

• priority外部參考Priority table的number

• Priority
{ number: 1, description: 'Critical'}
Join relational tables – 2/2
• JOIN二個資料表

• 結果
{ text: 'Take out the trash', complete: false, description: 'Critical'}
Run a query that returns raw
results – 1/2
• queryRaw

• 回傳查詢的結果為原始的資料
• 結果資料表結構
• 每一個Row / Column的結構
Run a query that returns raw
results – 2/2
回傳資料表結構

Row/Column
資料對應
Get access to a database
connection
• 使用open方法取得database connection
• 使用情境
• 執行transaction時

• connection object:
close, queryRaw, query, beginTransaction, commit, a
nd rollback.
Debugging and trouble
shopping
• Write output to the mobile service logs
• 追bug最主要的方法

• 預設server script發生錯誤會產生logs
• 自定義邏輯利用console object寫入log

• %j:代表JSON物件

• 提醒
• logs請小心使用以免超載
管理Mobile Service
儀表板
Mobile Service重點提醒
• 提供整合Push Notification介面
• 結合多種User AUTH機制減少開發
• 支持Client與Server端便利資料操作
• Server Scripts加強複雜的邏輯情境
• Custom API擴充自訂功能
• Job Schedule、Source Control
• 複雜的資料表操作
• 管理功能
Q&A
Thank you for listening.
References – 1/2
• Work with server scripts in Mobile Services
• Schedule recurring jobs in Mobile Services
• Send email from Mobile Services with
SendGrid
• Upload images to Windows Azure Storage by
using Mobile Services
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

Mais conteúdo relacionado

Destaque

Medición de la calidad en Programas de Postgrado
Medición de la calidad en Programas de PostgradoMedición de la calidad en Programas de Postgrado
Medición de la calidad en Programas de PostgradoAlvaro Padilla Omiste
 
BizTalk練習投影片
BizTalk練習投影片BizTalk練習投影片
BizTalk練習投影片Pou Mason
 
Replant: a new generation of botanical art
Replant: a new generation of botanical artReplant: a new generation of botanical art
Replant: a new generation of botanical artTheGEOProject
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryMeetu Maltiar
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaMeetu Maltiar
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaMeetu Maltiar
 
WP8.1 Tiles and Notifications
WP8.1 Tiles and NotificationsWP8.1 Tiles and Notifications
WP8.1 Tiles and NotificationsPou Mason
 
Jeopardy Christmas
Jeopardy ChristmasJeopardy Christmas
Jeopardy Christmascdaleyccs
 
Christmas Jeopardy
Christmas JeopardyChristmas Jeopardy
Christmas Jeopardycdaleyccs
 
Františkánská zahrada
Františkánská zahradaFrantiškánská zahrada
Františkánská zahradarehbeluk
 
NP Presentación borrador presupuestos
NP Presentación borrador presupuestosNP Presentación borrador presupuestos
NP Presentación borrador presupuestosUPyD Getafe
 
Curriculum asier
Curriculum asierCurriculum asier
Curriculum asierlansaco13
 
Ayuda o desayuda
Ayuda o desayudaAyuda o desayuda
Ayuda o desayudavirimedel
 
Xin visa trung quoc
Xin visa trung quocXin visa trung quoc
Xin visa trung quocNguyen Quan
 

Destaque (20)

Medición de la calidad en Programas de Postgrado
Medición de la calidad en Programas de PostgradoMedición de la calidad en Programas de Postgrado
Medición de la calidad en Programas de Postgrado
 
Scala Collections
Scala CollectionsScala Collections
Scala Collections
 
BizTalk練習投影片
BizTalk練習投影片BizTalk練習投影片
BizTalk練習投影片
 
Replant: a new generation of botanical art
Replant: a new generation of botanical artReplant: a new generation of botanical art
Replant: a new generation of botanical art
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Fitnesse With Scala
Fitnesse With ScalaFitnesse With Scala
Fitnesse With Scala
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Xmasquiz
XmasquizXmasquiz
Xmasquiz
 
WP8.1 Tiles and Notifications
WP8.1 Tiles and NotificationsWP8.1 Tiles and Notifications
WP8.1 Tiles and Notifications
 
Jeopardy Christmas
Jeopardy ChristmasJeopardy Christmas
Jeopardy Christmas
 
Christmas Jeopardy
Christmas JeopardyChristmas Jeopardy
Christmas Jeopardy
 
Františkánská zahrada
Františkánská zahradaFrantiškánská zahrada
Františkánská zahrada
 
NP Presentación borrador presupuestos
NP Presentación borrador presupuestosNP Presentación borrador presupuestos
NP Presentación borrador presupuestos
 
Curriculum asier
Curriculum asierCurriculum asier
Curriculum asier
 
Trabajo variedad del español
Trabajo variedad del españolTrabajo variedad del español
Trabajo variedad del español
 
Ayuda o desayuda
Ayuda o desayudaAyuda o desayuda
Ayuda o desayuda
 
Xin visa trung quoc
Xin visa trung quocXin visa trung quoc
Xin visa trung quoc
 
Luis catalogue mvargas
Luis catalogue mvargasLuis catalogue mvargas
Luis catalogue mvargas
 
Lucyana Villas - Unique Tour
Lucyana Villas - Unique TourLucyana Villas - Unique Tour
Lucyana Villas - Unique Tour
 

Semelhante a 如何使用Azure mobile service

Azure mobileservice
Azure mobileservice Azure mobileservice
Azure mobileservice twMVC
 
Windows Azure Developer Day - WAMS
Windows Azure Developer Day - WAMSWindows Azure Developer Day - WAMS
Windows Azure Developer Day - WAMSJeffray Huang
 
Appium手机自动化测试 testerhome_bqcon版本
Appium手机自动化测试 testerhome_bqcon版本Appium手机自动化测试 testerhome_bqcon版本
Appium手机自动化测试 testerhome_bqcon版本延胜 黄
 
中国联通沃商店客户端功能特色
中国联通沃商店客户端功能特色中国联通沃商店客户端功能特色
中国联通沃商店客户端功能特色ocean1110
 
一步一步开发Html5 mobile apps
一步一步开发Html5 mobile apps一步一步开发Html5 mobile apps
一步一步开发Html5 mobile appsAdam Lu
 
行動與雲端科技在圖書館的應用
行動與雲端科技在圖書館的應用行動與雲端科技在圖書館的應用
行動與雲端科技在圖書館的應用皓仁 柯
 
Appcan介绍自己的应用开发平台
Appcan介绍自己的应用开发平台Appcan介绍自己的应用开发平台
Appcan介绍自己的应用开发平台MOBINODE
 
Appcan平台介绍
Appcan平台介绍Appcan平台介绍
Appcan平台介绍36Kr.com
 
新浪微博平台与安全架构
新浪微博平台与安全架构新浪微博平台与安全架构
新浪微博平台与安全架构n716
 
2016 AWS Summit TPE - Hiiir 如何透過 AWS IAM 做好雲端權限控管
2016 AWS Summit TPE - Hiiir 如何透過 AWS IAM 做好雲端權限控管2016 AWS Summit TPE - Hiiir 如何透過 AWS IAM 做好雲端權限控管
2016 AWS Summit TPE - Hiiir 如何透過 AWS IAM 做好雲端權限控管ChiaHsien Lee
 
微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構Jeff Chu
 
Appcan移动应用开发平台介绍120409
Appcan移动应用开发平台介绍120409Appcan移动应用开发平台介绍120409
Appcan移动应用开发平台介绍120409zhengyiwuxian
 
MBaaS
MBaaSMBaaS
MBaaSaikko
 
深入研究雲端應用程式平台-AppFabric
深入研究雲端應用程式平台-AppFabric深入研究雲端應用程式平台-AppFabric
深入研究雲端應用程式平台-AppFabricJohn Chang
 
ForumSentry客戶解決
ForumSentry客戶解決ForumSentry客戶解決
ForumSentry客戶解決Kevin Kao
 
Internet System Security Overview
Internet System Security OverviewInternet System Security Overview
Internet System Security OverviewChinaNetCloud
 
HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型Tom Fan
 
Teched 2012 60分钟构建私有云
Teched 2012 60分钟构建私有云Teched 2012 60分钟构建私有云
Teched 2012 60分钟构建私有云Cheng Zhang
 

Semelhante a 如何使用Azure mobile service (20)

Azure mobileservice
Azure mobileservice Azure mobileservice
Azure mobileservice
 
Windows Azure Developer Day - WAMS
Windows Azure Developer Day - WAMSWindows Azure Developer Day - WAMS
Windows Azure Developer Day - WAMS
 
Appium手机自动化测试 testerhome_bqcon版本
Appium手机自动化测试 testerhome_bqcon版本Appium手机自动化测试 testerhome_bqcon版本
Appium手机自动化测试 testerhome_bqcon版本
 
中国联通沃商店客户端功能特色
中国联通沃商店客户端功能特色中国联通沃商店客户端功能特色
中国联通沃商店客户端功能特色
 
一步一步开发Html5 mobile apps
一步一步开发Html5 mobile apps一步一步开发Html5 mobile apps
一步一步开发Html5 mobile apps
 
行動與雲端科技在圖書館的應用
行動與雲端科技在圖書館的應用行動與雲端科技在圖書館的應用
行動與雲端科技在圖書館的應用
 
Appcan介绍自己的应用开发平台
Appcan介绍自己的应用开发平台Appcan介绍自己的应用开发平台
Appcan介绍自己的应用开发平台
 
Appcan平台介绍
Appcan平台介绍Appcan平台介绍
Appcan平台介绍
 
新浪微博平台与安全架构
新浪微博平台与安全架构新浪微博平台与安全架构
新浪微博平台与安全架构
 
2016 AWS Summit TPE - Hiiir 如何透過 AWS IAM 做好雲端權限控管
2016 AWS Summit TPE - Hiiir 如何透過 AWS IAM 做好雲端權限控管2016 AWS Summit TPE - Hiiir 如何透過 AWS IAM 做好雲端權限控管
2016 AWS Summit TPE - Hiiir 如何透過 AWS IAM 做好雲端權限控管
 
Customer Sharing: Hiiir
Customer Sharing: HiiirCustomer Sharing: Hiiir
Customer Sharing: Hiiir
 
微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構
 
Appcan移动应用开发平台介绍120409
Appcan移动应用开发平台介绍120409Appcan移动应用开发平台介绍120409
Appcan移动应用开发平台介绍120409
 
MBaaS
MBaaSMBaaS
MBaaS
 
滙聚資訊科技業務簡介v1.1
滙聚資訊科技業務簡介v1.1滙聚資訊科技業務簡介v1.1
滙聚資訊科技業務簡介v1.1
 
深入研究雲端應用程式平台-AppFabric
深入研究雲端應用程式平台-AppFabric深入研究雲端應用程式平台-AppFabric
深入研究雲端應用程式平台-AppFabric
 
ForumSentry客戶解決
ForumSentry客戶解決ForumSentry客戶解決
ForumSentry客戶解決
 
Internet System Security Overview
Internet System Security OverviewInternet System Security Overview
Internet System Security Overview
 
HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型
 
Teched 2012 60分钟构建私有云
Teched 2012 60分钟构建私有云Teched 2012 60分钟构建私有云
Teched 2012 60分钟构建私有云
 

如何使用Azure mobile service

  • 1. 如何使用 Azure Mobile Services - Windows Phone為例 Pou 2013.11.30 http://www.windowsazure.com/en-us/develop/mobile/?fb=zh-tw
  • 2. 大綱 • Mobile Service概述 • 功能說明與練習 • Data • Push Notification • User Auth • Server Script • Custom API •…
  • 6. Mobile Service在做什麼 • 為 Windows、Android 及 iOS 應用程式 • 建立可擴充的安全後端 • 在雲端儲存資料 • 輕鬆驗證使用者 • 傳送推播通知 • 取用偏好的服務 • 監視、警示及自動擴充 (預覽)
  • 7. Mobile Service的好處 • 減少開撰寫維護雲端程式 • 專心在App端開發 • RESTful API支持多個平台使用 • Windows Store/Phone、Android、iOS、HTML • 提供App開發的基礎建設 • 彈性擴充 • 資料儲存、身份驗證、推播通知 • 官方提供了主流平台的SDK
  • 8. Mobile Service重點功能 • DATA • User Authorization • Push Notification • Server Script • Scale、Logging、Diagnastics
  • 9. DATA • 提供存取 Windows Azure 的儲存體 • SQL 資料庫 • Blob 儲存體 • 資料表儲存體 • 協力廠商資料服務,如 Mongo DB 等 • Mobile Service和 Service Bus Transfer • 提供了連接到內部部署資料庫的選項 • 敏感性資料未儲存在雲端亦可供行動裝置存取
  • 10. USER AUTH • 提供第三方驗證服務 • Facebook、Twitter、Microsoft、Google • 得到被賦予的應用程式識別碼和機密 • 免除自行開發、設定和測試驗證系統的需求 • 只需撰寫幾行程式碼就可以驗證您的使用
  • 11. PUSH NOTIFICATION • 支援Windows Store / Windows Phone • 更支持 • iOS • 僅需上傳特定的開發人員憑證 • Android • 登入GCM識別碼 • 輸入簡單指令即可完成 • push.apns.send (iOS 平台) • push.gcm.send (Android 平台)
  • 12. SCRIPTS • 撰寫簡單的伺服器端程式碼 • 能以自訂邏輯強化資料作業 • 自定排程、資料彙整、圖像處理 • 公開API支持整合其他服務 • 推播通知、簡訊或電子郵件 • 連接至其他 Windows Azure 服務 • Azure市集提供的附加元件 • 任何協力廠商服務
  • 13. 監視、警示及自動擴充 • 健全狀況計量儀表板 • 監視應用程式的健全狀況與可用性 • 設定警示規則 • 可用性偏低時您便會接獲通知 • 自動擴充規則 • 依照當前的需求自動擴充或縮編 • 以盡量降低成本 • 高可用性服務等級協定的安心保障
  • 17. 實作說明 • Get Start • 操作Data (Server & Client) • 操作Push Notification • 操作User Authorization • 熟悉Server Scripts
  • 18. 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
  • 21. 建立Mobile Service – 2/5 輸入想要的名字 選擇東亞
  • 22. 建立Mobile Service – 3/5 設定登入的帳密
  • 23. 建立Mobile Service – 4/5 輸入想要的定序 http://www.dotblogs.com.tw/jimmyyu/archive/2009/08/30/10320.aspx
  • 24. 建立Mobile Service – 5/5 Mobile Service
  • 28. 加入程式碼 – 1/2 • 直接使用範例程式下載
  • 29. 加入程式碼 – 2/2 • 加入既有專案 更換自行建立的Mobile Service
  • 30. 執行結果 todoTable.InsertAsync(todoItem) items = await todoTable .Where(todoItem => todoItem.Complete == false) .ToCollectionAsync(); await todoTable.UpdateAsync(item)
  • 31. 重點提醒 • 程式端必要元素 • Microsoft.WindowsAzure.MobileServices • MobileServiceClient • 請求與交易Mobile Service的處理器 • 設定Mobile Service位置 • 授權Key • IMobileServiceTable • 物件化操作Mobile Service資料表資料 • MobileServiceCollection<TTable, TCol> • 作用於databinding to list
  • 33. Validate and modify data • Server Script增加字串長度限制 • 更新Client端來支持驗證 • Server script增加timestamp完成新增 • 利用timestamp更新Client端呈現的資料
  • 36. 修改Client端程式碼 • MobileServiceInvalidOperationException • Sever Script增加的驗證條件 • 既有的Client執行會遇到Exception
  • 46. 重點提醒 • Server Scripts • CRUD過程裡程序化自訂邏輯加強功能 • 動態Schemas • Mobile Service預設開啟 • Server Script與Client端均可動態調整 • 建議二邊一致 • Client • 熟悉IMobileServiceTable物件的操作
  • 48. 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
  • 49. Windows Push Notification • Concept channel channel
  • 50. Microsoft Push Notification Service • MPNS • 每天的推播數超過500個訊息 • 需註冊MPNS channel channel
  • 51. Google Cloud Messaging • Need google account to Enable GCM • App get GCM channel • App Server send message to GCM by the channel
  • 52. Apple Push Notifications • Provider keep device token of the app • Send message to APNs by the device token
  • 53. Push Notification - Mobile Service • Concept Send message Provider Forward to target devices WNS APNs MPNS GCM
  • 54. Get Start for Push Notification • Server Script接收新增用戶時一併發送推送 • 以Windows Phone為例 • 同時發送給多個設備 • • • • 增加新table Client端增加新的類別 Client取得channel加入新table Server Script • 在新table加入新channel時加上檢查 • 新增項目時,加上發送推送
  • 56. Server Script新增並推送 – 2/3 • For Windows Phone Tile update
  • 57. Server Script新增並推送 – 3/3 • For GCM message
  • 61. Mobile Service推送其他平台 – 1/2 • Windows Store • http://www.windowsazure.com/enus/develop/mobile/tutorials/get-started-with-pushdotnet/?fb=zh-tw • Android • http://www.windowsazure.com/enus/develop/mobile/tutorials/get-started-with-pushandroid/?fb=zh-tw • message,from,collection-key • iOS • http://www.windowsazure.com/enus/develop/mobile/tutorials/push-notifications-tousers-wp8/?fb=zh-tw
  • 63. 同時推送給多個設備 – 1/5 • 建立新的Table:Channel
  • 64. 同時推送給多個設備 – 2/5 • Client增加對應Channel的類別 • Id • 建立新Table後自動產生的識別欄位
  • 65. 同時推送給多個設備 – 3/5 • Client取得Chanel,寫回Channel資料表
  • 66. 同時推送給多個設備 – 4/5 • Server Script增加Channel新增時的檢查 • 執行insertChannelIfNotFound • 參數有值代表重覆;無值直接新增
  • 68. 重點提醒 • Sever Script • 針對發送平台具有不同指令 • 註冊Channel需做重覆檢查 • Android / iOS獨立需具備的要素 • 憑證、Google Console API • MPNs推送類型有Tile、Toast
  • 69. User AUTH how to authenticate users in Windows Azure Mobile Services from your app
  • 70. User Auth原理 – 1/3 • 整合各種驗證用戶機制 • Microsoft Account • Facebook login • Twitter login • Google login • 減少需獨立開發帳號管理 • 減少個資外洩 • 無需負擔帳號成本
  • 71. User Auth原理 – 2/3 auth Microsoft Account Request authorization Client Application Return token token auth auth Transaction by the token auth Facebook login Twitter login Google logic
  • 72. 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
  • 73. Get started with authentication • Facebook login • Google logic • Microsoft Account • http://www.windowsazure.com/en-us/develop/mobile/tutorials/getstarted-with-users-wp8/?fb=zh-tw • http://www.windowsazure.com/en-us/develop/mobile/how-toguides/register-for-microsoft-authentication/?fb=zh-tw • Twitter login • http://www.windowsazure.com/en-us/develop/mobile/how-toguides/register-for-twitter-authentication/?fb=zh-tw
  • 75. 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
  • 78. Facebook login – 4/5 記下APP ID與APP Secret 需回填至Mobile Service 貼上Mobile Service 所產生的URL
  • 79. Facebook login – 5/5 填入APP ID與APP Secret
  • 81. 修改Client端程式碼 – 1/2 • 直接執行會收到Exception • status code of 401 (Unauthorized) • 增加需進行身份驗證 • 獨立變數保存用戶登入狀態 • MobileServiceUser • 因需要用戶輸入,建議放在第一個畫面
  • 83. Google login – 1/4 • 登入Google apis • 選擇APIs & auth • 選擇Registered apps • 輸入建立Name • 設定為Web application • 輸入Mobile Service所產生的網址 • 取得建立的 • Client ID、Client Secret
  • 86. Google login – 3/4 記下APP ID與APP Secret 需回填至Mobile Service 貼上Mobile Service 所產生的URL
  • 89. 重點提醒 • 依驗證身份提供者類型 • 將Mobile Service的URL加以註冊 • 取得提供者API Key • 將提供者提供的Key加入Mobile Service • 調整資料表需有通過驗證才可以存取 • Client增加呼叫驗證的方式 • 需保存用戶資訊
  • 91. 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 (完整指令教學)
  • 92. Server Script – 2/3 Script apns object gcm object push object request object response object service object mpns object mssql object update function delete function insert function read function statusCodes object table object user object wns object query object console object
  • 93. Server Script – 3/3 • Table operations • Custom API • Job Scheduler • Using Command line tool (教學) • Working with tables • Debugging and troubleshooting
  • 95. Table operations – 1/4 • 專註冊於處理tables上的CRUD • 其方法名稱需對應CRUD運作的一種 • insert • update • delete • read • 一個table僅能註冊一個operations script • 經由每次REST request時,被執行一次
  • 97. 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的內容
  • 98. 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) { … }
  • 99. 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
  • 100. Register for table operations – 1/2 • Azure Management Portal • 定義Mobile Service每個tables的opertaions • Source Control • 建立<table>.<opertaion>.js • 放置.servertable的子目錄 • 執行時,自動取得該js進行 • Common line tool
  • 101. 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.
  • 102. Override the default response • 實作自訂邏輯,修改成功回傳內容 • 例如:檢查值、確認符合條件等 For more examples, see Modify the response.
  • 103. Override execute success • 預設table operation呼叫execute() • 自動回傳response • Override • 可傳入二個參數,定義success或error
  • 104. Override default error handling • 實作自訂邏輯,修改失敗回傳內容 • 例如:儲存失敗、指令錯誤等 • 預設 • server script產生log • 產生error result至response • 自訂
  • 105. Access custom parameters – 1/2 • 請求Mobile Service可夾帶其他參數 • 利用URI的Query String:URI?key=value • 例如 • 傳遞的參數 • duplicateText = false • Custom parameters • 轉換成JSON物件後,放置request object的 「parameters」屬性
  • 106. Access custom parameters – 2/2 利用Request object 的parameters屬性 對table進行where查詢,成 功指定給定義好的function
  • 107. Work with users • Mobile Service提供身份驗證功能 • 當驗證用戶執行table operations時 • user object • 出現於operations的參數之中 • user object.userid • userid屬性可用於儲存與擷取用戶個別設定資訊
  • 109. 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>
  • 110. 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
  • 111. Define a custom API – 1/2 可依照不同的HTTP Method開放操作權限
  • 112. Define a custom API – 2/2 • 每一個HTTP Method均會有request / response • {}操作的回應項目,來自express.js library • 回傳格式為JSON
  • 113. Implement HTTP methods • Standard 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) { ... }; • 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.
  • 114. Send and receive data as XML • Mobile Service使用JSON呈現資料給Client • 想用XML的結構傳遞 • 相關範例 • Define a custom API that supports periodic notifications
  • 115. 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資料型態
  • 116. Work with users and headers in a custom API – 2/3 • 取得user的table • 藉由指定user.userid來搜尋對應的資料
  • 117. 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
  • 118. 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/add https://<service>.azure-mobile.net/api/calculator/sub
  • 119. 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
  • 120. Call a custom API from the client • 範例練習 • http://www.windowsazure.com/enus/develop/mobile/tutorials/call-custom-apiwp8/?fb=zh-tw
  • 122. Job Scheduler • 定義週期或on-demand的server script任務 • 常用於定期的任務 • 例如 • 清除表格資料、批量處理 • 藉由 Schedule jobs 操作 • 註冊 schedule job 其main function • 為schedule job name • 非HTTP Method • 無法傳入參數,由job自行處理 • 可自訂子function加以處理
  • 123. Define scheduled job scripts – 1/2 週期與on-demand
  • 124. Define scheduled job scripts – 2/2 與job name相同的main function為程式進入點
  • 125. Schedule recurring jobs in Mobile Services • 範例練習 • Schedule recurring jobs in Mobile Services • http://www.windowsazure.com/enus/develop/mobile/tutorials/schedule-backendtasks/?fb=zh-tw • 結合Twitter進行取得更新資料
  • 127. Source control, share code, … - 1/3 • Mobile Service使用Node.js • 自訂的script,直接被加入Node.js的module • 藉由source control • 自訂module • 新增其他Node.js至Service中
  • 128. 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
  • 129. 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
  • 130. Use helper functions – 1/2 • helper function • 需宣告在main function之後 • 需要的參數均要先全部定義好 • 寫在同一個script裡,屬於區域方法
  • 131. Use helper functions – 2/2 • 定義一次性與分享於其他server script • 需定義於 export object • .servicesshared:分享於不同server script
  • 132. Work with app settings – 1/3 • Mobile Service支持安全性儲存設置值 • 類別 app settings • 支持server script runtime時可使用 • App settings • name/value pairs • 儲存時均會加密
  • 133. Work with app settings – 2/3 • Custom API • 藉由 service object 擷取app settings
  • 134. Work with app settings – 3/3 • 使用configuration module
  • 136. Working with tables – 1/2 • 大部分情境中的Server Script會操作tables • 例如 • Mobile Service不具有狀態,如需保存則需tables • 搭配權限控制request存取的能力 • 二大方式連接tables • table object • 簡易式操作資料表資訊 • mssql object • 類似用T-SQL指令方式 • 提供更彈性與複雜的指令
  • 137. Working with tables – 2/2 • Access tables from scripts • Perform Bulk Insert • Map JSON types to database types • Using Transact-SQL to access tables
  • 138. Access tables from scripts – 1/3 • table object • getTable function取得指定資料表物件 • 指令於不同的模組用法不同 • table operations / schedule jobs • custom api • 在shared function不可使用table object • 要用參數傳入
  • 139. Access tables from scripts – 2/3 • 取得table object可以操作多次
  • 140. Access tables from scripts – 3/3 • 更新成功後,執行help function • 藉由物件新增其他資料表內容
  • 141. Perform Bulk Insert – 1/2 • 情境 • 如果需要大量新增資料 • Client直接用for 或 while來請求insert • 將受到SQL connection limit 的錯誤回傳 • 導向Client無法完成請求 • 或收到HTTP 500 Internal Server Error • 解法 • 採用batch的方式 • 例如:100筆資料分10筆一次來傳送
  • 142. Perform Bulk Insert – 2/2 定義Batch參數 定義成功處理下一筆 Error Handler 定義迴圈方法 第一次啟動
  • 143. 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
  • 144. Map JSON types to database types – 2/2 • JavaScirpt objects與T-SQL type類型對應表 JavaScript property Number Boolean Date String Buffer Object Array Stream T-SQL type Float(53) Bit DateTimeOffset(3) Nvarchar(max) Not supported Not supported Not supported Not supported
  • 145. 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
  • 146. 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
  • 147. Run a static query • 執行一個固定沒有傳入參數的查詢 • 回傳為JSON格式
  • 148. Run a dynamic parameterized query • 執行時夾帶參數,搭配User Auth進行
  • 149. Join relational tables – 1/2 • 使用mssql object進行tables的join • 舉例二個table • ToDoItem { text: 'Take out the trash', complete: false, priority: 1} • priority外部參考Priority table的number • Priority { number: 1, description: 'Critical'}
  • 150. Join relational tables – 2/2 • JOIN二個資料表 • 結果 { text: 'Take out the trash', complete: false, description: 'Critical'}
  • 151. Run a query that returns raw results – 1/2 • queryRaw • 回傳查詢的結果為原始的資料 • 結果資料表結構 • 每一個Row / Column的結構
  • 152. Run a query that returns raw results – 2/2 回傳資料表結構 Row/Column 資料對應
  • 153. Get access to a database connection • 使用open方法取得database connection • 使用情境 • 執行transaction時 • connection object: close, queryRaw, query, beginTransaction, commit, a nd rollback.
  • 154. Debugging and trouble shopping • Write output to the mobile service logs • 追bug最主要的方法 • 預設server script發生錯誤會產生logs • 自定義邏輯利用console object寫入log • %j:代表JSON物件 • 提醒 • logs請小心使用以免超載
  • 157. Mobile Service重點提醒 • 提供整合Push Notification介面 • 結合多種User AUTH機制減少開發 • 支持Client與Server端便利資料操作 • Server Scripts加強複雜的邏輯情境 • Custom API擴充自訂功能 • Job Schedule、Source Control • 複雜的資料表操作 • 管理功能
  • 158. Q&A
  • 159. Thank you for listening.
  • 160. References – 1/2 • Work with server scripts in Mobile Services • Schedule recurring jobs in Mobile Services • Send email from Mobile Services with SendGrid • Upload images to Windows Azure Storage by using Mobile Services
  • 161. 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