• <1 minute

Azure Mobile Services: why should ASP.NET developers care?

Azure Mobile Services provides a turnkey 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.

Update: The Mobile Services ASP.NET Web API support is now in general availability. Read the announcement on Scott Guthrie’s blog.

Azure Mobile Services provides a turnkey 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:

  • Turnkey backend with client SDKs for all mobile platforms. With Mobile Services you can quickly add a fully featured backend to your iOS, Android, Windows, Windows Phone, or HTML app, as well as use cross-platform frameworks such as Xamarin, Sencha, and PhoneGap. We provide client SDKs for each of these platforms making it easy to connect your applications to your cloud-hosted backend.
  • First-class hosting for your mobile API. Mobile services are built using Web API and you can publish any Web API controller to the service. What differentiates Mobile Services from other compute environments is that we monitor and manage your Web API controllers for you. If there is a problem with Web API runtime or extensions, we patch it without you ever worrying about it. If we think the problem is with your controller code, we will reach out to you.
  • High value mobile backend capabilities included. You will find many capabilities included in Mobile Services and readily available for your Web API. Mobile push notifications, real-time notifications with SignalR (auto-scaled out), social auth for your consumer apps, offline data sync for occasionally connected scenarios, to name a few.
  • Connectivity to enterprise systems. If you’re an enterprise developer, you will appreciate the ability for your app’s customers to log in via Active Directory and access enterprise assets such as SharePoint and Office 365. In addition, we’ve enabled seamless connectivity to on-premises assets, so you can reach databases and web services that are not exposed to the Internet.
  • 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.

Turnkey backend with client SDKs for all mobile platforms

It’s easy to get started with the Mobile Services support in your favorite environment. As always go to Windows Azure Management Portal and create a new Mobile Service. On the first screen of the wizard choose “.NET” as your backend. Once the service is created, head over to the Quickstart tab and download the starter project for the client platform you wish to target.

mobile-services-for-web-api-devs-1

mobile-services-for-web-api-devs-2

Alternatively, if you prefer to get started in Visual Studio, you can create a local project first and create the mobile service later when you want to publish your project. Please note that this requires Visual Studio 2013 Update 2 or later.

mobile-services-for-web-api-devs-3

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.

mobile-services-for-web-api-devs-4

Open the TodoItemController.cs controller file and examine its content. Set a breakpoint inside the GetAllTodoitems() method. This controller shows you how to work with data using Mobile Services .NET support:

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);
    }
}

Notice we already scaffold all the key CRUD methods for the TodoItem resource. You will note that by default the controller is using an EntityDomainManager, which is a thin wrapper on top of an Entity Framework model. It is easy to switch to using an alternative data store:

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 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 GetAllTodoItems() method. As you expect, you will hit the breakpoint you’ve set up earlier.

mobile-services-for-web-api-devs-5

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.

mobile-services-for-web-api-devs-6

Note that you can publish any existing Web API solution to Mobile Services and immediately offload management and monitoring of your Web API to Azure.

To learn more about Mobile Services .NET support follow these tutorials:

First-class hosting for your mobile API

If you are an experienced Web API developer, you might wonder what added value do you get when you publish your API to Mobile Services. After all, there are many easy ways to host Web API, both on Azure and on your premise. Let’s look at some of the value-added capabilities you get for free with Mobile Services:

  • Monitoring and diagnostics. Mobile Services offers a 99.9% SLA backed by instrumentation and monitoring capabilities that we’ve built in order to keep your API running healthy. In our paid tiers, we monitor the service’s HTTP traffic and SQL connection and will get in touch with you if we start seeing high error rates. This is in addition to a variety of great self-service capabilities including endpoint monitoring and alerts.
  • Automatic patching. We deploy new code to our hosting environment weekly, without you needing to redeploy your app. Whether it is bugfixes in the Mobile Services runtime, bugfixes in one of our dependencies, or new features, the changes get deployed to your mobile service transparently without interruption. Of course, we would never deploy breaking changes, all changes made are backward-compatible.
  • Automatic provisioning of cloud assets. When you create a mobile service, we provision and configure a SQL Database and a Notification Hub (our push notifications engine) for your service, and make sure all the relevant connection strings are configured. Moreover, it’s easy to create scheduled jobs and hybrid connections (for on-premises connectivity) in the cloud with just a few clicks. Mobile Services gives you a quick gateway to all the relevant cloud assets relevant to your service.

