Friday 8 August 2014

AZURE MOBILE SERVICES FOR EVERY DEVELOPER


Overview
Mobile services is one of the wonderful thing i have seen on azure in 2014. These services are build on top of the existing Azure infrastructure and the service is hosted by a Web Role and data is stored on a SQL Database, they’re just simpler for the developer to create and to interact with.Windows Azure Mobile Services are designed to create highly-functional mobile applications with ease .

Explanation & Implementation
Windows Azure Mobile Services brings together a set of services that enable backend capabilities for your apps and allows you to make rapid app development possible from storage, authenticate users, to push notifications. With SDKs for Windows, Android, iOS, and HTML as well as a powerful and flexible REST API, Mobile Services lets you to build inter connected and deliver a consistent experience across devices.If you will observe below screen shot ,you will find that

Mobile Services Capabilities
 Mobile Services provides the following backend capabilities in Azure to support your apps:
  • Simple provisioning and management of tables for storing application data.
  • Integration with notification services to deliver push notifications to your application.
  • Integration with other cloud services.
  • Service monitoring and logging. 
  • Create a straightforward and secure backed-as-a-service to handle common tasks and get relieved from burden of focusing on the front end that users much bother about.  
  • Integration with Visual Studio. Your favorite IDE now contains a dedicated project template and scaffolders for Mobile Services, and has first-class support for publishing and remote debugging baked in.
Activating the feature
As discussed in my previous article Here that the first thing you’ll need is an Azure subscription:

Creating the service
Now that the feature is enabled, we can start using it from the new Azure Management portal: press the New button and choose Create in the Mobile Services section. In the first step of the wizard you’ll be asked to choose:

  • A name for the service (it will be the first part of the URL, followed by the domain azure-mobile.net (In my case it is, GetAllDevices.azure-mobile.net)

  • This Step is about database where we will be forced to select Create a new SQL database, unless you already have other SQL Azure instances.
  • The region where the service will be hosted: choose the closest region to your country
  • Just click on url or simply browse to URL. You will see below screen which clearly shows that service is up and browsable


Azure gives option to choose database while creating mobile services Make sure you fallow below steps:
  • The name of the database.
  • The server where to store the database (use the default option, that is New SQL Database server).
  • Credentials(UserName and password) of the user that will be used to access to the database.
  • The region where the database will be hosted:

This is all about mobile service and we ’re done!. Now service is up and running! If you go to the URL that you’ve chosen in the first step you’ll see a welcome page. This is the only “real” page you’ll see: we have created a service, specifically it’s a standard REST service. What Next ?

Next we’ll see how we will be able to do operations on the database simply by using standard HTTP requests.

Azure Mobile Services provides a great way for mobile developers to add a cloud-hosted backend to their app. The service now has full support for writing your backend logic using ASP.NET Web API. Mobile Services presents an attractive choice for developers building mobile facing APIs with ASP.NET:

 Once the service is created, Go to Quickstart tab and download the starter project for the client platform you wish to target.once downloaded you will find service started project solution ready here


NOTE: Below implementation requires Visual Studio 2013 Update 2 or above.
 
Alternatively, you can create a local project first and create the mobile service later when you want to publish your project.



Either way what you’ll get is a Mobile Services .NET template project. Notice this is simply a Web API project with few additional NuGet packages used.


Open the TodoItemController.cs controller file and look into  its content. Better,just set a breakpoint inside the GetAllTodoitems() method  and watch how to work with data using Mobile Services .NET support:
Notice we already scaffold all the key CRUD methods for the TodoItem resource.
public class TodoItemController : TableController
{
    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
        csharp_testContext context = new csharp_testContext();
        DomainManager = new EntityDomainManager(context, Request, Services);
    }

    // GET tables/TodoItem
    public IQueryable GetAllTodoItems()
    {
        return Query();
    }

    // GET tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public SingleResult GetTodoItem(string id)
    {
        return Lookup(id);
    }

    // PATCH tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task PatchTodoItem(string id, Delta patch)
    {
        return UpdateAsync(id, patch);
    }

    // POST tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public async Task PostTodoItem(TodoItem item)
    {
        TodoItem current = await InsertAsync(item);
        return CreatedAtRoute("Tables", new { id = current.Id }, current);
    }

    // DELETE tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task DeleteTodoItem(string id)
    {
        return DeleteAsync(id);
    }
}

Just replace above controller name with DeviceController.CS  and code with operations you want to ... here is what i am doing.. 

public class DeviceController : TableController
{
    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
        csharp_testContext context = new csharp_testContext();
        DomainManager = new EntityDomainManager(context, Request, Services);
    }

    // GET tables/Device
    public IQueryable GetAllDevices()
    {
        return Query();
    }

    // GET tables/Device/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public SingleResult GetDevice(string id)
    {
        return Lookup(id);
    }

    // PATCH tables/Device/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task PatchDevice(string id, Delta patch)
    {
        return UpdateAsync(id, patch);
    }

    // POST tables/Device/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public async Task PostDeviceTodoItem item)
    {
        TodoItem current = await InsertAsync(item);
        return CreatedAtRoute("Tables", new { id = current.Id }, current);
    }

    // DELETE tables/Device/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task DeleteDevice(string id)
    {
        return DeleteAsync(id);
    }
}
Notice we already scaffold all the key CRUD methods for the TodoItem resource.

With Mobile Services .NET support you can run Mobile Services backend locally and debug your backend logic. Hit F5, on the default page choose “try it out”. Mobile Services .NET support comes with a help page for your Web API, as you would expect. Click on the GET tables/TodoItem for me GetAllDevices to bring up method documentation, as well as the test client. Click on the “try this out” link and then press “send” to invoke the GetAllDevices() method. As you expect, you will hit the breakpoint you’ve set up earlier.


Once you are done developing your backend API, you can publish your Web API to the Mobile Service. Publishing support is built right into Visual Studio, simply right-click the project and select “Publish”. You can pick an existing mobile service or create a new one right from within Visual Studio, without having to go the Azure portal.

Conclusion:Writing mobile service has become fun a.The service now has full support for writing your backend logic using ASP.NET Web API. Mobile Services presents an attractive choice for developers building mobile facing APIs with ASP.NET especially integration with enterprise level application.