Azure Mobile App Services Implementation

In this article, you will learn about Azure Mobile App Services implementation.

What Is Azure Mobile App Services?

 
Azure App Service is a fully-managed Platform-as-a-Service (PaaS) offering for professional developers. The Mobile Apps feature of Azure App Service gives enterprise developers and system integrators a mobile-application development platform that's highly scalable and globally available.
 
The advantages are:
 
(For more information please refer to this link: Reference Link.)
  1. Build engaging mobile apps fast  
  2. Quickly add corporate sign-in  
  3. Use offline data sync to build responsive apps 
  4. Connect your apps to on-premises data 
  5. Auto scale to fit your business
  6. Broadcast personalized push notifications to millions in minutes 

Create Azure Mobile Service Through Visual Studio

 
Note

Please install Azure SDK in your Visual Studio. If you don't have it installed please update your Visual Studio with cloud features.
 
In your Visual Studio right-click your project, click Manage NuGet Packages, search for the Microsoft.Azure.Mobile.Client package, then click Install.
 
In your main activity file, remember to add the following using statement:
  1. using Microsoft.WindowsAzure.MobileServices;  
Step 1
 
First, select ASP .Net Web Application, rename the project name as you like. Please refer to the below figure.
 
 
The second step is to select the Azure Mobile App and click on the Ok button. Please refer to the below figure.
 
 
Now your solution is ready to run by the default "TodoItemController" controller. Run your solution. Refer to the below figure.
 
 
 
Step 2
 
Please add the below tag in webconfig file under ConnectionStrings tag. The below connection string is pointing to our Database. (For security reasons, I am not mentioning my db credentials; please use your appropriate database credentials).
  1. <add name="MS_TableConnectionString" connectionString="Server=;Database=;User ID=;Password=;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;" providerName="System.Data.SqlClient" />  
Step 3
 
Right-click on the DataObjects folder and select the Add option and create the class. 
 
 
 
I have created a class and named it AzureMobileAppDemoTbl. Please see the below figure.
 
 
 
Class IS 
  1. using Microsoft.Azure.Mobile.Server;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6.   
  7. namespace AzureMobileAppServices.DataObjects  
  8. {  
  9.     public class AzureMobileAppDemoTbl : EntityData  
  10.     {  
  11.         public int studentid { getset; }  
  12.         public string name { getset; }  
  13.     }  
  14. }  
In the above code AzureMobileAppDemoTbl is inherited EntityData abstract class. EntityData class is implementing ITableData Interface.
 
 
What is ITableData Interface?
 
Microsoft.Azure.Mobile.Server.Tables.ITableData provides an abstraction indicating how the system properties for a given table data model are to be serialized when communicating with the clients. The uniform serialization of system properties ensures that the clients can process the system properties uniformly across platforms. It contains default properties such as Id, Version, CreatedAt, UpdatedAt, Deleted.
 
As of now in our class "AzureMobileAppDemoTbl" we have 2 properties such as student id and name. But we inherited EntityData class and for that reason, our class has the properties of EntityData(base) class also.
 
Step 4
 
Now we are creating a controller. Right-click on the "Controllers" folder and go for creating a controller.
 
Please check the below figure. Select Azure Mobile Apps table controller.
 
 
On the click of the Azure Mobile Apps Table controller, you will get one small window and in that, we need to -
  • Choose a Model (it means our DTO Object)
  • Select a Mobile Service Context.
  • Specify the controller name. 
 
 
Once you click on the Add Button, it will start to do the Scaffolding process. After that, it will create AzureMobileAppDemoTblController with Post, Get, Patch, Delete methods.
 
