Azure Mobile Services and Autofac

If you want to use an IoC container in your backend hosted in Azure Mobile Services I recommend to use Autofac. The reason is that Autofac already are wired up in Mobile Services and is used by the runtime to resolve references.

Because Autofac already is wired up you cannot create a container as you use to when not hosting in Mobile Services.

To configure how to resolve your references using Autofac in Mobile Services se the code below. The code should be in the register method in the WebApiConfig class. The class can be found in App_Start folder.

ConfigOptions options = new ConfigOptions();
var configBuilder = new ConfigBuilder(options, builder =>
{
      builder.RegisterType<MyImplementation>().As<IMyInterface>();
});
 
HttpConfiguration config = ServiceConfig.Initialize(configBuilder);
configBuilder.ConfigureOwin(config);

Leave a Reply

Your email address will not be published. Required fields are marked *