High value mobile backend capabilities included.

As you start building out your app with Mobile Services, you’ll find that a lot of the functionality you need is available right out of the box, which can shave weeks off your time to market. Examples include:

  • Turnkey authentication. Every mobile service comes with all the cloud assets and framework code needed for most common authentication scenarios. It’s easy to enable social login with Microsoft Account, Facebook, Twitter, or Google, and enterprise login with AAD. All the necessary framework pieces are already there so you don’t have to become an OAuth expert, just specify some configuration settings and the rest is taken care of. For more information see this tutorial.
  • Scalable push notifications. Each mobile service comes with a cloud-hosted push engine that can deliver a massive number of notifications across all major device platforms: Windows, iOS, Android, and Kindle. All it takes is a single API call to notify millions of connected devices. We also support tagging and tag expressions that let you easily segment your audience, and also templates which lets you customize notifications on the device itself. This doc provides step-by-step instructions.
  • Offline sync. Today customer expect your mobile apps to continue working even when the network connection is unavailable. Mobile Services provides built-in offline SQLite-based sync support across all the major client platforms. More info here.
  • Realtime. Push notifications work great to increase customer engagement with your app, but providing a scalable high-bandwidth low-latency connection inside your app requires a different toolset. Mobile Services provides built-in realtime support based on WebSocket and SignalR, allowing bi-directional messaging to your app. The service comes with a built-in message bus (backplane) for seamless scale-out as your app grows. It also integrated with the same authentication pipeline as the rest of your service, it is easy to authenticate and authorize users. For more information, see this blog post.

Connectivity to enterprise systems

.NET is frequently the platform of choice in the enterprise, so supporting .NET languages has opened up a lot of great scenarios where we can help developers building business apps. Check out this page, for more info and some cool videos.

  • Active Directory login and on-behalf-of access. In enterprise scenarios, enabling app users to log in with their domain identity is a critical requirement. That makes the management of account lifetime simple, and also enables customers to access cloud assets that are also protected by Active Directory such as SharePoint Online and Office 365. For more information on configuring your app for Active Directory authentication see this tutorial. Check out this video for instructions on how to configure on-behalf-of access.
  • On-premises connectivity. Sometimes enterprise assets such as databases and web services need to stay on premises because they contain sensitive data or there are other reasons why they can’t be moved to the cloud. It is still possible to consume these assets from a mobile services through our “hybrid connections” features, which lets you relay on-premises traffic to the cloud. Check out this video introduction and this tutorial.
  • Xamarin SDK. Xamarin provides a client-side framework and toolset that lets you write cross-platform apps (including Android and iOS) using C#. It’s a productive way for developers familiar with the Microsoft stack to reuse their skills on other platforms. Mobile Services provides a SDK and some great resources to help you build Xamarin-based apps.
  • Accelerators. Sometimes it’s easiest to think about implementing a scenario if there is a great reference implementation available that helps you get started. The team has recently focused on building up a set of “accelerators”, which are polished apps that cover common customer verticals and show how to address them with Mobile Services. The apps are published to the store and the source code is available for you to use and modify. For more information see here. Currently we have 2 accelerators ready (a field engineer and a sales assistant scenario), but more are coming soon.

Integration with Visual Studio

In addition to the functionality shown above, Visual Studio contains some other useful tools for developing and debugging your mobile service. We have scaffolders in place that let you create Table Controllers (for storing relational data), Custom Controllers (for building arbitrary HTTP APIs), and Scheduled Jobs.

mobile-services-for-web-api-devs-7

We have also ensured a great debugging experience by lighting up support for remote debugging. Simply right-click your mobile service in Server Explorer and select “Attach Debugger”. Be sure to publish a debug build of your service code to make the most out of this feature!

mobile-services-for-web-api-devs-8

Another cool capability you’ll find in Server Explorer is the “View Logs” command which lets you browse the logs emitted by your mobile service in the cloud, including error messages and stack traces.

mobile-services-for-web-api-devs-9

We really hope this great new set of features encourages you to try out Mobile Services for your next project. If you need help or have any feedback, please don’t hesitate to reach out on Twitter to @theYavor.