The important thing is we selected Azure Mobile Apps Table controller and mapped to AzureMobileAppDemoTbl class right, so with the help of the code first approach it will create one table having AzureMobileAppDemoTbl class properties as columns. 
 
 
Code snippet of  "AzureMobileAppDemoTbl Controller" 
  1. using System.Linq;  
  2. using System.Threading.Tasks;  
  3. using System.Web.Http;  
  4. using System.Web.Http.Controllers;  
  5. using System.Web.Http.OData;  
  6. using Microsoft.Azure.Mobile.Server;  
  7. using AzureMobileAppServices.DataObjects;  
  8. using AzureMobileAppServices.Models;  
  9.   
  10. namespace AzureMobileAppServices.Controllers  
  11. {  
  12.     public class AzureMobileAppDemoTblController : TableController<AzureMobileAppDemoTbl>  
  13.     {  
  14.         protected override void Initialize(HttpControllerContext controllerContext)  
  15.         {  
  16.             base.Initialize(controllerContext);  
  17.             MobileServiceContext context = new MobileServiceContext();  
  18.             DomainManager = new EntityDomainManager<AzureMobileAppDemoTbl>(context, Request);  
  19.         }  
  20.            
  21.   
  22.         // GET tables/AzureMobileAppDemoTbl  
  23.         public IQueryable<AzureMobileAppDemoTbl> GetAllAzureMobileAppDemoTbl()  
  24.         {  
  25.             return Query();   
  26.         }  
  27.   
  28.         // GET tables/AzureMobileAppDemoTbl/48D68C86-6EA6-4C25-AA33-223FC9A27959  
  29.         public SingleResult<AzureMobileAppDemoTbl> GetAzureMobileAppDemoTbl(string id)  
  30.         {  
  31.             return Lookup(id);  
  32.         }  
  33.   
  34.         // PATCH tables/AzureMobileAppDemoTbl/48D68C86-6EA6-4C25-AA33-223FC9A27959  
  35.         public Task<AzureMobileAppDemoTbl> PatchAzureMobileAppDemoTbl(string id, Delta<AzureMobileAppDemoTbl> patch)  
  36.         {  
  37.              return UpdateAsync(id, patch);  
  38.         }  
  39.   
  40.         // POST tables/AzureMobileAppDemoTbl  
  41.         public async Task<IHttpActionResult> PostAzureMobileAppDemoTbl(AzureMobileAppDemoTbl item)  
  42.         {  
  43.             AzureMobileAppDemoTbl current = await InsertAsync(item);  
  44.             return CreatedAtRoute("Tables"new { id = current.Id }, current);  
  45.         }  
  46.   
  47.         // DELETE tables/AzureMobileAppDemoTbl/48D68C86-6EA6-4C25-AA33-223FC9A27959  
  48.         public Task DeleteAzureMobileAppDemoTbl(string id)  
  49.         {  
  50.              return DeleteAsync(id);  
  51.         }  
  52.     }  
  53. }  
After doing this process the API is ready for making a CRUD operation with mapped table. Let's check with help of postman.
 
Note
While calling the API  in the header we need to pass the  API version keys. If you are not passing the API version at the header it means it will throw an exception. 
 
Please refer to the below figure.
 
 
So please pass the below keys:
 
  1. "key":"ZUMO-API-VERSION","value":"2.0.0"   
 
Insert Query
 
URL:-Http://localhost:64729/tables/AzureMobileAppDemoTbl
Method:-POST
 
Please check the bellow figure. We are calling API and the method is POST. In the body, we are posting data that needs to insert to a particular table.
 
 
 
 
Please check below figure. Please run the below command in your SQL server. 
 
  1. select * from [dbo].[AzureMobileAppDemoTbl]  
 
Select Query
 
URL:-Http://localhost:64729/tables/AzureMobileAppDemoTbl
Method:-GET
Please check the below figure. We are calling the API with the "Get" method.
 
 
Update Query
 
URL:-Http://localhost:64729/tables/AzureMobileAppDemoTbl?id=3688e7c5d6dc41edaca5d6e4338f0226
Method:-PATCH
Query Parameter:- Id represents the primary key of the table. 
 
Please check the below figure. 
 
 
Delete Query
 
URL:-http://localhost:64729/tables/AzureMobileAppDemoTbl?id=3688e7c5d6dc41edaca5d6e4338f0226
Method:-DELETE
 
Query Parameter: ID represents the primary key of the table.  
 
Please check the below figure.
 
 
Note
In Azure Mobile Apps Service you can create a custom controller also. Please check the below figure.
 
 
 

Summary

 
In this blog, we discussed the following.
  • Azure Mobile Apps Service
  • How to create API's using Azure Mobile Apps Table controller
  • Tested API through Postman.
I hope it's helpful. Eat->Code->Sleep->Repeat.