Dependency Injection with Azure Mobile Services .NET Backend

17/04/2014

I’m a big fan of dependency injection, and after starting to work on a project using the Azure Mobile Services .NET Backend I quickly found myself looking for a solution on how to do DI like I’m used to in my other WebAPI projects.

My DI framework of choice have for a long time being Unity (old habits die hard), so at first I tried to add Unity to the project via. the NuGet package Unity.AspNet.WebApi. This was not a success, as my Controllers still wasn’t injected with the registrations I had setup up in my Unity container. So I started to dig a little into how the Mobile Services .NET Backend actually works.

If we look in the WebApiConfig class we can see that there are two lines that supposedly set up everything for us.
[csharp]
// 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));
[/csharp]

If look closely at the ConfigBuilder constructor, we can see that it has some extra paramters that can be set
[csharp]
public ConfigBuilder(ConfigOptions options, Action<HttpConfiguration, ContainerBuilder> autofacConfig);
[/csharp]

You might not have this Constructor if you havn’t updated your Mobile Services framework to the latest version 1.0.258. To update it run the following command from the package manager console.

Update-Package WindowsAzure.MobileServices.Backend.Entity

It will update all packages related to Mobile Services.

Dependency Injection with Autofac

The constructor parameters gives away that the dependency framework used in Mobile Services are Autofac. I have never used it before, but it is just another dependency injection framework, that is pretty widespread when working with OWIN. Using it in our Mobile Service application is straightforward. Here’s how my WebApiConfig class looks after registering IMyService with the Autofac container.
[csharp]
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Web.Http;
using MobileService1.DataObjects;
using MobileService1.Models;
using Microsoft.WindowsAzure.Mobile.Service;
using MobileService1.Controllers;
using Autofac;

namespace MobileService1
{
public static class WebApiConfig
{
public static void Register()
{
// 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, (configuration, builder) => {
builder.RegisterType<MyService>().As<IMyService>();
}));

// To display errors in the browser during development, uncomment the following
// line. Comment it out again when you deploy your service for production use.
// config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

}
}
}
[/csharp]
Be sure to add the using Autofac; statement.

Now you can use common Constructor injection in your controllers and Autofac will resolve the parameters for you. This is how my TodoItemController constructor looks.
[csharp]
public class TodoItemController : TableController<TodoItem>
{
private IMyService myService;

public TodoItemController(IMyService myService)
{
this.myService = myService;
}

[/csharp]