SlideShare une entreprise Scribd logo
1  sur  126
Télécharger pour lire hors ligne
Microsoft Azure Mobile Services
Paolo Salvatori
Microsoft
Paolo Salvatori
Senior Program Manager
Microsoft
Agenda
▪ 1st Hour: Introduction to Mobile Services (45 minutes)
▪ General Architecture
▪ Management Portal
▪ REST API
▪ Break (15 minutes)
Enterprises want to manage internal
mobile apps and consumer facing event-
or product-specific mobile apps from the
same portal as core LOB apps
Small Businesses require solutions that
accelerate development time and
decrease development costs.
Developers shouldn’t have to constantly
reinvent the wheel and reproduce
common backend functionality


Consumers expect a continuous
experience across all devices
App Development

Challenges
Windows Azure Mobile Services
accelerates connected client application
development by streamlining common
backend tasks like structuring storage,
authenticating users, and sending push
notifications.
3
Connected Apps
Windows Store
iOS
Android
Windows Phone 8
iOS
Android
HTML 5/JS
Data in the Cloud
SQL Table Blob
User Authentication
Facebook Twitter Microsoft Google
Push Notifications

WNS & APNS GCM
MPNS
SDKs
Server-
Side
Scripts
&
Scheduler
Basic Features
Supported Platforms
iOSWindows 8
Windows
Phone 8 Android
HTML5 &
Javascript
Architecture
Portal
Zumo
Resource
Provider
Zumo
Mgmt DB Site 1
Zumo
Runtime
SQL 1
Scheduler
RDFE
Zumo
Antares
Stamp
Site 2 Site N
Azure
SDK
3rd party
SDKS
SQL 2 SQL N
AzureLBARR
App
Scripts
Win8
Device
iPhone
Management Portal
Dashboard
▪ Quick glance
▪ Displays relevant mobile service
information, including the service URL,
status, location, database and subscription
▪ Usage Timeline:
▪ Displays a scalable chart of CPU time, API
calls and data reads over a specific period
of time.
▪ Usage Overview:
▪ Displays an overview of the total compute
time and storage used by this mobile
service
▪ Manage Keys
▪ Manage Keys
▪ Delete
▪ Delete the Mobile Service
Data
▪ In Windows Azure Mobile Services, data is
stored in tables, which are maintained in the
Windows Azure SQL Database that is
associated with your mobile service.
▪ Click the Create button to add a new table to
your mobile service.
▪ By default, you don’t need to predefine the
schema of tables in your database.
▪ Mobile Services automatically add columns
to a table based on the data you insert. To
change this dynamic schema behavior, use
the Dynamic Schema setting on the
Configure tab.
▪ To browse data, manage permissions and
indexes on tables, or add server scripts,
click on a table in the list.
▪ To delete a table, click to select a table row,
and then click Delete.
Browse
▪ The Browse tab lets you
browse data rows in a
selected table.
▪ Click the forward and back
arrows to navigate through
multiple pages of data.
▪ Click on table names in the left
navigation bar to switch
between tables.
▪ Click on the large left arrow to
go back to the Data tab.
Script
▪ Mobile Services enables you to
register JavaScript code to be
executed by the service when an
insert, update, delete, or read
operation occurs against a specific
table.
▪ For each table there are 4 CRUD
operations (insert, read, update, del)
▪ These scripts provide the ability to
inject business logic into table
operation, stop an operation from
taking place, change or enrich values
supplied to the operation or change
the results.
▪ For authenticated clients, you can also
perform user-specific authorizations
based on the userId value of the
supplied user object.
Columns
▪ To delete columns, click the column and then
click Delete.
▪ To add additional columns to the table,
simply send an insert request including the
new properties from your app with dynamic
schema enabled.
▪ Once a column is created, its data type
cannot be changed by Mobile Services.
▪ Insert or update operations fail when the type
of a property in the JSON object cannot be
converted to the type of the equivalent
column in the table.
▪ You can improve the performance of queries
by defining an index on the table.
▪ To add a column to the index, select the
column and click Set as Index.
Permissions
▪ Mobile Services enables you to set the
following permissions on table operations:
▪ Everyone: request for the operation against the table
is accepted. This option leaves your data wide-open
for everyone to access.
▪ Anybody with the Application Key: The application
key is required to perform the operation. The
application key is distributed with the application.
Note: Because this key is not securely distributed, it
cannot be considered a security token. To secure
access to your mobile service data, you must
instead authenticate users before accessing.
▪ Only Authenticated Users: Only authenticated users
are permitted to perform the operation. Scripts can
be used to further restrict access to tables based on
an authenticated user.
▪ Only Scripts and Admins: The operation requires the
service master key, which limits the operation only to
registered scripts or to administrator accounts
Scheduler
▪ Mobile Services enables you to
register JavaScript code to be
executed by the service either on
a schedule that you define or on-
demand from the Management
Portal.
▪ Removing duplicate records from a
table.
▪ Backing up a database.
▪ Send a report via email.
▪ When your mobile service runs in
free mode, you can create only
one scheduled job at a time; in
reserved mode, you can create
up to ten scheduled jobs at the
same time.
Push
▪ Mobile Services can send push notifications to
your apps.
▪ The way that you configure and send push
notifications depends on your app.
▪ You can send push notifications to the following
applications:
▪ Windows Store app by using the Windows Push
Notification Service (WNS). To send push notifications to
your app, you must configure your mobile service to work
with WNS.
▪ iPhone and iPad apps by using the Apple Push
Notification Service (APNS). To send push notifications to
your app, you must configure your mobile service to work
with APNS. Do this by generating a special push
notification certificate at the iOS Provisioning Portal
▪ Android: You can send push notifications to Android
apps by using the Google Cloud Messaging (GCM)
service. To send push notifications to your app, you must
configure your mobile service to work with GCM.
▪ Windows Phone 8 app by using the Microsoft Push
Notification Service (MPNS). Mobile Services does not
require you to register your Windows Phone 8 app to
send push notifications.
Identity
▪ Mobile Services integrates with the
following identity providers to make it easy
to authenticate users from your app:
▪ Microsoft Account
▪ Facebook login
▪ Twitter login
▪ Google login
▪ To enable authentication, you must first
register your app with one or more of
these identity providers and then configure
your mobile service.
▪ You must also add authentication code to
your app.
▪ When a user login is authenticated by
Mobile Services, the value of the userId
property on the user object passed to
server scripts is set to a value that
uniquely identifies a user. This value can
then be used to authorize access to data.
Configure
▪ You can directly access to the
database used by your mobile
service just clicking the link.
▪ You can change the database
used by your mobile service to
store data with a new or
existing DB by clicking Change
DB and select one of the
following options.
▪ Enable/disable dynamic
schema.
▪ In reserved mode, you can
configure endpoint monitoring.
Scale
▪ On the Scale tab, you can scale
your mobile service by switching
to reserved mode and adding
role instances.
▪ You can also scale the Windows
Azure SQL Database instance
used by the service.
▪ Note: When you set the Mobile service
mode to Reserved, all of your mobile
services in the same region run in
Reserved mode.
▪ To estimate the cost required to
scale your mobile service, see
the Mobile Services Pricing
Calculator.
Log
▪ Mobile Services writes system and
script errors to the log.
▪ Click on a log entry to view details
and copy to the clipboard.
▪ Click the link in the Source column
to directly jump to the script that
generated the error.
▪ Note: Existing log entries can be viewed in
the Management Portal for 7 days.
▪ Your registered server scripts can
also write information directly to
the log by using the console
object.
REST API
REST API
Operation Description
Login Gets the user ID for a supplied authentication token.
Query
records
Queries data in a table.
Insert record Inserts a new record into a table.
Update
record
Updates an existing record in a table.
Delete record Deletes an existing record from a table.
Login
▪ Verb: POST
▪ URL: https://<service-name>.azure-mobile.net/login?mode=authenticationToken
▪ Uri Parameter: mode
▪ Request Headers:





▪ Body: JSON object

