Part 2: Azure Mobile Services: What you need to know to get started with Storage Services




How Azure Mobile Services enable your Cloud First Mobile First World is a generic overview of Azure Mobile Services and the various features that provide Mobile-Backend-As-A-Service (MB-a-a-S). In this article, I will dig deeper into Storage Services. Through this article, I will introduce you to the basics of setting up the backend of your app to utilize the storage services:

Storing Data in Cloud using Mobile Services:

Azure Mobile Services uses SQL data bases to store data in the form of Blob Storage or Table Storage which provide a highly scalable and flexible platform. Table Storage is NoSQL implementation where as Blob storage is used for images and videos.  Storing data in the cloud is easy and intuitive. The basic steps involved are as follows:

  1. Create Your Mobile Service
  2. Make the connection to your app and the cloud backend (which is your mobile service)
  3. Create the data storage on your cloud.
  4. Design and Store data on your cloud

Step 1: Create your Mobile Service


Go to the Azure Management Portal, click on New-> Compute-> Mobile Service –> Create. This will guide you through creating your Mobile service and associating this service to a storage account. Once your mobile service is created, you need to proceed to Step 2

Step 2: Make the connection to your app and Mobile Service

Now that the mobile service is created, it is important to connect your app. What is required is the Application Key, which can be found in Mobile Service Created-> Dashboard –> Manage Keys

imageNow, if you are using C# to develop a Windows app, you will need to add the following connection parameters to the App.xaml.cs file.

 1: private static MobileServiceClient MobileService = new MobileServiceClient( 
 2:            "AppUrl", 
 3:            "AppKey");
 4: //AppUrl can be obtained from Dashboard
 5: //Appkey is obtained as shown in the above diagram


Step 3: Create Data Storage on the cloud


If you have just created your mobile service, you will have to create a New Table by Clicking on Create, under the Data Tab (this tab appears if you have chosen Javascipt as your backend). As you can see in the screenshot above, you have the option to set permissions to access the data. This is important and shall be explained in detail in a future tutorial about Authentication and Authorization.


Step 4: Design and store data on your cloud

Once the table is created, there are 2 things that can be done:

  • Add code in the server backend , the mobile service, for the specific options of insert, update, delete and read
  • Add code in the client app to store data in the table

The code in the client app is primarily used to handle data from the client to the backend in the cloud. However, the server backend also gives you the option to customize the 4 operations of insert, update, delete and read.


Once you have created the table, you can go into the table and click on Script. There are 4 operations as described above and there can an associated script for each of these (select from the dropdown menu). It is here that you can customize your script as per the need of your app. Whether you want to use Blob Storage or Table Storage depends on the type of service that you create using the Azure Mobile Service Library as shown below:

 1: //Create Blob Storage
 2: function insert(item, user, request) {
 4:     var accountName = 'accountname';
 5:     var accountKey = 'accountkey';
 6:     var host = accountName + '';
 7:     var blobService = azure.createBlobService(accountName, accountKey, host);
 8: ....
 9:     }
 11: //Create Table Storage
 12: function insert(item, user, request) {
 14:     var accountName = 'accountname';
 15:     var accountKey = 'accountkey';
 16:     var host = accountName + '';
 17:     var tableService = azure.createTableService(accountName, accountKey, host);
 18: .....
 19:     }

The same customizability is possible with the other 3 operations: delete, update and read. Upload images to Azure Storage by using Mobile Services describes the process in detail for Blob Storage. Get Started with Data in Mobile Services clearly lays out how the client side can be designed to store data in the cloud. It also shows the cross platform capability of Azure Mobile Services by allowing you to chose the different platforms and backend as per your requirements.

Dynamic Schematization

Dynamic Schematization is one of the key features of Table Storage. Using this feature, one does not need to model the data from beforehand. The idea is zero impedance development, just get directly into coding, throwing your objects in the table without determining/modeling the schema which is ideally suited for a scalable environment. 


In the management portal of your Mobile Service, if you click Configure, you get the option to enable/disable dynamic schematization. 


Asynchronous Programming

The basic idea of asynchronous programming is intended to help with offloading long IO operations off the UI thread. In C#, async-await is the construct which enables asynchronous programming. This is key to developing cloud applications.

The beginning of any async method is executed synchronously till it reaches the ‘awaitable’, the single operator or parameter of the keyword await. Once the ‘awaitable’ starts executing, the method becomes asynchronous by suspending the method until the execution of the ‘awaitable’, while the main thread resumes execution. Once the ‘awaitable’ finishes execution, the remainder of the method resumes execution. Note: the async- await have to be used in conjunction. An ‘async’ method has to have one or more ‘await’ operations and cannot be executed in a method that is not marked ‘async’. Let us have a look at an example of how it works:

 1: private void InsertTodoItem(TodoItem todoItem)
 2: {
 3:     // This code inserts a new TodoItem into the database. When the operation completes, the item is added to the CollectionView 
 5:     todoItem.Id = Guid.NewGuid().ToString();
 7:     items.Add(todoItem);
 9: }

Now if we have to convert this to a cloud app, where the data is to be stored in the cloud rather than on local database, we will need to use asynchronous programming to ensure that while the insert operation continues, the application is not hung. There are a few libraries for Azure Mobile Services which allows us to do that:

 1: using Microsoft.WindowsAzure.MobileServices; 
 3: private async void InsertTodoItem(TodoItem todoItem)
 4: {
 5:             // Commenting this line out as Mbile Services auto-generates the ID. 
 6:             //todoItem.Id = Guid.NewGuid().ToString();
 8:             // This code inserts a new TodoItem into the cloud database. When the operation completes 
 9:             // and Mobile Services has assigned an Id, the item is added to the CollectionView 
 11:             await todoTable.InsertAsync(todoItem);  
 13:             items.Add(todoItem);
 14: }





Check C# async and await: Why Do We Need Them? to understand more about Async programming in C#


That covers the basics of what you need to know about Storage Services in Azure Mobile Services to get started. We covered the steps of creating a Mobile Service, how structured storage is executed and used in Mobile Services which was followed by key concepts such as Dynamic Schematization and Async-await. The next part I will dig into Notification Hub and its capabilities. Let me know how you get started with Mobile Services. Tweet me at @AdarshaDatta or @cdndevs to share your stories with me. 

Comments (0)

Skip to main content