Designing Your ASP.NET Web API Services for Windows Azure Mobile Services Hosting

Windows Azure Mobile Services now support .NET as a back end, based on the Windows Azure updates released on February 20, 2014. Although this is still in preview, it essentially means that you can now use your ASP.NET Web API services to be deployed as Mobile Services in Azure and leverage features such as Push Notifications and Mobile Authentication. Let us explore how you can design your ASP.NET Web API controllers to be deployed as a Mobile Service.

The first thing you need to do is to install the Windows Azure Mobile Services Backend NuGet package. Note that you will not be able to find the packages in NuGet, since they are in pre-release mode. Launch the Package Manager Console, and type the following commands to install the required packages in your solution.

Install-Package WindowsAzure.MobileServices.Backend –PreInstall-Package WindowsAzure.MobileServices.Backend.Tables -PreInstall-Package WindowsAzure.MobileServices.Backend.Entity -Pre

In addition, you will also need to use NuGet to upgrade the Web API framework to the pre-release version of ASP.NET Web API using the command:

Install-Package Microsoft.AspNet.WebApi -Pre

Next step would be to configure a starting point for kicking off the Mobile Service configuration process. For this you need to modify the Register method in the WebApiConfig class as follows:

public static void Register()        {            // Web API configuration and services            // Use this class to set configuration options for your mobile service            ConfigOptions options = new ConfigOptions();            // Use this class to set WebAPI configuration options            HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));                   }

Call the register method in your application start.

public class WebApiApplication : System.Web.HttpApplication    {        protected void Application_Start()        {            WebApiConfig.Register();        }    } 

The mobile service configuration uses a bunch of settings information, in the Web.config, that are overridden by the values specified in the portal.

                                        

That is it. You can start using Web API Controllers as a mobile service. In addition, you can also use the default TableController class that comes with Azure Mobile Services for remote data scenarios. You can derive your Web API controller classes from the generic TableController implementation.

public class StockController : TableController

The model used with the TableController generic class derives from EntityData exposed by the WindowsAzure.Mobile.Service namespace, which determines how the properties for a given table data model are serialized while communicating with clients using EF as a backend store.

using Microsoft.WindowsAzure.Mobile.Service;    using System;    using System.Collections.Generic;        public partial class Stock : EntityData    {        public int Id { get; set; }        public string StockName { get; set; }        public decimal UnitPrice { get; set; }    }

In your controller class, override the context initialization implementation to create the column annotations.

 protected override void Initialize(HttpControllerContext controllerContext)        {            base.Initialize(controllerContext);            var context = new StockMarketEntities(Services.Settings.Name.Replace('-', '_'));            DomainManager = new EntityDomainManager(context, Request, Services);        }

You can now use the TableController methods inside your CRUD operations.

 // GET api/Stock        public IQueryable GetStocks()        {            return Query();        }

To deploy the service, first download the publish profile from your Windows Azure Mobile Services Dashboard page. In Visual Studio, click on Publish from the project context menu. When prompted, select the publish profile file you just downloaded. In addition, under the File Publish settings, specify the option to remove additional files in destination.

Once publish is successful, you will be redirected to your Mobile Services landing page, which you can start using.

Another important element to note is that, if you are using EF with your Web API controllers, you should select the Async option while creating the controller methods. If you are generating the Controller from the EF model, then you can specify this while using the Wizard to create the controller class (this is only available with EF 6).

You are all set to use ASP.NET Web API as a Windows Azure Mobile Service!

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

positive contribution tech

Technology’s Positive Contributions to Society

Technology has and continues to improve our lives. From the business world to the world of medicine, and our day-to-day lives, you can’t go a day without interacting with at least one form of technology. While some fear technology may be going too far, there are many ways in which

How to Choose From The Best Big Data Platforms in 2023

How to Choose From The Best Big Data Platforms in 2023

As big data continues to become increasingly popular in the business world, companies are always looking for better ways to process and analyze complex data. The process critically depends on the platform that manages and analyzes the data. In this article, we will provide a guide to help you choose

Why transparent code is a good idea

Why Transparent Code is a Good Idea

Code is used to make up the apps and software we use every day. From our favorite social media platforms to our online banking services, code is the framework used to build these tools that help make our lives easier. Code is complex. Software today requires large teams of programmers

The Role of WordPress Hosting in Website Speed and Performance

The Role of WordPress Hosting in Website Performance

The term “WordPress hosting” refers to a specific type of web hosting service that offers hardware and software configurations tailored to the needs of WP sites. It’s important to remember that a WP hosting package is not required to host WordPress webpages. WP web pages are also compatible with standard

Data Privacy vs. Data Security: What you Should Know

Data Privacy vs. Data Security: What you Should Know

Data privacy and data security are often used interchangeably, but they are two completely different things. It’s important to understand the difference for anyone who handles sensitive information, such as personal data or financial records. In this article, we’ll take a closer look at data privacy vs. data security. We’ll