{"authenticationToken":“<authentication-provider-token>"} for Microsoft

{“access_token":“<authentication-provider-token>"} for Facebook and Google
▪ Response: JSON object

{"user":{"userId":"<authentication-provider>:cb392ceae24a44408ed863482b20f274"},

"authenticationToken":"<mobile-service-authentication-token>"}
Request Header Required Value
Accept No Set this header to application/json.
Content-Length Yes The length of the request body.
Content-Type No Set this header to application/json.
Query Records
▪ Verb: GET
▪ URL: https://<service_name>.azure-mobile.net/tables/<table_name>
▪ URI Parameters: $filter, $inlinecount, $orderby, $select, $skip, $top
▪ Request Headers:







▪ Response: data in JSON format
▪ Samples:
▪ GET https://plunko.azure-mobile.net/tables/todoitem
▪ GET https://plunko.azure-mobile.net/tables/todoitem/1
▪ GET https://plunko.azure-mobile.net/tables/todoitem?$filter=(complete%20eq%20false)
▪ GET https://plunko.azure-mobile.net/tables/todoitem?$filter=(complete%20eq%20false)&$orderby=text%20asc
Request Header Required Value
Accept No Set this header to application/json.
X-zumo-application Conditional
The application key of the mobile service. You must specify a valid application
key when required to access the table operation. This is the default table
operation access permission.
X-zumo-auth Conditional
The service-generated authentication token for an authenticated user. You must
specify a token for an authenticated user when required to access the table
operation.
Insert Record
▪ Verb: POST
▪ URL: https://<service_name>.azure-mobile.net/tables/<table_name>
▪ Request Headers:







▪ Body: new record in JSON format
▪ Response: The JSON representation of the inserted item, which includes the ID for the item
generated by the mobile service.
▪ Sample:
▪ POST https://plunko.azure-mobile.net/tables/todoitem
▪ Body: {"text" : "Go to Las Vegas", "complete": false, "when" : "don't know"}
Request Header Required Value
Accept No Set this header to application/json.
Content-Length Yes The length of the request body.
Content-Type No Set this header to application/json.
X-zumo-application Conditional The application key of the mobile service.
X-zumo-auth Conditional The service-generated authentication token for an authenticated user.
Update Record
▪ Verb: PATCH
▪ URL: https://<service_name>.azure-mobile.net/tables/<table_name>/<item_id>
▪ Request Headers:







▪ Body: The updated values encoded in JSON format, along with the optional id value of the
object.
▪ Response: The JSON representation of the updated item
▪ Sample:
▪ PATCH https://plunko.azure-mobile.net/tables/todoitem/1
▪ Body: {"text" : "Go to Barcelona", "complete": false, "when" : "don't know"}
Request Header Required Value
Accept No Set this header to application/json.
Content-Length Yes The length of the request body.
Content-Type No Set this header to application/json.
X-zumo-application Conditional The application key of the mobile service.
X-zumo-auth Conditional The service-generated authentication token for an authenticated user.
Delete Record
▪ Verb: DELETE
▪ URL: https://<service_name>.azure-mobile.net/tables/<table_name>/<item_id>
▪ Request Headers:







▪ Body: none
▪ Response: none
▪ Sample:
▪ DELETE https://plunko.azure-mobile.net/tables/todoitem/6
Request Header Required Value
X-zumo-application Conditional The application key of the mobile service.
X-zumo-auth Conditional The service-generated authentication token for an authenticated user.
HttpClient and Mobile Services
private async void btnSend_Click(object sender, EventArgs e)
{
try
{
var httpClient = new HttpClient();
var uri = new Uri("https://<your-mobile-service>.azure-mobile.net/tables/TodoItem");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add("x-zumo-application", “<application-key>");
httpClient.DefaultRequestHeaders.Host = uri.Host;
var todoItem = new TodoItem {Text = txtText.Text, Complete = false};
var serializer = new DataContractJsonSerializer(typeof (TodoItem));
using (var stream = new MemoryStream())
{
serializer.WriteObject(stream, todoItem);
stream.Seek(0, SeekOrigin.Begin);
//use a Stream reader to construct the StringContent (Json)
using (var reader = new StreamReader(stream))
{
var response = await httpClient.PostAsync(uri, new StringContent(reader.ReadToEnd(),
Encoding.UTF8, "application/json"));
WriteToLog(response.IsSuccessStatusCode
? string.Format("Message <{0}> successfully inserted!", txtText.Text)
: string.Format("HTTP Status: <{0}> Reason: <{1}>", response.StatusCode,
response.ReasonPhrase));
}
}
}
catch (Exception ex)
{
WriteToLog(ex.Message);
}
}
Break
Agenda
▪ 2nd Hour: Server Scripts (45 minutes)
▪ Server script objects
▪ Server script modules
▪ Configure user authentication:
▪ Windows Live
▪ Facebook
▪ Twitter
▪ Google
▪ User object and integration with identity providers
▪ Data validation and duplicate detection
▪ Administer Mobile Services using Windows Azure Cross-Platform Command Line tool
▪ Break (15 minutes)
Server Script Objects
Objects
Name Description
apns Provide functionality to send notifications to Apple Push Notification Service (APNS)
console Write information to Services logs
gcm Provide functionality to send notifications to Google Cloud Messaging (GCM)
mpns Provide functionality to send notifications to Microsoft Push Notification Service (MPNS)
mssql
Provides functionality for working directly with tables in the SQL Database using Transact-
SQL
push Provides access to objects that are used for sending push notifications to apps
Query Provides functionality for querying Windows Azure Mobile Services
Request Used to execute the requested operation and determine the response to the client
statusCodes
Enumerates the supported HTTP status codes can be returned by Windows Azure Mobile
Services
Table
Provides functionality for working with specific tables (insert, del, update, where, take,
select, etc.)
tables Provides functionality for working with specific tables as a Table object instance
Samples
console object
console.log("Inserting item '%j'.", item); // %j for 'JSON'
console.error("Operation failed: ", error);
mssql object
mssql.query('select * from orders where country = "Ukraine"', {

success: function (results) {

console.log(results);

}

});
Samples
Query object: is passed to read scripts
function read(query, user, request) {
query.where({ userId: user.userId });
request.execute();
}
Request object: is passed to all scripts
request.execute(); //Executes the default behavior for the
operation
request.respond(); //Writes the default response
request.respond(error); //Writes a response with the specified
error
Samples
Tables and Table objects
function insert(item, user, request) {
var addressTable = tables.getTable('address');
addressTable.where({userId: user.userId})
.read({success: function (results) {
item.address = results.length > 0 ?
results[0] :
'unknown';
request.execute(); }
});
}
Server Script Modules
Modules
Name Description
azure Provides functionality to access Azure services (Blobs, Tables, Service Bus)
request Sends HTTP requests to external web services, such as Twitter and Facebook.
sendgrid Used to send email by using the Sendgrid email service.
util Provides useful methods like format, isDate, isArray, isError
xml2js XML to JavaScript object converter
js2xml JavaScript to XML object converter
xmlbuilder Can be used to create XML documents
sax A very simple tool to parse through an XML string
Samples
Request
var request = require('request');
var url = 'https://api.twitter.com/1/users/show.json?
screen_name=babosbird';
request(url, function(error, response, body) {
if (error) {
console.error(error);
} else {
var data = JSON.parse(body);
console.log(data);
}
});
Samples
SendGrid
function sendEmail(text) {
var sendgrid = new SendGrid('<user-name>', ('<password>');
sendgrid.send({
to: 'paolos@microsoft.com',
from: ‘info@acme.com',
subject: ‘ACME Newsletter',
text: text
}, function (success, message) {
// If the email failed to send, log it as an error so we can
investigate
if (!success) {
console.error(message);
}
});
}
Client Libraries
Portable Class Library
▪ The Portable Class Library project supports a subset of assemblies from the .NET
Framework, Silverlight, .NET for Windows Store apps, Windows Phone, and Xbox
360, and provides a Visual Studio template that you can use to build assemblies
that run without modification on these platforms.
▪ Mobile Services support the Portable Class Library model and this offers several
advantages for developers, since they can now share the codebase of client
applications between the different platforms.
▪ The Portable Class Library needs a platform extension assembly (.Ext.dll) to work
correctly.
▪ We provide platform extension assemblies for:
▪ Windows Phone 7.5
▪ Windows Phone 8
▪ Windows Store apps.
Portable Class Library
▪ The Portable Class Library cannot contain all the classes of the managed clients for all the platforms.
▪ Some functionality is only available on one platform, while other functionality differs between platforms.
▪ Therefore, to be able to use the new Managed Client you also need a platform specific assembly,
which contains code to be able to use features available on that specific platform.
▪ UI authentication is an example of a feature that works different on different platforms and is not
portable.
▪ For you as a developer this means you need two assemblies for your Windows Store App to use
Mobile Services.
▪ For Windows Phone there is an additional assembly which contains functionality which is not allowed
in background agents (e.g. login)
Windows Store App
App.xaml.cs
using Microsoft.WindowsAzure.MobileServices;
sealed partial class App : Application
{
// This MobileServiceClient must be configured to communicate with
// your Mobile Service's url and application key.
// The MobileServiceClient class provides basic access to Mobile
Services.
public static readonly MobileServiceClient MobileService =
new MobileServiceClient("https://<your-mobile-service>.azure-
mobile.net/", “XXXXXXXXXXXXXXXXXXXX");
}
Windows Store App
MainPage.xaml.cs
using Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json;
public class TodoItem
{
public int Id { get; set; }
[JsonProperty(PropertyName = "text")]
public string Text { get; set; }
[JsonProperty(PropertyName = "complete")]
public bool Complete { get; set; }
}
public sealed partial class MainPage : Page
{
private MobileServiceCollection<TodoItem, TodoItem> items;
// the IMobileServiceTable interface Provides operations on a table for a Mobile Service.
private IMobileServiceTable<TodoItem> todoTable = App.MobileService.GetTable<TodoItem>();
private async void RefreshTodoItems()
{
// This code refreshes the entries in the list view by querying the TodoItems
table.
items = await todoTable
.Where(todoItem => todoItem.Complete == false)
.ToCollectionAsync();
}
}
HttpMessageHandlers
▪In the new version of the managed client,
HttpMessageHandlers replace ServiceFilters.
▪The MobileServiceClient internally uses an instance of
the HttpClient to send HTTP requests and receive HTTP
responses to Mobile Services REST services.
▪In a managed client, HttpMessageHandlers can be
used to intercept and handle outgoing requests to
Mobile Services.
▪You can define one or more handlers for a a single
MobileServiceClient.
Sample 1
public class BusyHandler : DelegatingHandler
{
private int callCount;
private readonly Action<bool> busyIndicator;
public BusyHandler(Action<bool> busyIndicator)
{
this.busyIndicator = busyIndicator;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var outgoingCount = Interlocked.Increment(ref callCount);
if (outgoingCount == 1)
{
busyIndicator(true);
}
var response = await base.SendAsync(request, cancellationToken);
var incomingCount = Interlocked.Decrement(ref callCount);
if (incomingCount == 0)
{
busyIndicator(false);
}
return response;
}
}
Sample 2
public class DebugHandler : DelegatingHandler
{
// Sends an HTTP request to the inner handler to send the request to the server as an async
operation
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken
cancellationToken)
{
if (request != null && request.Method == HttpMethod.Post && request.Content != null)
{
Debug.WriteLine("REQUEST: {0}", await request.Content.ReadAsStringAsync());
}
var response = await base.SendAsync(request, cancellationToken);
if (response != null && response.Content != null)
{
Debug.WriteLine("RESPONSE: {0}", await response.Content.ReadAsStringAsync());
}
return response;
}
}
HTML 5 Client
HTML Page: inset the following script references
<script src='//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js'></script>
<script src='https://<your-service-name>.azure-mobile.net/client/MobileServices.Web-1.0.0.min.js'></script>
JavaScript File
// In the editor, open or create a JavaScript file, and add the following code that defines the 

// MobileServiceClient variable. Supply the application URL and application key from the mobile service in
// the MobileServiceClient constructor.
var client = new WindowsAzure.MobileServiceClient('https://<your-service-name>.azure-mobile.net/', ‘AppKey')
// You get a reference to a table by calling the getTable() function on the MobileServiceClient object
var todoItemTable = client.getTable('todoitem');
function refreshTodoItems() {
// Filter data by including a where clause in a query that returns all items whose complete field is equal
to false
var query = todoItemTable.where({ complete: false });
// Reads all data from the table
query.read().then(function(todoItems) {
// Use jQuery and JavaScript to update controls on the page.
});
}
Authentication
Authentication
▪ Mobile Services integrates with the
following identity providers to make it easy
to authenticate users from your app:
▪ Microsoft Account
▪ Facebook login
▪ Twitter login
▪ Google login
▪ To enable authentication, you must first
register your app with one or more of
these identity providers and then configure
your mobile service.
▪ You must also add authentication code to
your app.
▪ When a user login is authenticated by
Mobile Services, the value of the userId
property on the user object passed to
server scripts is set to a value that
uniquely identifies a user. This value can
then be used to authorize access to data.
OAuth Authentication Flow
* By Carlos Figueira, see references
Windows Store Apps
private static async System.Threading.Tasks.Task Authenticate()
{
while (App.MobileService.CurrentUser == null)
{
string message;
try
{
await
App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook);
message = string.Format("User successfully authenticated: UserId = {0}",
App.MobileService.CurrentUser.UserId);
}
catch (InvalidOperationException)
{
message = "Login failed!";
}
var messageDialog = new MessageDialog(message, "Login Status");
await messageDialog.ShowAsync();
}
}
Windows Phone 8 Apps
private async Task Authenticate()
{
while (user == null)
{
string message;
try
{
user = await
App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);
message = string.Format("You are now logged in - {0}", user.UserId);
}
catch (InvalidOperationException ex)
{
message = ex.Message;
}
MessageBox.Show(message);
}
}
HTML 5 App
HTML Page
<div id="logged-out">
You are not logged in.
<button>Log in</button>
<select id="authentication-providers">
<option selected="selected" value="MicrosoftAccount">Microsoft</option>
<option>Twitter</option>
<option>Facebook</option>
<option>Google</option>
</select>
</div>
HTML 5 App
JavaScript File
function logIn() {
client.login($("#authentication-providers :selected")
.val())
.done(refreshAuthDisplay, function (error) {
alert(error);
});
}
function logOut() {
client.logout();
refreshAuthDisplay();
$('#summary').html('<strong>You must login to access data.</
strong>');
}
User Object
// The User object which is passed to all scripts
// has a function called getIdentities(),
// which returns an object with provider-specific
data
// which can be used to query their user information.
// Facebook
{
"facebook":{
"userId":"Facebook:<user-id>",
"accessToken":“<access-token>"
}
}
// Microsoft:
{
"microsoft":{
"userId":“MicrosoftAccount:<user-id>",
"accessToken":“<access-token>"
}
}
//Google:
{
"google":{
"userId":"Google:<user-id>",
"accessToken":“<access-token>"
}
}
//And for Twitter:
{
"twitter":{
"userId":"Twitter:<user-id>",
"accessToken":“<access-token>",
"accessTokenSecret":“<access-token-
secret>"
}
}
Get User Name (1/2)
function getUserName() {
// Set default name
item.userName = '<unknown>';
// Get credentials for the authenticated user
var identities = user.getIdentities();
var req = require('request');
var url;
// Create url to query the authentication provider for user name
if (identities.microsoft) {
url = 'https://apis.live.net/v5.0/me/?method=GET&access_token=' +
identities.microsoft.accessToken;
} else if (identities.facebook) {
url = 'https://graph.facebook.com/me?access_token=' + identities.facebook.accessToken;
} else if (identities.google) {
url = 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' +
identities.google.accessToken;
} else if (identities.twitter) {
url = 'https://api.twitter.com/1/users/show.json?user_id=' +
user.userId.substring(user.userId.indexOf(':') + 1);
}
Get User Name (1/2)
// Call authentication provider
if (url) {
req(url, function (error, response, body) {
if (error) {
console.error('An error occurred while sending data to Facebook Graph API: ', error);
}
else {
try {
var data = JSON.parse(body);
item.userName = data.name ? data.name : '<unknown>';
}
catch (ex) {
console.error('An error occurred while parsing response data from the auth provider: ',
ex);
}
}
if (f) {
f();
}
});
}
}
Data Validation and Duplicate Detection
Data Validation
function insert(item, user, request) {
// Validation
if (!item.text) {
request.respond(statusCodes.BAD_REQUEST, "The
text cannot be null");
return;
}
}
Duplicate Detection
function insert(item, user, request) {
var todoItemTable = tables.getTable("TodoItem");
todoItemTable.where({userId: user.userId, text: item.text, complete:
false})
.read({
success: function(results) {
if (results && results.length > 0) {
console.log("Duplicate detected: userId = [" +
user.userId +
"] text = [" + item.text + "]")
request.respond(statusCodes.OK, results[0]);
} else {
request.execute();
}
}
});
}
Authorization
function insert(item, user, request) {
var permissionsTable = tables.getTable('permissions');
permissionsTable.where({
userId: user.userId,
permission: 'submit order'
}).read({
success: function (results) {
if (results.length > 0) {
// Permission record was found. Continue normal execution.
request.execute();
} else {
console.log('User %s attempted to submit an order without
permissions.', user.userId);
request.respond(statusCodes.FORBIDDEN, 'You do not have
permission to submit orders.');
}
}
});
}
Administer Mobile Services using
Windows Azure Cross-Platform
Command Line tool
CLI
▪This tool can be
used on the
following
platforms:
▪ Windows
▪ Linux
▪ Mac
▪Mobile Services
can be managed
using CLI
Agenda
▪ 3rd Hour: Push Notifications and integration with other
services
▪ Send push notifications using WNS and MPNS
▪ Windows mobile and Windows Store Apps notifications
▪ Invoke REST services via request Node.js module and HTTP protocol
▪ Integrate Mobile Services with on-premises LOB applications:
▪ Via Service Bus Relay Services
▪ Via Service Bus Queues, Topics and Subscriptions
▪ Break (15 minutes)
Push Notifications
Push
▪ Mobile Services can send push notifications to
your apps.
▪ The way that you configure and send push
notifications depends on your app.
▪ You can send push notifications to the following
applications:
▪ Windows Store app by using the Windows Push
Notification Service (WNS). To send push notifications to
your app, you must configure your mobile service to work
with WNS.
▪ iPhone and iPad apps by using the Apple Push
Notification Service (APNS). To send push notifications to
your app, you must configure your mobile service to work
with APNS. Do this by generating a special push
notification certificate at the iOS Provisioning Portal
▪ Android: You can send push notifications to Android
apps by using the Google Cloud Messaging (GCM)
service. To send push notifications to your app, you must
configure your mobile service to work with GCM.
▪ Windows Phone 8 app by using the Microsoft Push
Notification Service (MPNS). Mobile Services does not
require you to register your Windows Phone 8 app to
send push notifications.
Push Notifications
Windows Store App
private static async System.Threading.Tasks.Task AcquirePushChannel()
{
// Create a channel to receive notifications
var currentChannel = await
PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
var uriAsString = currentChannel.Uri;
// Check if the channel already exists in the Channel table
var channelTable = App.MobileService.GetTable<Channel>();
var enumerable = await channelTable.
Where(c => c.Uri == uriAsString).ToEnumerableAsync();
if (enumerable.Any())
{
// Exit if the channel already exists in the Channel table
return;
}
var channel = new Channel { Uri = currentChannel.Uri, Type = "WNS" };
await channelTable.InsertAsync(channel);
}
Make your App Toast Capable
▪To configure the
Windows Store App
to receive push
notifications, open
the
Package.appxmanife
st file and make sure
that in the Application
UI tab, Toast capable
is set to Yes
Windows Phone 8 App
private HttpNotificationChannel CurrentChannel;
private void AcquirePushChannel()
{
CurrentChannel = HttpNotificationChannel.Find("MyPushChannel");
if (CurrentChannel == null)
{
CurrentChannel = new HttpNotificationChannel("MyPushChannel");
CurrentChannel.Open();
CurrentChannel.BindToShellTile();
CurrentChannel.BindToShellToast();
}
var channelTable = App.MobileService.GetTable<Channel>();
var channel = new Channel { Uri =
CurrentChannel.ChannelUri.ToString() };
channelTable.InsertAsync(channel);
}
Configure the App
▪To configure the Windows
Phone 8 App to receive
push notifications, open the
WMAppManifest.xml file
and make sure that in the
Capabilities tab,
ID_CAP_PUSH_NOTIFICATI
ON is checked.
Server Script Code
function sendPushNotification() {
var channelTable = tables.getTable('Channel');
channelTable.read({
success: function (channels) {
channels.forEach(function (channel) {
if (channel.type === "WNS") {
push.wns.sendToastText02(channel.uri, { text1: "New Todo Item",
text2: item.text},
{success: function (pushResponse) {
console.log("Sent push:", pushResponse);
}
});
}
if (channel.type === "MPNS") {
push.mpns.sendToast(channel.uri, {
text1: 'TodoList',
text2: item.text
}, {
success: function (pushResponse) {
console.log("Sent push:", pushResponse);
}
});
}
DEMO
Push Notifications
Integrate Mobile Services with on-
premises LOB applications
Hybrid Solutions
▪In today’s IT landscape, it is not uncommon that data and services
used by a system are located in multiple application domains.
▪Resources may be stored in a corporate data center, while other
services may be located across the organizational boundaries, in
the cloud or in the data centers of business partners.
▪There are many reasons for requiring a hybrid approach to cloud
applications:
▪ Certain types of data come under national regulations which explicitly
forbid data from being stored in another country.
▪ Data may need to be shared locally with on –premise applications and
services.
Windows Azure Service Bus
▪ The Windows Azure Service Bus is an Internet Service Bus that offers secure,
scalable and highly available connectivity and messaging capabilities.
▪ The Service Bus is part of Windows Azure and is designed to provide
connectivity, queuing, and routing capabilities that allow on-premises and
cloud applications to exchange messages across the boundaries of logical
and physical domains.
▪ The Service Bus can be used as the foundation to create a new range of
hybrid and distributed applications that span the cloud and corporate
environments.
▪ The Service Bus provides 2 messaging types:
▪ Relayed Messaging
▪ Brokered Messaging
Service Bus Relayed Messaging
▪ The Service Bus allows a WCF service to expose an
endpoint in a Windows Azure data center that can be
accessed by consumer applications in a mediated and
secure way using different transport protocols (TCP,
HTTP/S) and message formats (SOAP and REST).
▪ The relay service provides a rendezvous connection point
in the cloud between the client application and the
service provider.
▪ Both the client and service can reside in the cloud or on-
premises.
▪ The Service Bus supports the WCF programming model
and provides a rich set of bindings
(BasicHttpRelayBinding, NetTcpRelayBinding,
NetEventRelayBinding, etc.) to cover a wide variety of
design patterns and Message Exchange Formats.
▪ One-way communications
▪ Publish/Subscribe
▪ Peer-to-peer communications
▪ Multicast messaging
Service Bus Brokered Messaging - Queues
▪ Queues provide messaging capabilities that enable a large and heterogeneous class of
applications running on premises or in the cloud to exchange messages in a secure and reliable
fashion across trust boundaries.
▪ Queues are based on a new messaging infrastructure backed by a replicated, durable store - up
to 5GB per Queue.
▪ The max message size is 256KB, but the session feature allows creating unlimited-size
sequences of related messages.
▪ Queues are available through a .NET API, WCF, HTTP/REST and AMQP Interfaces
Queues - Capabilities
▪ Session-based message correlation.
▪ Message Scheduled Delivery.
▪ Reliable delivery patterns via Peek/Lock Receive Mode.
▪ Transactions support to ensure batches of messaging operations are
committed atomically.
▪ Detection of inbound message duplicates, allowing clients to send the same
message multiple times without adverse consequences (Idem-potency).
▪ Dead-letter facility for messages that cannot be processed or expire before
being received.
▪ Deferring messages for later processing.
Queues - Scenarios
▪ Loosely-Coupled Asynchronous Communication allows to establish an
asynchronous and loosely-coupled communication between autonomous systems
that run in different application domains, on-premises or in the cloud.
▪ Load Leveling allows to flatten a highly-variable traffic into a predictable stream of
work.
▪ Load Balancing allows to distribute the load across a set of worker processes
which size can vary dynamically to accommodate the incoming message volume.
▪ In a Competing Consumers scenario, when a publisher writes a message to a queue, multiple
consumers compete with each other to receive the message, but only one of them will receive
and process the message in question.
▪ Temporal Decoupling allows to work on independent schedules. The producers
and consumers are not required to be online at the same time. This allows to
publish messages to workers that are temporarily offline.
Service Bus Brokered Messaging - Topics
▪ Topics provide all the Features of Queues + Publish/Subscribe capabilities.
▪ A Topic consists of a sequential message store just like a Queue, but it supports up to 2000 concurrent
and durable subscriptions
▪ Each Subscription is a virtual queue getting message copies
▪ A Subscription may have a single consumer that gets all the messages or a set of competing consumers
that fetch messages on a first-come-first-served basis.
▪ Subscriptions can have filters expressions and filter actions
Topics - Scenarios
▪Publish Events to many subscribers
▪Each subscription can be shared by a set of competing
consumers
▪Secondary subscriptions can be used for auditing purposes.
▪Distribute workload across partitioned pools of workers,
each defined by a different subscription
▪Decoupled message fan-out scenario to send messages to
many consumers requiring the same information
▪As the foundation in event-driven architecture
implementations
Topics – Rules & Filters
▪Rules:
▪ Rules select messages based on conditions defined on
system or application-specific properties
▪ Topics support more than one rule for each subscription
▪Filter Conditions and Actions:
▪ Conditions are represented by filter expressions
expressed in SQL’92 syntax:
▪ ShipCountry=“Ukraine” AND ShipCity=“Lviv”
▪ Actions are expressed by action expressions and can add
and modify message properties during retrieval
▪ Set Priority='High';Set Severity=1
Messages
▪ Messages are modeled by the BrokeredMessage class
▪ Messages can have user-defined:
▪ Time-To-Live periods (with no enforced maximum lifetime)
▪ Lock duration
▪ Duplicate detection history time
▪ Messages expose the following properties:
▪ MessageId that can be used for duplicate detection
▪ CorrelationId to correlate a message to a specific request
▪ SessionId for session-enabled communication
▪ ReplyTo that can be used to implement a request-reply MEP.
▪ Label for custom use
▪ User defined properties are key/value pairs
▪ Brokered messaging properties are not SOAP headers
DEMO
Service Bus Explorer
How to call a Service Bus relay service (1/3)
function getUserAddress(f) {
// Set default value
item.userAddress = '<unknown>';
// Create the SOAP Envelope
var body = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' +
'<s:Body>' +
'<user xmlns="http://windowsazure.cat.microsoft.com/samples/
mobileservices">' +
'<userId>' + user.userId + '</userId>' +
'</user>' +
'</s:Body>' +
'</s:Envelope>';
// Import the request module
var req = require('request');
* You can the xmlbuilder, sax and js2xml Node.js modules to create the SOAP messag
How to call a Service Bus relay service (2/3)
// Create call options
var options = {
method: 'POST',
url: 'http://paolosalvatori.servicebus.windows.net/basichttp/todoitem',
headers: {
'Content-Type': 'text/xml',
'SOAPAction': 'GetUserAddress'
},
body: body
};
// Call the Service Bus Relay Service
req(options, function(error, response, body) {
if (error) {
console.log('An error occurred while invoking the service: ', error);
} else {
// Import xml2js module
var xml2js = require('xml2js');
// Create a xml2js parser
var parser = new xml2js.Parser();
How to call a Service Bus relay service (3/3)
// Parse the SOAP envelope of the response from XML to JSON
parser.parseString(body, function (err, result) {
try {
// Read the address from the JSON object
var address = result["s:Envelope"]["s:Body"][0]["userAddress"][0]["text"]
[0];
if (address) {
// Set the userAddress property of the item
item.userAddress = address;
}
}
catch (ex) {
// Log the error
console.log("An error occurred while parsing the response: ", error);
}
});
if (f) {
// Invoke a continuation function, if any
f();
}
}
});
}
How to use Send a message to queue (1/2)
function sendMessageToServiceBus() {
// Import the azure module
var azure = require('azure');
// Create a ServiceBusService object
var serviceBusService = azure.createServiceBusService
('<your-service-bus-namespace-name>',
'<your-service-bus-namespace-key>');
// Define queue options
var queueOptions = {
EnableBatchedOperations: true,
RequiresDuplicateDetection: true,
DuplicateDetectionHistoryTimeWindow: 'PT8H'
};
How to use Send a message to queue (2/2)
// Create queue if it does not exist
serviceBusService.createQueueIfNotExists('mobileservices/todoitem', queueOptions, function (error) {
if (error) {
console.error("An error occurred creating/accessing the Service Bus queue: ", error);
}
else {
// Create a message
var message = {
body: 'Hello World!',
messageId: item.id,
customProperties: {
source: 'Mobile Services',
author: 'Paolo Salvatori'
}
};
// Send the message to the queue
serviceBusService.sendQueueMessage('mobileservices/todoitem', message, function (error) {
if (!error) {
console.log('Sent message: ' + message);
}
});
}
});
}
Integration via Service Bus
DEMO
Integrate Mobile Services with on-
premises LOB applications
Agenda
▪ 4th Hour: Storage Services, Scheduler, SendGrid
▪ Store data to Table Storage using the azure Node.js module
▪ Use Shared Access Signatures and Blobs with Mobile Services
▪ Create and schedule jobs with the scheduler
▪ Send emails using SendGrid Node.js module
▪ Break (15 minutes)
Table Storage
Choose the PartitionKey and RowKey wisely
▪ Tables are partitioned to support load balancing across storage nodes.
▪ A table's entities are organized by partition.
▪ A partition is a consecutive range of entities sharing the same partition key value.
▪ The partition key is a unique identifier for the partition within a given table,
specified by the PartitionKey property.
▪ The partition key forms the first part of an entity's primary key.
▪ The second part of the primary key is the row key, specified by the RowKey
property.
▪ The row key is a unique identifier for an entity within a given partition.
▪ Together the PartitionKey and RowKey uniquely identify every entity within a
table.
▪ An entity can have up to 255 properties, including 3 system properties
described in the following section.
Insert Script (simplified)
function insert(item, user, request) {
// Access table service
var azure = require('azure');
var tableService = azure.createTableService('<your-storage-account-name>', '<your-storage-account-key>');
// Create table if not exists
tableService.createTableIfNotExists('TodoItems', function(error) {
if (error) {
request.respond(statusCodes.BAD_REQUEST, error);
} else {
var entity = {
PartitionKey: user.userId || 'default',
RowKey: (new Date()).getTime(),
text: item.text,
complete: item.complete,
};
// Insert the item in the storage table
tableService.insertEntity('TodoItems', entity, function (error) {
if (error) {
request.respond(statusCodes.BAD_REQUEST, error);
}
else {
request.respond(statusCodes.OK, entity);
}
});
}
});
}
DEMO
Table Storage
Shared Access Signatures
▪ A shared access signature (SAS) is a URI that grants restricted access rights to
containers, blobs, queues, and tables.
▪ You can provide a SAS to clients that are not trusted to use the storage account key
but to whom you wish to delegate access to certain storage account resources.
▪ By distributing a SAS URI to these clients, you can grant them access to a resource
for a specified period of time, with a specified set of permissions.
▪ A shared access signature can grant any of the following operations to a client that
possesses the signature:
▪ Reading and writing page or block blob content, block lists, properties, and metadata
▪ Deleting, leasing, and creating a snapshot of a blob
▪ Listing the blobs within a container
▪ Adding, removing, updating, and deleting queue messages
▪ Getting queue metadata, including the message count
▪ Querying, adding, updating, deleting, and upserting table entities
Blobs and Shared Access Signature
1. The Windows Store app creates a new
album
2. The Mobile Service insert a new record
in the Album table
3. Thw Windows Store app creates a new
picture
4. The Mobile service calls the storage
account to get a SAS for the image and
its thumbnail
5. The Mobile Services saves the image
data in the Picture table
6. The Insert script returns a SAS for the
image and thumbnail in the item object
7. The Windows Store app uses the
storage account client API and the SAS
returned by the Mobile Service to save
the image and thumbnail as blobs.
*Sample by Nick Harris, see references for mo
Server Script and SAS
▪ The insert script uses the following line of code to build a SAS URL:
▪ return 'https://' + host + blobRelativePath + '?' + sasQueryString;
▪ A typical SAS query string contains many parameters which depend on the
storage resource (blob, container, table, queue)
▪ se=2013-05-21T15%3A37%3A15Z&sr=b&sp=w&sig=dj9zqN%2BSM1UX
▪ se = signedexpiry: the time at which the shared access signature becomes invalid
▪ sr = signedresource: b to grant access to a blob, c to grant access to a container
▪ sp = signedpermissions: the permissions associated with the Shared Access Signature (w = write)
▪ sig = signature: the signature part of the URI is used to authenticate the request made with the SAS.
▪ See http://msdn.microsoft.com/en-us/library/windowsazure/dn140255.aspx
DEMO
Blobs and SAS
What is the Azure Scheduler service?
▪Platform service that allows users a simple way to
schedule recurring actions against other services in
the cloud
▪Examples:
▪ Store a daily backup of my SQL database to blob storage.
▪ Clean up my To Do app’s done items every day
▪ Check the health of my service every few minutes
▪ Send a newsletter every day
Azure Scheduler Capabilities
▪Azure Portal integrated experience
▪Actions can be scheduled to fire once or on a regular
recurring schedule
▪Reliably and securely invoke services inside and
outside of Azure using Node.js modules
Use a Job to backup a SQL Database
1. The scheduler calls the
backup job
2. The backup job uses
the request module to
invoke the WA SQL
Database Import/Export
service by calling its
REST endpoint
3. The Import/Export
service reads the DB
schema and data
4. The service exports DB
schema and data to
Windows Azure Blob
Storage as a BACPAC
*Sample by Josh Twist, see references for mor
Code (1/2)
function backup() {
var request = require('request');
var util = require('util');
var date = new Date();
var year = date.getUTCFullYear();
var month = date.getUTCMonth() + 1;
var day = date.getUTCDate();
var body = {
BlobCredentials: {
__type: "BlobStorageAccessKeyCredentials:#Microsoft.SqlServer.Management.Dac.ServiceTypes",
Uri: util.format("https://<your-storage-account-name>.blob.core.windows.net/bacpac/%s-%s-%s.bacpac", year, month,
day),
StorageAccessKey: "<your-storage-account-key>"
},
ConnectionInfo: {
DatabaseName: "<your-database-name>",
Password: "<your-server-password>",
ServerName: "<your-server-name>.database.windows.net",
UserName: “<your-server-username>"
}
}
Code (2/2)
var postUri = "https://am1prod-dacsvc.azure.com/DACWebService.svc/Export";
request.post({
uri: postUri,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body)
}, function (e, r, b) {
if (e || r.statusCode != 200) {
console.error('backup failed', e || r.statusCode, b);
}
else {
console.log('backup successful', b);
}
});
}
DEMO
Schedule SQL Database Backup
Send a Report via Email
1. The scheduler calls the job function
2. The script reads data from SQL tables
and/or Storage tables
3. The script aggregates and formats data
in a report.
4. The script sends the report via email
using the the sendgrid Node.js module
Code (1/3)
function CountTodoItems() {
var SendGrid = require('sendgrid').SendGrid;
// Get access to the storage table
var azure = require('azure');
var tableService = azure.createTableService('<your-storage-account-name>',
'<your-storage-account-name>');
// Define query to retrieve todo items
var tableQuery = azure.TableQuery
.select()
.from('TodoItems')
.where('complete eq ?', 'false');
// Retrieve todo items
queryEntities();
Code (2/3)
function queryEntities() {
tableService.queryEntities(tableQuery, function (error, entities) {
if (error) {
var message = 'Failed to retrieve todo items from the TodoItems table.';
console.error(message);
}
else {
var text = 'The TodoItems table actually contains ' + entities.length + ' rows:rnrn';
for (var i = 0; i < entities.length; i++) {
text = text + ' - UserId: ' + entities[i].PartitionKey;
text = text + ' Item: ' + entities[i].text + 'rn';
}
text = text + 'rnMessage sent by TodoItems site at ' + (new Date()).toString();
sendEmail(text);
console.log(text);
}
});
}
Code (3/3)
function sendEmail(text) {
var sendgrid = new SendGrid('babosbird', 'Trustno1');
sendgrid.send({
to: 'paolos@microsoft.com',
from: 'leprino@hotmail.com',
subject: 'Number of Todo Items',
text: text
}, function (success, message) {
// If the email failed to send, log it as an error so we can investigate
if (!success) {
console.error(message);
}
});
}
}
DEMO
Send a Report via Email
Trigger a Worker Role with a Service Bus Msg
1. The scheduler calls the job function.
2. The script creates and send a message
to a Service Bus queue or topic.
3. One instance of the Worker Role reads
the message from the queue/
subscription.
4. The Worker Role instance processes
the message.
Code
function SendMessageToServiceBus() {
var azure = require('azure');
var serviceBusService = azure.createServiceBusService('paolosalvatori',
'PgTi69pNLU2Me58C3xiEDT5xosclhfEgQs6NsUCY9Qs=');
serviceBusService.createQueueIfNotExists('mobileservices/scheduler', function (error) {
if (!error) {
var builder = require('xmlbuilder');
var root = builder.create('job',
{ 'version': '1.0', 'encoding': 'UTF-8' })
.att('xmlns', 'http://windowsazure.cat.microsoft.com/samples/mobileservices');
root.ele('command', 'read');
root.ele('date', (new Date()).toUTCString());
var message = root.end({ 'pretty': true, 'indent': ' ', 'newline': 'n' });
serviceBusService.sendQueueMessage('mobileservices/scheduler', message, function (error) {
if (!error) {
console.log('Sent message: ' + message);
}
});
}
});
}
DEMO
Trigger a Worker Role with a Service Bus Msg
References
▪ Windows Azure Mobile Services
▪ http://www.windowsazure.com/en-us/develop/mobile/
▪ Tutorials and Resources
▪ http://www.windowsazure.com/en-us/develop/mobile/resources-html/
▪ Upload File to Windows Azure Blob Storage using Windows Azure Mobile Services
▪ http://code.msdn.microsoft.com/Upload-File-to-Windows-c9169190
▪ Getting user information on Azure Mobile Services
▪ http://blogs.msdn.com/b/carlosfigueira/archive/2012/10/25/getting-user-information-on-azure-mobile-services.aspx
▪ Troubleshooting authentication issues in Azure Mobile Services
▪ http://blogs.msdn.com/b/carlosfigueira/archive/2012/10/23/troubleshooting-authentication-issues-in-azure-mobile-services.aspx
References
▪ Using the scheduler to backup your Mobile Service database
▪ http://www.thejoyofcode.com/Using_the_scheduler_to_backup_your_Mobile_Service_database.aspx
▪ Getting started with the CLI and backing up your scripts
▪ http://www.thejoyofcode.com/Getting_started_with_the_CLI_and_backing_up_your_scripts_Day_4_.aspx
▪ More CLI – changing your Mobile Services workflow
▪ http://www.thejoyofcode.com/More_CLI_ndash_changing_your_Mobile_Services_workflow_Day_5_.aspx
▪ New Windows Azure Mobile Services Getting Started Content
▪ http://blogs.msdn.com/b/windowsazure/archive/2013/01/09/new-windows-azure-mobile-services-getting-started-content.aspx
▪ How to use Windows Azure Table Storage in Windows Azure Mobile Services
▪ http://code.msdn.microsoft.com/windowsazure/Use-Table-Storage-in-0b6bcbc7
References
▪ How to Use the Table Service from Node.js
▪ http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/table-services/
▪ How to Use Service Bus Queues
▪ http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/service-bus-queues/
▪ How to Use Service Bus Topics/Subscriptions
▪ http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/service-bus-topics/
▪ How to Send Email Using SendGrid from Node.js
▪ http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/sendgrid-email-service/
Blogs
▪Josh Twist Blog
▪ http://www.thejoyofcode.com/
▪Nick Harris Blog
▪ http://www.nickharris.net/
▪Carlos Figuera
▪ http://blogs.msdn.com/b/carlosfigueira/
▪Paolo Salvatori Blog
▪ http://blogs.msdn.com/b/paolos
Contacts
Paolo Salvatori
Email: Paolos@Microsoft.com
Blog: http://blogs.msdn.com/b/paolos/
Twitter: @babosbird
© 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be
a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE
INFORMATION IN THIS PRESENTATION.

Contenu connexe

Tendances

Configuration manager
Configuration managerConfiguration manager
Configuration manager
Raghu nath
 
Summer '16 Realease notes
Summer '16 Realease notesSummer '16 Realease notes
Summer '16 Realease notes
aggopal1011
 
SharePoint 2010, Claims-Based Identity, Facebook, and the Cloud
SharePoint 2010, Claims-Based Identity, Facebook, and the CloudSharePoint 2010, Claims-Based Identity, Facebook, and the Cloud
SharePoint 2010, Claims-Based Identity, Facebook, and the Cloud
Danny Jessee
 

Tendances (17)

Understanding Office 365’s Identity Solutions: Deep Dive - EPC Group
Understanding Office 365’s Identity Solutions: Deep Dive - EPC GroupUnderstanding Office 365’s Identity Solutions: Deep Dive - EPC Group
Understanding Office 365’s Identity Solutions: Deep Dive - EPC Group
 
RealMe Technical Overview 2013 v1.1
RealMe Technical Overview 2013 v1.1RealMe Technical Overview 2013 v1.1
RealMe Technical Overview 2013 v1.1
 
08 asp.net session11
08 asp.net session1108 asp.net session11
08 asp.net session11
 
Sharepoint
SharepointSharepoint
Sharepoint
 
WSS 3.0 using asp.net 2.0 for extending pages,Server Farms etc..
WSS 3.0 using asp.net 2.0 for extending pages,Server Farms etc..WSS 3.0 using asp.net 2.0 for extending pages,Server Farms etc..
WSS 3.0 using asp.net 2.0 for extending pages,Server Farms etc..
 
Blackberry Enterprise Server Installation
Blackberry Enterprise Server InstallationBlackberry Enterprise Server Installation
Blackberry Enterprise Server Installation
 
Sql server lesson2
Sql server lesson2Sql server lesson2
Sql server lesson2
 
Microsoft Dynamics CRM 2011 Walkthrough Part 1
Microsoft Dynamics CRM 2011 Walkthrough Part 1Microsoft Dynamics CRM 2011 Walkthrough Part 1
Microsoft Dynamics CRM 2011 Walkthrough Part 1
 
Tech p22 integrating sap with web sphere portal
Tech p22 integrating sap with web sphere portalTech p22 integrating sap with web sphere portal
Tech p22 integrating sap with web sphere portal
 
Configuration manager
Configuration managerConfiguration manager
Configuration manager
 
Summer '16 Realease notes
Summer '16 Realease notesSummer '16 Realease notes
Summer '16 Realease notes
 
SharePoint 2010, Claims-Based Identity, Facebook, and the Cloud
SharePoint 2010, Claims-Based Identity, Facebook, and the CloudSharePoint 2010, Claims-Based Identity, Facebook, and the Cloud
SharePoint 2010, Claims-Based Identity, Facebook, and the Cloud
 
Sp jayen gopi
Sp jayen gopiSp jayen gopi
Sp jayen gopi
 
SharePoint 2010, Claims-Based Identity, Facebook, and the Cloud
SharePoint 2010, Claims-Based Identity, Facebook, and the CloudSharePoint 2010, Claims-Based Identity, Facebook, and the Cloud
SharePoint 2010, Claims-Based Identity, Facebook, and the Cloud
 
24032022 Zero Trust for Developers Pub.pdf
24032022 Zero Trust for Developers Pub.pdf24032022 Zero Trust for Developers Pub.pdf
24032022 Zero Trust for Developers Pub.pdf
 
Claims-Based Identity, Facebook, and the Cloud
Claims-Based Identity, Facebook, and the CloudClaims-Based Identity, Facebook, and the Cloud
Claims-Based Identity, Facebook, and the Cloud
 
Step by step exercise for bw 365
Step by step exercise for bw 365Step by step exercise for bw 365
Step by step exercise for bw 365
 

Similaire à Microsoft mobile services

CV_PurnimaBalla_WCS-Consultant_7Yrs
CV_PurnimaBalla_WCS-Consultant_7YrsCV_PurnimaBalla_WCS-Consultant_7Yrs
CV_PurnimaBalla_WCS-Consultant_7Yrs
Purnima Balla
 
IBM Innovate 2013: Making Rational HATS a Strategic Investment
IBM Innovate 2013: Making Rational HATS a Strategic InvestmentIBM Innovate 2013: Making Rational HATS a Strategic Investment
IBM Innovate 2013: Making Rational HATS a Strategic Investment
Strongback Consulting
 
Resume - Abhishek Gupt
Resume - Abhishek GuptResume - Abhishek Gupt
Resume - Abhishek Gupt
abhishek gupta
 

Similaire à Microsoft mobile services (20)

ServiceNow Tokyo Release- Aelumconsulting.pdf
ServiceNow Tokyo Release- Aelumconsulting.pdfServiceNow Tokyo Release- Aelumconsulting.pdf
ServiceNow Tokyo Release- Aelumconsulting.pdf
 
Job center
Job centerJob center
Job center
 
CV_PurnimaBalla_WCS-Consultant_7Yrs
CV_PurnimaBalla_WCS-Consultant_7YrsCV_PurnimaBalla_WCS-Consultant_7Yrs
CV_PurnimaBalla_WCS-Consultant_7Yrs
 
A WEB-BASED SERVICE MARKETPLACE
A WEB-BASED SERVICE MARKETPLACEA WEB-BASED SERVICE MARKETPLACE
A WEB-BASED SERVICE MARKETPLACE
 
New Feature in CRM 2016
New Feature in CRM 2016New Feature in CRM 2016
New Feature in CRM 2016
 
What is sup
What is supWhat is sup
What is sup
 
IBM Innovate 2013: Making Rational HATS a Strategic Investment
IBM Innovate 2013: Making Rational HATS a Strategic InvestmentIBM Innovate 2013: Making Rational HATS a Strategic Investment
IBM Innovate 2013: Making Rational HATS a Strategic Investment
 
Vj courier
Vj courier Vj courier
Vj courier
 
Analytics and etl based bi solutions
Analytics and etl based bi solutionsAnalytics and etl based bi solutions
Analytics and etl based bi solutions
 
M365 Teams Automation
M365 Teams AutomationM365 Teams Automation
M365 Teams Automation
 
BrightGen's Salesforce Winter 22 Webinar
BrightGen's Salesforce Winter 22 WebinarBrightGen's Salesforce Winter 22 Webinar
BrightGen's Salesforce Winter 22 Webinar
 
Thiru_Resume
Thiru_ResumeThiru_Resume
Thiru_Resume
 
#SPSToronto The SharePoint Framework and the Microsoft Graph on steroids with...
#SPSToronto The SharePoint Framework and the Microsoft Graph on steroids with...#SPSToronto The SharePoint Framework and the Microsoft Graph on steroids with...
#SPSToronto The SharePoint Framework and the Microsoft Graph on steroids with...
 
#SPSottawa The SharePoint Framework and The Microsoft Graph on steroids with ...
#SPSottawa The SharePoint Framework and The Microsoft Graph on steroids with ...#SPSottawa The SharePoint Framework and The Microsoft Graph on steroids with ...
#SPSottawa The SharePoint Framework and The Microsoft Graph on steroids with ...
 
Get started azure- Azure Mobile Services
Get started azure- Azure Mobile ServicesGet started azure- Azure Mobile Services
Get started azure- Azure Mobile Services
 
Azure Mobile Services
Azure Mobile ServicesAzure Mobile Services
Azure Mobile Services
 
Making Rational HATS a Strategic Investment
Making Rational HATS a Strategic InvestmentMaking Rational HATS a Strategic Investment
Making Rational HATS a Strategic Investment
 
Granite state #spug The #microsoftGraph and #SPFx on steroids with #AzureFunc...
Granite state #spug The #microsoftGraph and #SPFx on steroids with #AzureFunc...Granite state #spug The #microsoftGraph and #SPFx on steroids with #AzureFunc...
Granite state #spug The #microsoftGraph and #SPFx on steroids with #AzureFunc...
 
Resume - Abhishek Gupt
Resume - Abhishek GuptResume - Abhishek Gupt
Resume - Abhishek Gupt
 
Saml sap netweaver_fiori
Saml sap netweaver_fioriSaml sap netweaver_fiori
Saml sap netweaver_fiori
 

Plus de Maksym Davydov

Plus de Maksym Davydov (20)

Firebase overview
Firebase overviewFirebase overview
Firebase overview
 
Design of mobile apps
Design of mobile appsDesign of mobile apps
Design of mobile apps
 
Mobile app design feature development
Mobile app design feature developmentMobile app design feature development
Mobile app design feature development
 
Android mix Java and C++
Android mix Java and C++Android mix Java and C++
Android mix Java and C++
 
Android animations
Android animationsAndroid animations
Android animations
 
Handler declaration in layout
Handler declaration in layoutHandler declaration in layout
Handler declaration in layout
 
Android Networking
Android NetworkingAndroid Networking
Android Networking
 
Android Storage
Android StorageAndroid Storage
Android Storage
 
Interface Programming Android
Interface Programming AndroidInterface Programming Android
Interface Programming Android
 
Java Small Tests
Java Small TestsJava Small Tests
Java Small Tests
 
Android Programming Intro
Android Programming IntroAndroid Programming Intro
Android Programming Intro
 
Lecture 02 Mobile hardware
Lecture 02 Mobile hardwareLecture 02 Mobile hardware
Lecture 02 Mobile hardware
 
Lecture 01 Mobile operating systems
Lecture 01 Mobile operating systemsLecture 01 Mobile operating systems
Lecture 01 Mobile operating systems
 
Lecture 13 Local Optimization on Mobile Devices
Lecture 13 Local Optimization on Mobile DevicesLecture 13 Local Optimization on Mobile Devices
Lecture 13 Local Optimization on Mobile Devices
 
Lecture 12. iOS and Android Animations
Lecture 12. iOS and Android AnimationsLecture 12. iOS and Android Animations
Lecture 12. iOS and Android Animations
 
Lecture 11. Microsoft mobile services
Lecture 11. Microsoft mobile servicesLecture 11. Microsoft mobile services
Lecture 11. Microsoft mobile services
 
Lecture 11 Firebase overview
Lecture 11 Firebase overviewLecture 11 Firebase overview
Lecture 11 Firebase overview
 
Lecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesLecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile Devices
 
Lecture 09 Android Storage
Lecture 09 Android StorageLecture 09 Android Storage
Lecture 09 Android Storage
 
Lecture 08 Xamarin
Lecture 08 XamarinLecture 08 Xamarin
Lecture 08 Xamarin
 

Dernier

An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Dernier (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

Microsoft mobile services

  • 1. Microsoft Azure Mobile Services Paolo Salvatori Microsoft
  • 2. Paolo Salvatori Senior Program Manager Microsoft
  • 3. Agenda ▪ 1st Hour: Introduction to Mobile Services (45 minutes) ▪ General Architecture ▪ Management Portal ▪ REST API ▪ Break (15 minutes)
  • 4. Enterprises want to manage internal mobile apps and consumer facing event- or product-specific mobile apps from the same portal as core LOB apps Small Businesses require solutions that accelerate development time and decrease development costs. Developers shouldn’t have to constantly reinvent the wheel and reproduce common backend functionality 
 Consumers expect a continuous experience across all devices App Development
 Challenges
  • 5. Windows Azure Mobile Services accelerates connected client application development by streamlining common backend tasks like structuring storage, authenticating users, and sending push notifications.
  • 6. 3 Connected Apps Windows Store iOS Android Windows Phone 8 iOS Android HTML 5/JS Data in the Cloud SQL Table Blob User Authentication Facebook Twitter Microsoft Google Push Notifications
 WNS & APNS GCM MPNS SDKs Server- Side Scripts & Scheduler Basic Features
  • 7. Supported Platforms iOSWindows 8 Windows Phone 8 Android HTML5 & Javascript
  • 8.
  • 9.
  • 10. Architecture
Portal Zumo Resource Provider Zumo Mgmt DB Site 1 Zumo Runtime SQL 1 Scheduler RDFE Zumo Antares Stamp Site 2 Site N Azure SDK 3rd party SDKS SQL 2 SQL N AzureLBARR App Scripts Win8 Device iPhone
  • 12. Dashboard ▪ Quick glance ▪ Displays relevant mobile service information, including the service URL, status, location, database and subscription ▪ Usage Timeline: ▪ Displays a scalable chart of CPU time, API calls and data reads over a specific period of time. ▪ Usage Overview: ▪ Displays an overview of the total compute time and storage used by this mobile service ▪ Manage Keys ▪ Manage Keys ▪ Delete ▪ Delete the Mobile Service
  • 13. Data ▪ In Windows Azure Mobile Services, data is stored in tables, which are maintained in the Windows Azure SQL Database that is associated with your mobile service. ▪ Click the Create button to add a new table to your mobile service. ▪ By default, you don’t need to predefine the schema of tables in your database. ▪ Mobile Services automatically add columns to a table based on the data you insert. To change this dynamic schema behavior, use the Dynamic Schema setting on the Configure tab. ▪ To browse data, manage permissions and indexes on tables, or add server scripts, click on a table in the list. ▪ To delete a table, click to select a table row, and then click Delete.
  • 14. Browse ▪ The Browse tab lets you browse data rows in a selected table. ▪ Click the forward and back arrows to navigate through multiple pages of data. ▪ Click on table names in the left navigation bar to switch between tables. ▪ Click on the large left arrow to go back to the Data tab.
  • 15. Script ▪ Mobile Services enables you to register JavaScript code to be executed by the service when an insert, update, delete, or read operation occurs against a specific table. ▪ For each table there are 4 CRUD operations (insert, read, update, del) ▪ These scripts provide the ability to inject business logic into table operation, stop an operation from taking place, change or enrich values supplied to the operation or change the results. ▪ For authenticated clients, you can also perform user-specific authorizations based on the userId value of the supplied user object.
  • 16. Columns ▪ To delete columns, click the column and then click Delete. ▪ To add additional columns to the table, simply send an insert request including the new properties from your app with dynamic schema enabled. ▪ Once a column is created, its data type cannot be changed by Mobile Services. ▪ Insert or update operations fail when the type of a property in the JSON object cannot be converted to the type of the equivalent column in the table. ▪ You can improve the performance of queries by defining an index on the table. ▪ To add a column to the index, select the column and click Set as Index.
  • 17. Permissions ▪ Mobile Services enables you to set the following permissions on table operations: ▪ Everyone: request for the operation against the table is accepted. This option leaves your data wide-open for everyone to access. ▪ Anybody with the Application Key: The application key is required to perform the operation. The application key is distributed with the application. Note: Because this key is not securely distributed, it cannot be considered a security token. To secure access to your mobile service data, you must instead authenticate users before accessing. ▪ Only Authenticated Users: Only authenticated users are permitted to perform the operation. Scripts can be used to further restrict access to tables based on an authenticated user. ▪ Only Scripts and Admins: The operation requires the service master key, which limits the operation only to registered scripts or to administrator accounts
  • 18. Scheduler ▪ Mobile Services enables you to register JavaScript code to be executed by the service either on a schedule that you define or on- demand from the Management Portal. ▪ Removing duplicate records from a table. ▪ Backing up a database. ▪ Send a report via email. ▪ When your mobile service runs in free mode, you can create only one scheduled job at a time; in reserved mode, you can create up to ten scheduled jobs at the same time.
  • 19. Push ▪ Mobile Services can send push notifications to your apps. ▪ The way that you configure and send push notifications depends on your app. ▪ You can send push notifications to the following applications: ▪ Windows Store app by using the Windows Push Notification Service (WNS). To send push notifications to your app, you must configure your mobile service to work with WNS. ▪ iPhone and iPad apps by using the Apple Push Notification Service (APNS). To send push notifications to your app, you must configure your mobile service to work with APNS. Do this by generating a special push notification certificate at the iOS Provisioning Portal ▪ Android: You can send push notifications to Android apps by using the Google Cloud Messaging (GCM) service. To send push notifications to your app, you must configure your mobile service to work with GCM. ▪ Windows Phone 8 app by using the Microsoft Push Notification Service (MPNS). Mobile Services does not require you to register your Windows Phone 8 app to send push notifications.
  • 20. Identity ▪ Mobile Services integrates with the following identity providers to make it easy to authenticate users from your app: ▪ Microsoft Account ▪ Facebook login ▪ Twitter login ▪ Google login ▪ To enable authentication, you must first register your app with one or more of these identity providers and then configure your mobile service. ▪ You must also add authentication code to your app. ▪ When a user login is authenticated by Mobile Services, the value of the userId property on the user object passed to server scripts is set to a value that uniquely identifies a user. This value can then be used to authorize access to data.
  • 21. Configure ▪ You can directly access to the database used by your mobile service just clicking the link. ▪ You can change the database used by your mobile service to store data with a new or existing DB by clicking Change DB and select one of the following options. ▪ Enable/disable dynamic schema. ▪ In reserved mode, you can configure endpoint monitoring.
  • 22. Scale ▪ On the Scale tab, you can scale your mobile service by switching to reserved mode and adding role instances. ▪ You can also scale the Windows Azure SQL Database instance used by the service. ▪ Note: When you set the Mobile service mode to Reserved, all of your mobile services in the same region run in Reserved mode. ▪ To estimate the cost required to scale your mobile service, see the Mobile Services Pricing Calculator.
  • 23. Log ▪ Mobile Services writes system and script errors to the log. ▪ Click on a log entry to view details and copy to the clipboard. ▪ Click the link in the Source column to directly jump to the script that generated the error. ▪ Note: Existing log entries can be viewed in the Management Portal for 7 days. ▪ Your registered server scripts can also write information directly to the log by using the console object.
  • 25. REST API Operation Description Login Gets the user ID for a supplied authentication token. Query records Queries data in a table. Insert record Inserts a new record into a table. Update record Updates an existing record in a table. Delete record Deletes an existing record from a table.
  • 26. Login ▪ Verb: POST ▪ URL: https://<service-name>.azure-mobile.net/login?mode=authenticationToken ▪ Uri Parameter: mode ▪ Request Headers:
 
 
 ▪ Body: JSON object
 {"authenticationToken":“<authentication-provider-token>"} for Microsoft
 {“access_token":“<authentication-provider-token>"} for Facebook and Google ▪ Response: JSON object
 {"user":{"userId":"<authentication-provider>:cb392ceae24a44408ed863482b20f274"},
 "authenticationToken":"<mobile-service-authentication-token>"} Request Header Required Value Accept No Set this header to application/json. Content-Length Yes The length of the request body. Content-Type No Set this header to application/json.
  • 27. Query Records ▪ Verb: GET ▪ URL: https://<service_name>.azure-mobile.net/tables/<table_name> ▪ URI Parameters: $filter, $inlinecount, $orderby, $select, $skip, $top ▪ Request Headers:
 
 
 
 ▪ Response: data in JSON format ▪ Samples: ▪ GET https://plunko.azure-mobile.net/tables/todoitem ▪ GET https://plunko.azure-mobile.net/tables/todoitem/1 ▪ GET https://plunko.azure-mobile.net/tables/todoitem?$filter=(complete%20eq%20false) ▪ GET https://plunko.azure-mobile.net/tables/todoitem?$filter=(complete%20eq%20false)&$orderby=text%20asc Request Header Required Value Accept No Set this header to application/json. X-zumo-application Conditional The application key of the mobile service. You must specify a valid application key when required to access the table operation. This is the default table operation access permission. X-zumo-auth Conditional The service-generated authentication token for an authenticated user. You must specify a token for an authenticated user when required to access the table operation.
  • 28. Insert Record ▪ Verb: POST ▪ URL: https://<service_name>.azure-mobile.net/tables/<table_name> ▪ Request Headers:
 
 
 
 ▪ Body: new record in JSON format ▪ Response: The JSON representation of the inserted item, which includes the ID for the item generated by the mobile service. ▪ Sample: ▪ POST https://plunko.azure-mobile.net/tables/todoitem ▪ Body: {"text" : "Go to Las Vegas", "complete": false, "when" : "don't know"} Request Header Required Value Accept No Set this header to application/json. Content-Length Yes The length of the request body. Content-Type No Set this header to application/json. X-zumo-application Conditional The application key of the mobile service. X-zumo-auth Conditional The service-generated authentication token for an authenticated user.
  • 29. Update Record ▪ Verb: PATCH ▪ URL: https://<service_name>.azure-mobile.net/tables/<table_name>/<item_id> ▪ Request Headers:
 
 
 
 ▪ Body: The updated values encoded in JSON format, along with the optional id value of the object. ▪ Response: The JSON representation of the updated item ▪ Sample: ▪ PATCH https://plunko.azure-mobile.net/tables/todoitem/1 ▪ Body: {"text" : "Go to Barcelona", "complete": false, "when" : "don't know"} Request Header Required Value Accept No Set this header to application/json. Content-Length Yes The length of the request body. Content-Type No Set this header to application/json. X-zumo-application Conditional The application key of the mobile service. X-zumo-auth Conditional The service-generated authentication token for an authenticated user.
  • 30. Delete Record ▪ Verb: DELETE ▪ URL: https://<service_name>.azure-mobile.net/tables/<table_name>/<item_id> ▪ Request Headers:
 
 
 
 ▪ Body: none ▪ Response: none ▪ Sample: ▪ DELETE https://plunko.azure-mobile.net/tables/todoitem/6 Request Header Required Value X-zumo-application Conditional The application key of the mobile service. X-zumo-auth Conditional The service-generated authentication token for an authenticated user.
  • 31. HttpClient and Mobile Services private async void btnSend_Click(object sender, EventArgs e) { try { var httpClient = new HttpClient(); var uri = new Uri("https://<your-mobile-service>.azure-mobile.net/tables/TodoItem"); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpClient.DefaultRequestHeaders.Add("x-zumo-application", “<application-key>"); httpClient.DefaultRequestHeaders.Host = uri.Host; var todoItem = new TodoItem {Text = txtText.Text, Complete = false}; var serializer = new DataContractJsonSerializer(typeof (TodoItem)); using (var stream = new MemoryStream()) { serializer.WriteObject(stream, todoItem); stream.Seek(0, SeekOrigin.Begin); //use a Stream reader to construct the StringContent (Json) using (var reader = new StreamReader(stream)) { var response = await httpClient.PostAsync(uri, new StringContent(reader.ReadToEnd(), Encoding.UTF8, "application/json")); WriteToLog(response.IsSuccessStatusCode ? string.Format("Message <{0}> successfully inserted!", txtText.Text) : string.Format("HTTP Status: <{0}> Reason: <{1}>", response.StatusCode, response.ReasonPhrase)); } } } catch (Exception ex) { WriteToLog(ex.Message); } }
  • 32. Break
  • 33. Agenda ▪ 2nd Hour: Server Scripts (45 minutes) ▪ Server script objects ▪ Server script modules ▪ Configure user authentication: ▪ Windows Live ▪ Facebook ▪ Twitter ▪ Google ▪ User object and integration with identity providers ▪ Data validation and duplicate detection ▪ Administer Mobile Services using Windows Azure Cross-Platform Command Line tool ▪ Break (15 minutes)
  • 35. Objects Name Description apns Provide functionality to send notifications to Apple Push Notification Service (APNS) console Write information to Services logs gcm Provide functionality to send notifications to Google Cloud Messaging (GCM) mpns Provide functionality to send notifications to Microsoft Push Notification Service (MPNS) mssql Provides functionality for working directly with tables in the SQL Database using Transact- SQL push Provides access to objects that are used for sending push notifications to apps Query Provides functionality for querying Windows Azure Mobile Services Request Used to execute the requested operation and determine the response to the client statusCodes Enumerates the supported HTTP status codes can be returned by Windows Azure Mobile Services Table Provides functionality for working with specific tables (insert, del, update, where, take, select, etc.) tables Provides functionality for working with specific tables as a Table object instance
  • 36. Samples console object console.log("Inserting item '%j'.", item); // %j for 'JSON' console.error("Operation failed: ", error); mssql object mssql.query('select * from orders where country = "Ukraine"', {
 success: function (results) {
 console.log(results);
 }
 });
  • 37. Samples Query object: is passed to read scripts function read(query, user, request) { query.where({ userId: user.userId }); request.execute(); } Request object: is passed to all scripts request.execute(); //Executes the default behavior for the operation request.respond(); //Writes the default response request.respond(error); //Writes a response with the specified error
  • 38. Samples Tables and Table objects function insert(item, user, request) { var addressTable = tables.getTable('address'); addressTable.where({userId: user.userId}) .read({success: function (results) { item.address = results.length > 0 ? results[0] : 'unknown'; request.execute(); } }); }
  • 40. Modules Name Description azure Provides functionality to access Azure services (Blobs, Tables, Service Bus) request Sends HTTP requests to external web services, such as Twitter and Facebook. sendgrid Used to send email by using the Sendgrid email service. util Provides useful methods like format, isDate, isArray, isError xml2js XML to JavaScript object converter js2xml JavaScript to XML object converter xmlbuilder Can be used to create XML documents sax A very simple tool to parse through an XML string
  • 41. Samples Request var request = require('request'); var url = 'https://api.twitter.com/1/users/show.json? screen_name=babosbird'; request(url, function(error, response, body) { if (error) { console.error(error); } else { var data = JSON.parse(body); console.log(data); } });
  • 42. Samples SendGrid function sendEmail(text) { var sendgrid = new SendGrid('<user-name>', ('<password>'); sendgrid.send({ to: 'paolos@microsoft.com', from: ‘info@acme.com', subject: ‘ACME Newsletter', text: text }, function (success, message) { // If the email failed to send, log it as an error so we can investigate if (!success) { console.error(message); } }); }
  • 44. Portable Class Library ▪ The Portable Class Library project supports a subset of assemblies from the .NET Framework, Silverlight, .NET for Windows Store apps, Windows Phone, and Xbox 360, and provides a Visual Studio template that you can use to build assemblies that run without modification on these platforms. ▪ Mobile Services support the Portable Class Library model and this offers several advantages for developers, since they can now share the codebase of client applications between the different platforms. ▪ The Portable Class Library needs a platform extension assembly (.Ext.dll) to work correctly. ▪ We provide platform extension assemblies for: ▪ Windows Phone 7.5 ▪ Windows Phone 8 ▪ Windows Store apps.
  • 45. Portable Class Library ▪ The Portable Class Library cannot contain all the classes of the managed clients for all the platforms. ▪ Some functionality is only available on one platform, while other functionality differs between platforms. ▪ Therefore, to be able to use the new Managed Client you also need a platform specific assembly, which contains code to be able to use features available on that specific platform. ▪ UI authentication is an example of a feature that works different on different platforms and is not portable. ▪ For you as a developer this means you need two assemblies for your Windows Store App to use Mobile Services. ▪ For Windows Phone there is an additional assembly which contains functionality which is not allowed in background agents (e.g. login)
  • 46. Windows Store App App.xaml.cs using Microsoft.WindowsAzure.MobileServices; sealed partial class App : Application { // This MobileServiceClient must be configured to communicate with // your Mobile Service's url and application key. // The MobileServiceClient class provides basic access to Mobile Services. public static readonly MobileServiceClient MobileService = new MobileServiceClient("https://<your-mobile-service>.azure- mobile.net/", “XXXXXXXXXXXXXXXXXXXX"); }
  • 47. Windows Store App MainPage.xaml.cs using Microsoft.WindowsAzure.MobileServices; using Newtonsoft.Json; public class TodoItem { public int Id { get; set; } [JsonProperty(PropertyName = "text")] public string Text { get; set; } [JsonProperty(PropertyName = "complete")] public bool Complete { get; set; } } public sealed partial class MainPage : Page { private MobileServiceCollection<TodoItem, TodoItem> items; // the IMobileServiceTable interface Provides operations on a table for a Mobile Service. private IMobileServiceTable<TodoItem> todoTable = App.MobileService.GetTable<TodoItem>(); private async void RefreshTodoItems() { // This code refreshes the entries in the list view by querying the TodoItems table. items = await todoTable .Where(todoItem => todoItem.Complete == false) .ToCollectionAsync(); } }
  • 48. HttpMessageHandlers ▪In the new version of the managed client, HttpMessageHandlers replace ServiceFilters. ▪The MobileServiceClient internally uses an instance of the HttpClient to send HTTP requests and receive HTTP responses to Mobile Services REST services. ▪In a managed client, HttpMessageHandlers can be used to intercept and handle outgoing requests to Mobile Services. ▪You can define one or more handlers for a a single MobileServiceClient.
  • 49. Sample 1 public class BusyHandler : DelegatingHandler { private int callCount; private readonly Action<bool> busyIndicator; public BusyHandler(Action<bool> busyIndicator) { this.busyIndicator = busyIndicator; } protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var outgoingCount = Interlocked.Increment(ref callCount); if (outgoingCount == 1) { busyIndicator(true); } var response = await base.SendAsync(request, cancellationToken); var incomingCount = Interlocked.Decrement(ref callCount); if (incomingCount == 0) { busyIndicator(false); } return response; } }
  • 50. Sample 2 public class DebugHandler : DelegatingHandler { // Sends an HTTP request to the inner handler to send the request to the server as an async operation protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request != null && request.Method == HttpMethod.Post && request.Content != null) { Debug.WriteLine("REQUEST: {0}", await request.Content.ReadAsStringAsync()); } var response = await base.SendAsync(request, cancellationToken); if (response != null && response.Content != null) { Debug.WriteLine("RESPONSE: {0}", await response.Content.ReadAsStringAsync()); } return response; } }
  • 51. HTML 5 Client HTML Page: inset the following script references <script src='//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js'></script> <script src='https://<your-service-name>.azure-mobile.net/client/MobileServices.Web-1.0.0.min.js'></script> JavaScript File // In the editor, open or create a JavaScript file, and add the following code that defines the 
 // MobileServiceClient variable. Supply the application URL and application key from the mobile service in // the MobileServiceClient constructor. var client = new WindowsAzure.MobileServiceClient('https://<your-service-name>.azure-mobile.net/', ‘AppKey') // You get a reference to a table by calling the getTable() function on the MobileServiceClient object var todoItemTable = client.getTable('todoitem'); function refreshTodoItems() { // Filter data by including a where clause in a query that returns all items whose complete field is equal to false var query = todoItemTable.where({ complete: false }); // Reads all data from the table query.read().then(function(todoItems) { // Use jQuery and JavaScript to update controls on the page. }); }
  • 53. Authentication ▪ Mobile Services integrates with the following identity providers to make it easy to authenticate users from your app: ▪ Microsoft Account ▪ Facebook login ▪ Twitter login ▪ Google login ▪ To enable authentication, you must first register your app with one or more of these identity providers and then configure your mobile service. ▪ You must also add authentication code to your app. ▪ When a user login is authenticated by Mobile Services, the value of the userId property on the user object passed to server scripts is set to a value that uniquely identifies a user. This value can then be used to authorize access to data.
  • 54. OAuth Authentication Flow * By Carlos Figueira, see references
  • 55. Windows Store Apps private static async System.Threading.Tasks.Task Authenticate() { while (App.MobileService.CurrentUser == null) { string message; try { await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook); message = string.Format("User successfully authenticated: UserId = {0}", App.MobileService.CurrentUser.UserId); } catch (InvalidOperationException) { message = "Login failed!"; } var messageDialog = new MessageDialog(message, "Login Status"); await messageDialog.ShowAsync(); } }
  • 56. Windows Phone 8 Apps private async Task Authenticate() { while (user == null) { string message; try { user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount); message = string.Format("You are now logged in - {0}", user.UserId); } catch (InvalidOperationException ex) { message = ex.Message; } MessageBox.Show(message); } }
  • 57. HTML 5 App HTML Page <div id="logged-out"> You are not logged in. <button>Log in</button> <select id="authentication-providers"> <option selected="selected" value="MicrosoftAccount">Microsoft</option> <option>Twitter</option> <option>Facebook</option> <option>Google</option> </select> </div>
  • 58. HTML 5 App JavaScript File function logIn() { client.login($("#authentication-providers :selected") .val()) .done(refreshAuthDisplay, function (error) { alert(error); }); } function logOut() { client.logout(); refreshAuthDisplay(); $('#summary').html('<strong>You must login to access data.</ strong>'); }
  • 59. User Object // The User object which is passed to all scripts // has a function called getIdentities(), // which returns an object with provider-specific data // which can be used to query their user information. // Facebook { "facebook":{ "userId":"Facebook:<user-id>", "accessToken":“<access-token>" } } // Microsoft: { "microsoft":{ "userId":“MicrosoftAccount:<user-id>", "accessToken":“<access-token>" } } //Google: { "google":{ "userId":"Google:<user-id>", "accessToken":“<access-token>" } } //And for Twitter: { "twitter":{ "userId":"Twitter:<user-id>", "accessToken":“<access-token>", "accessTokenSecret":“<access-token- secret>" } }
  • 60. Get User Name (1/2) function getUserName() { // Set default name item.userName = '<unknown>'; // Get credentials for the authenticated user var identities = user.getIdentities(); var req = require('request'); var url; // Create url to query the authentication provider for user name if (identities.microsoft) { url = 'https://apis.live.net/v5.0/me/?method=GET&access_token=' + identities.microsoft.accessToken; } else if (identities.facebook) { url = 'https://graph.facebook.com/me?access_token=' + identities.facebook.accessToken; } else if (identities.google) { url = 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + identities.google.accessToken; } else if (identities.twitter) { url = 'https://api.twitter.com/1/users/show.json?user_id=' + user.userId.substring(user.userId.indexOf(':') + 1); }
  • 61. Get User Name (1/2) // Call authentication provider if (url) { req(url, function (error, response, body) { if (error) { console.error('An error occurred while sending data to Facebook Graph API: ', error); } else { try { var data = JSON.parse(body); item.userName = data.name ? data.name : '<unknown>'; } catch (ex) { console.error('An error occurred while parsing response data from the auth provider: ', ex); } } if (f) { f(); } }); } }
  • 62. Data Validation and Duplicate Detection
  • 63. Data Validation function insert(item, user, request) { // Validation if (!item.text) { request.respond(statusCodes.BAD_REQUEST, "The text cannot be null"); return; } }
  • 64. Duplicate Detection function insert(item, user, request) { var todoItemTable = tables.getTable("TodoItem"); todoItemTable.where({userId: user.userId, text: item.text, complete: false}) .read({ success: function(results) { if (results && results.length > 0) { console.log("Duplicate detected: userId = [" + user.userId + "] text = [" + item.text + "]") request.respond(statusCodes.OK, results[0]); } else { request.execute(); } } }); }
  • 65. Authorization function insert(item, user, request) { var permissionsTable = tables.getTable('permissions'); permissionsTable.where({ userId: user.userId, permission: 'submit order' }).read({ success: function (results) { if (results.length > 0) { // Permission record was found. Continue normal execution. request.execute(); } else { console.log('User %s attempted to submit an order without permissions.', user.userId); request.respond(statusCodes.FORBIDDEN, 'You do not have permission to submit orders.'); } } }); }
  • 66. Administer Mobile Services using Windows Azure Cross-Platform Command Line tool
  • 67. CLI ▪This tool can be used on the following platforms: ▪ Windows ▪ Linux ▪ Mac ▪Mobile Services can be managed using CLI
  • 68. Agenda ▪ 3rd Hour: Push Notifications and integration with other services ▪ Send push notifications using WNS and MPNS ▪ Windows mobile and Windows Store Apps notifications ▪ Invoke REST services via request Node.js module and HTTP protocol ▪ Integrate Mobile Services with on-premises LOB applications: ▪ Via Service Bus Relay Services ▪ Via Service Bus Queues, Topics and Subscriptions ▪ Break (15 minutes)
  • 70. Push ▪ Mobile Services can send push notifications to your apps. ▪ The way that you configure and send push notifications depends on your app. ▪ You can send push notifications to the following applications: ▪ Windows Store app by using the Windows Push Notification Service (WNS). To send push notifications to your app, you must configure your mobile service to work with WNS. ▪ iPhone and iPad apps by using the Apple Push Notification Service (APNS). To send push notifications to your app, you must configure your mobile service to work with APNS. Do this by generating a special push notification certificate at the iOS Provisioning Portal ▪ Android: You can send push notifications to Android apps by using the Google Cloud Messaging (GCM) service. To send push notifications to your app, you must configure your mobile service to work with GCM. ▪ Windows Phone 8 app by using the Microsoft Push Notification Service (MPNS). Mobile Services does not require you to register your Windows Phone 8 app to send push notifications.
  • 72. Windows Store App private static async System.Threading.Tasks.Task AcquirePushChannel() { // Create a channel to receive notifications var currentChannel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); var uriAsString = currentChannel.Uri; // Check if the channel already exists in the Channel table var channelTable = App.MobileService.GetTable<Channel>(); var enumerable = await channelTable. Where(c => c.Uri == uriAsString).ToEnumerableAsync(); if (enumerable.Any()) { // Exit if the channel already exists in the Channel table return; } var channel = new Channel { Uri = currentChannel.Uri, Type = "WNS" }; await channelTable.InsertAsync(channel); }
  • 73. Make your App Toast Capable ▪To configure the Windows Store App to receive push notifications, open the Package.appxmanife st file and make sure that in the Application UI tab, Toast capable is set to Yes
  • 74. Windows Phone 8 App private HttpNotificationChannel CurrentChannel; private void AcquirePushChannel() { CurrentChannel = HttpNotificationChannel.Find("MyPushChannel"); if (CurrentChannel == null) { CurrentChannel = new HttpNotificationChannel("MyPushChannel"); CurrentChannel.Open(); CurrentChannel.BindToShellTile(); CurrentChannel.BindToShellToast(); } var channelTable = App.MobileService.GetTable<Channel>(); var channel = new Channel { Uri = CurrentChannel.ChannelUri.ToString() }; channelTable.InsertAsync(channel); }
  • 75. Configure the App ▪To configure the Windows Phone 8 App to receive push notifications, open the WMAppManifest.xml file and make sure that in the Capabilities tab, ID_CAP_PUSH_NOTIFICATI ON is checked.
  • 76. Server Script Code function sendPushNotification() { var channelTable = tables.getTable('Channel'); channelTable.read({ success: function (channels) { channels.forEach(function (channel) { if (channel.type === "WNS") { push.wns.sendToastText02(channel.uri, { text1: "New Todo Item", text2: item.text}, {success: function (pushResponse) { console.log("Sent push:", pushResponse); } }); } if (channel.type === "MPNS") { push.mpns.sendToast(channel.uri, { text1: 'TodoList', text2: item.text }, { success: function (pushResponse) { console.log("Sent push:", pushResponse); } }); }
  • 78. Integrate Mobile Services with on- premises LOB applications
  • 79. Hybrid Solutions ▪In today’s IT landscape, it is not uncommon that data and services used by a system are located in multiple application domains. ▪Resources may be stored in a corporate data center, while other services may be located across the organizational boundaries, in the cloud or in the data centers of business partners. ▪There are many reasons for requiring a hybrid approach to cloud applications: ▪ Certain types of data come under national regulations which explicitly forbid data from being stored in another country. ▪ Data may need to be shared locally with on –premise applications and services.
  • 80. Windows Azure Service Bus ▪ The Windows Azure Service Bus is an Internet Service Bus that offers secure, scalable and highly available connectivity and messaging capabilities. ▪ The Service Bus is part of Windows Azure and is designed to provide connectivity, queuing, and routing capabilities that allow on-premises and cloud applications to exchange messages across the boundaries of logical and physical domains. ▪ The Service Bus can be used as the foundation to create a new range of hybrid and distributed applications that span the cloud and corporate environments. ▪ The Service Bus provides 2 messaging types: ▪ Relayed Messaging ▪ Brokered Messaging
  • 81. Service Bus Relayed Messaging ▪ The Service Bus allows a WCF service to expose an endpoint in a Windows Azure data center that can be accessed by consumer applications in a mediated and secure way using different transport protocols (TCP, HTTP/S) and message formats (SOAP and REST). ▪ The relay service provides a rendezvous connection point in the cloud between the client application and the service provider. ▪ Both the client and service can reside in the cloud or on- premises. ▪ The Service Bus supports the WCF programming model and provides a rich set of bindings (BasicHttpRelayBinding, NetTcpRelayBinding, NetEventRelayBinding, etc.) to cover a wide variety of design patterns and Message Exchange Formats. ▪ One-way communications ▪ Publish/Subscribe ▪ Peer-to-peer communications ▪ Multicast messaging
  • 82. Service Bus Brokered Messaging - Queues ▪ Queues provide messaging capabilities that enable a large and heterogeneous class of applications running on premises or in the cloud to exchange messages in a secure and reliable fashion across trust boundaries. ▪ Queues are based on a new messaging infrastructure backed by a replicated, durable store - up to 5GB per Queue. ▪ The max message size is 256KB, but the session feature allows creating unlimited-size sequences of related messages. ▪ Queues are available through a .NET API, WCF, HTTP/REST and AMQP Interfaces
  • 83. Queues - Capabilities ▪ Session-based message correlation. ▪ Message Scheduled Delivery. ▪ Reliable delivery patterns via Peek/Lock Receive Mode. ▪ Transactions support to ensure batches of messaging operations are committed atomically. ▪ Detection of inbound message duplicates, allowing clients to send the same message multiple times without adverse consequences (Idem-potency). ▪ Dead-letter facility for messages that cannot be processed or expire before being received. ▪ Deferring messages for later processing.
  • 84. Queues - Scenarios ▪ Loosely-Coupled Asynchronous Communication allows to establish an asynchronous and loosely-coupled communication between autonomous systems that run in different application domains, on-premises or in the cloud. ▪ Load Leveling allows to flatten a highly-variable traffic into a predictable stream of work. ▪ Load Balancing allows to distribute the load across a set of worker processes which size can vary dynamically to accommodate the incoming message volume. ▪ In a Competing Consumers scenario, when a publisher writes a message to a queue, multiple consumers compete with each other to receive the message, but only one of them will receive and process the message in question. ▪ Temporal Decoupling allows to work on independent schedules. The producers and consumers are not required to be online at the same time. This allows to publish messages to workers that are temporarily offline.
  • 85. Service Bus Brokered Messaging - Topics ▪ Topics provide all the Features of Queues + Publish/Subscribe capabilities. ▪ A Topic consists of a sequential message store just like a Queue, but it supports up to 2000 concurrent and durable subscriptions ▪ Each Subscription is a virtual queue getting message copies ▪ A Subscription may have a single consumer that gets all the messages or a set of competing consumers that fetch messages on a first-come-first-served basis. ▪ Subscriptions can have filters expressions and filter actions
  • 86. Topics - Scenarios ▪Publish Events to many subscribers ▪Each subscription can be shared by a set of competing consumers ▪Secondary subscriptions can be used for auditing purposes. ▪Distribute workload across partitioned pools of workers, each defined by a different subscription ▪Decoupled message fan-out scenario to send messages to many consumers requiring the same information ▪As the foundation in event-driven architecture implementations
  • 87. Topics – Rules & Filters ▪Rules: ▪ Rules select messages based on conditions defined on system or application-specific properties ▪ Topics support more than one rule for each subscription ▪Filter Conditions and Actions: ▪ Conditions are represented by filter expressions expressed in SQL’92 syntax: ▪ ShipCountry=“Ukraine” AND ShipCity=“Lviv” ▪ Actions are expressed by action expressions and can add and modify message properties during retrieval ▪ Set Priority='High';Set Severity=1
  • 88. Messages ▪ Messages are modeled by the BrokeredMessage class ▪ Messages can have user-defined: ▪ Time-To-Live periods (with no enforced maximum lifetime) ▪ Lock duration ▪ Duplicate detection history time ▪ Messages expose the following properties: ▪ MessageId that can be used for duplicate detection ▪ CorrelationId to correlate a message to a specific request ▪ SessionId for session-enabled communication ▪ ReplyTo that can be used to implement a request-reply MEP. ▪ Label for custom use ▪ User defined properties are key/value pairs ▪ Brokered messaging properties are not SOAP headers
  • 90. How to call a Service Bus relay service (1/3) function getUserAddress(f) { // Set default value item.userAddress = '<unknown>'; // Create the SOAP Envelope var body = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' + '<s:Body>' + '<user xmlns="http://windowsazure.cat.microsoft.com/samples/ mobileservices">' + '<userId>' + user.userId + '</userId>' + '</user>' + '</s:Body>' + '</s:Envelope>'; // Import the request module var req = require('request'); * You can the xmlbuilder, sax and js2xml Node.js modules to create the SOAP messag
  • 91. How to call a Service Bus relay service (2/3) // Create call options var options = { method: 'POST', url: 'http://paolosalvatori.servicebus.windows.net/basichttp/todoitem', headers: { 'Content-Type': 'text/xml', 'SOAPAction': 'GetUserAddress' }, body: body }; // Call the Service Bus Relay Service req(options, function(error, response, body) { if (error) { console.log('An error occurred while invoking the service: ', error); } else { // Import xml2js module var xml2js = require('xml2js'); // Create a xml2js parser var parser = new xml2js.Parser();
  • 92. How to call a Service Bus relay service (3/3) // Parse the SOAP envelope of the response from XML to JSON parser.parseString(body, function (err, result) { try { // Read the address from the JSON object var address = result["s:Envelope"]["s:Body"][0]["userAddress"][0]["text"] [0]; if (address) { // Set the userAddress property of the item item.userAddress = address; } } catch (ex) { // Log the error console.log("An error occurred while parsing the response: ", error); } }); if (f) { // Invoke a continuation function, if any f(); } } }); }
  • 93. How to use Send a message to queue (1/2) function sendMessageToServiceBus() { // Import the azure module var azure = require('azure'); // Create a ServiceBusService object var serviceBusService = azure.createServiceBusService ('<your-service-bus-namespace-name>', '<your-service-bus-namespace-key>'); // Define queue options var queueOptions = { EnableBatchedOperations: true, RequiresDuplicateDetection: true, DuplicateDetectionHistoryTimeWindow: 'PT8H' };
  • 94. How to use Send a message to queue (2/2) // Create queue if it does not exist serviceBusService.createQueueIfNotExists('mobileservices/todoitem', queueOptions, function (error) { if (error) { console.error("An error occurred creating/accessing the Service Bus queue: ", error); } else { // Create a message var message = { body: 'Hello World!', messageId: item.id, customProperties: { source: 'Mobile Services', author: 'Paolo Salvatori' } }; // Send the message to the queue serviceBusService.sendQueueMessage('mobileservices/todoitem', message, function (error) { if (!error) { console.log('Sent message: ' + message); } }); } }); }
  • 96. DEMO Integrate Mobile Services with on- premises LOB applications
  • 97. Agenda ▪ 4th Hour: Storage Services, Scheduler, SendGrid ▪ Store data to Table Storage using the azure Node.js module ▪ Use Shared Access Signatures and Blobs with Mobile Services ▪ Create and schedule jobs with the scheduler ▪ Send emails using SendGrid Node.js module ▪ Break (15 minutes)
  • 99.
  • 100. Choose the PartitionKey and RowKey wisely ▪ Tables are partitioned to support load balancing across storage nodes. ▪ A table's entities are organized by partition. ▪ A partition is a consecutive range of entities sharing the same partition key value. ▪ The partition key is a unique identifier for the partition within a given table, specified by the PartitionKey property. ▪ The partition key forms the first part of an entity's primary key. ▪ The second part of the primary key is the row key, specified by the RowKey property. ▪ The row key is a unique identifier for an entity within a given partition. ▪ Together the PartitionKey and RowKey uniquely identify every entity within a table. ▪ An entity can have up to 255 properties, including 3 system properties described in the following section.
  • 101. Insert Script (simplified) function insert(item, user, request) { // Access table service var azure = require('azure'); var tableService = azure.createTableService('<your-storage-account-name>', '<your-storage-account-key>'); // Create table if not exists tableService.createTableIfNotExists('TodoItems', function(error) { if (error) { request.respond(statusCodes.BAD_REQUEST, error); } else { var entity = { PartitionKey: user.userId || 'default', RowKey: (new Date()).getTime(), text: item.text, complete: item.complete, }; // Insert the item in the storage table tableService.insertEntity('TodoItems', entity, function (error) { if (error) { request.respond(statusCodes.BAD_REQUEST, error); } else { request.respond(statusCodes.OK, entity); } }); } }); }
  • 103. Shared Access Signatures ▪ A shared access signature (SAS) is a URI that grants restricted access rights to containers, blobs, queues, and tables. ▪ You can provide a SAS to clients that are not trusted to use the storage account key but to whom you wish to delegate access to certain storage account resources. ▪ By distributing a SAS URI to these clients, you can grant them access to a resource for a specified period of time, with a specified set of permissions. ▪ A shared access signature can grant any of the following operations to a client that possesses the signature: ▪ Reading and writing page or block blob content, block lists, properties, and metadata ▪ Deleting, leasing, and creating a snapshot of a blob ▪ Listing the blobs within a container ▪ Adding, removing, updating, and deleting queue messages ▪ Getting queue metadata, including the message count ▪ Querying, adding, updating, deleting, and upserting table entities
  • 104. Blobs and Shared Access Signature 1. The Windows Store app creates a new album 2. The Mobile Service insert a new record in the Album table 3. Thw Windows Store app creates a new picture 4. The Mobile service calls the storage account to get a SAS for the image and its thumbnail 5. The Mobile Services saves the image data in the Picture table 6. The Insert script returns a SAS for the image and thumbnail in the item object 7. The Windows Store app uses the storage account client API and the SAS returned by the Mobile Service to save the image and thumbnail as blobs. *Sample by Nick Harris, see references for mo
  • 105. Server Script and SAS ▪ The insert script uses the following line of code to build a SAS URL: ▪ return 'https://' + host + blobRelativePath + '?' + sasQueryString; ▪ A typical SAS query string contains many parameters which depend on the storage resource (blob, container, table, queue) ▪ se=2013-05-21T15%3A37%3A15Z&sr=b&sp=w&sig=dj9zqN%2BSM1UX ▪ se = signedexpiry: the time at which the shared access signature becomes invalid ▪ sr = signedresource: b to grant access to a blob, c to grant access to a container ▪ sp = signedpermissions: the permissions associated with the Shared Access Signature (w = write) ▪ sig = signature: the signature part of the URI is used to authenticate the request made with the SAS. ▪ See http://msdn.microsoft.com/en-us/library/windowsazure/dn140255.aspx
  • 107. What is the Azure Scheduler service? ▪Platform service that allows users a simple way to schedule recurring actions against other services in the cloud ▪Examples: ▪ Store a daily backup of my SQL database to blob storage. ▪ Clean up my To Do app’s done items every day ▪ Check the health of my service every few minutes ▪ Send a newsletter every day
  • 108. Azure Scheduler Capabilities ▪Azure Portal integrated experience ▪Actions can be scheduled to fire once or on a regular recurring schedule ▪Reliably and securely invoke services inside and outside of Azure using Node.js modules
  • 109. Use a Job to backup a SQL Database 1. The scheduler calls the backup job 2. The backup job uses the request module to invoke the WA SQL Database Import/Export service by calling its REST endpoint 3. The Import/Export service reads the DB schema and data 4. The service exports DB schema and data to Windows Azure Blob Storage as a BACPAC *Sample by Josh Twist, see references for mor
  • 110. Code (1/2) function backup() { var request = require('request'); var util = require('util'); var date = new Date(); var year = date.getUTCFullYear(); var month = date.getUTCMonth() + 1; var day = date.getUTCDate(); var body = { BlobCredentials: { __type: "BlobStorageAccessKeyCredentials:#Microsoft.SqlServer.Management.Dac.ServiceTypes", Uri: util.format("https://<your-storage-account-name>.blob.core.windows.net/bacpac/%s-%s-%s.bacpac", year, month, day), StorageAccessKey: "<your-storage-account-key>" }, ConnectionInfo: { DatabaseName: "<your-database-name>", Password: "<your-server-password>", ServerName: "<your-server-name>.database.windows.net", UserName: “<your-server-username>" } }
  • 111. Code (2/2) var postUri = "https://am1prod-dacsvc.azure.com/DACWebService.svc/Export"; request.post({ uri: postUri, headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(body) }, function (e, r, b) { if (e || r.statusCode != 200) { console.error('backup failed', e || r.statusCode, b); } else { console.log('backup successful', b); } }); }
  • 113. Send a Report via Email 1. The scheduler calls the job function 2. The script reads data from SQL tables and/or Storage tables 3. The script aggregates and formats data in a report. 4. The script sends the report via email using the the sendgrid Node.js module
  • 114. Code (1/3) function CountTodoItems() { var SendGrid = require('sendgrid').SendGrid; // Get access to the storage table var azure = require('azure'); var tableService = azure.createTableService('<your-storage-account-name>', '<your-storage-account-name>'); // Define query to retrieve todo items var tableQuery = azure.TableQuery .select() .from('TodoItems') .where('complete eq ?', 'false'); // Retrieve todo items queryEntities();
  • 115. Code (2/3) function queryEntities() { tableService.queryEntities(tableQuery, function (error, entities) { if (error) { var message = 'Failed to retrieve todo items from the TodoItems table.'; console.error(message); } else { var text = 'The TodoItems table actually contains ' + entities.length + ' rows:rnrn'; for (var i = 0; i < entities.length; i++) { text = text + ' - UserId: ' + entities[i].PartitionKey; text = text + ' Item: ' + entities[i].text + 'rn'; } text = text + 'rnMessage sent by TodoItems site at ' + (new Date()).toString(); sendEmail(text); console.log(text); } }); }
  • 116. Code (3/3) function sendEmail(text) { var sendgrid = new SendGrid('babosbird', 'Trustno1'); sendgrid.send({ to: 'paolos@microsoft.com', from: 'leprino@hotmail.com', subject: 'Number of Todo Items', text: text }, function (success, message) { // If the email failed to send, log it as an error so we can investigate if (!success) { console.error(message); } }); } }
  • 117. DEMO Send a Report via Email
  • 118. Trigger a Worker Role with a Service Bus Msg 1. The scheduler calls the job function. 2. The script creates and send a message to a Service Bus queue or topic. 3. One instance of the Worker Role reads the message from the queue/ subscription. 4. The Worker Role instance processes the message.
  • 119. Code function SendMessageToServiceBus() { var azure = require('azure'); var serviceBusService = azure.createServiceBusService('paolosalvatori', 'PgTi69pNLU2Me58C3xiEDT5xosclhfEgQs6NsUCY9Qs='); serviceBusService.createQueueIfNotExists('mobileservices/scheduler', function (error) { if (!error) { var builder = require('xmlbuilder'); var root = builder.create('job', { 'version': '1.0', 'encoding': 'UTF-8' }) .att('xmlns', 'http://windowsazure.cat.microsoft.com/samples/mobileservices'); root.ele('command', 'read'); root.ele('date', (new Date()).toUTCString()); var message = root.end({ 'pretty': true, 'indent': ' ', 'newline': 'n' }); serviceBusService.sendQueueMessage('mobileservices/scheduler', message, function (error) { if (!error) { console.log('Sent message: ' + message); } }); } }); }
  • 120. DEMO Trigger a Worker Role with a Service Bus Msg
  • 121. References ▪ Windows Azure Mobile Services ▪ http://www.windowsazure.com/en-us/develop/mobile/ ▪ Tutorials and Resources ▪ http://www.windowsazure.com/en-us/develop/mobile/resources-html/ ▪ Upload File to Windows Azure Blob Storage using Windows Azure Mobile Services ▪ http://code.msdn.microsoft.com/Upload-File-to-Windows-c9169190 ▪ Getting user information on Azure Mobile Services ▪ http://blogs.msdn.com/b/carlosfigueira/archive/2012/10/25/getting-user-information-on-azure-mobile-services.aspx ▪ Troubleshooting authentication issues in Azure Mobile Services ▪ http://blogs.msdn.com/b/carlosfigueira/archive/2012/10/23/troubleshooting-authentication-issues-in-azure-mobile-services.aspx
  • 122. References ▪ Using the scheduler to backup your Mobile Service database ▪ http://www.thejoyofcode.com/Using_the_scheduler_to_backup_your_Mobile_Service_database.aspx ▪ Getting started with the CLI and backing up your scripts ▪ http://www.thejoyofcode.com/Getting_started_with_the_CLI_and_backing_up_your_scripts_Day_4_.aspx ▪ More CLI – changing your Mobile Services workflow ▪ http://www.thejoyofcode.com/More_CLI_ndash_changing_your_Mobile_Services_workflow_Day_5_.aspx ▪ New Windows Azure Mobile Services Getting Started Content ▪ http://blogs.msdn.com/b/windowsazure/archive/2013/01/09/new-windows-azure-mobile-services-getting-started-content.aspx ▪ How to use Windows Azure Table Storage in Windows Azure Mobile Services ▪ http://code.msdn.microsoft.com/windowsazure/Use-Table-Storage-in-0b6bcbc7
  • 123. References ▪ How to Use the Table Service from Node.js ▪ http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/table-services/ ▪ How to Use Service Bus Queues ▪ http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/service-bus-queues/ ▪ How to Use Service Bus Topics/Subscriptions ▪ http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/service-bus-topics/ ▪ How to Send Email Using SendGrid from Node.js ▪ http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/sendgrid-email-service/
  • 124. Blogs ▪Josh Twist Blog ▪ http://www.thejoyofcode.com/ ▪Nick Harris Blog ▪ http://www.nickharris.net/ ▪Carlos Figuera ▪ http://blogs.msdn.com/b/carlosfigueira/ ▪Paolo Salvatori Blog ▪ http://blogs.msdn.com/b/paolos
  • 125. Contacts Paolo Salvatori Email: Paolos@Microsoft.com Blog: http://blogs.msdn.com/b/paolos/ Twitter: @babosbird
  • 126. © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.