Christian Droulers

Agile and flexible programmer

StructureMap 3 and ASP.NET Web Api 2

Posted on 2015-03-03

The easiest way to integrate StructureMap 3 and ASP.NET Web Api 2 is quite simple. StructureMap has a NuGet package that does it, but it creates 5 new files in a project and doesn’t solve the problem well.

This article gives an overview of why using the ‘IDependencyScope’ is not the best solution.

The final solution looks like this

public class StructureMapWebApiControllerActivator : IHttpControllerActivator
{
    private readonly IContainer _container;

    public StructureMapWebApiControllerActivator(IContainer container)
    {
        _container = container;
    }

    public IHttpController Create(
        HttpRequestMessage request,
        HttpControllerDescriptor controllerDescriptor,
        Type controllerType)
    {
        var nested = _container.GetNestedContainer();
        var instance = nested.GetInstance(controllerType) as IHttpController;
        request.RegisterForDispose(nested);
        return instance;
    }
}

You then register it in the GlobalConfiguration, usually in the App_Start/WebApiConfig.cs file

    IContainer container = new Container(c => c.AddRegistry<ProvincesRegistry>());
    config.Services.Replace(
        typeof(IHttpControllerActivator), 
        new StructureMapWebApiControllerActivator(container));

And bam! Integration. The RegisterForDispose call also ensures that any IDisposable entities in your StructureMap configuration are properly disposed after a request.

comments powered by Disqus