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

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!


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist