Open Source

Introducing the Unity.WebAPI NuGet Package

Whilst the IDependencyResolver interface in the new ASP.NET Web API is identical to its in ASP.NET MVC 3+ compatriot, it is nevertheless in a different namespace and assembly and thus, effectively a different interface. Unity.WebAPI brings the functionality of the popular Unity.Mvc3 package to WebAPI, allowing you to inject dependencies into your ApiControllers via Unity and have IDisposable components automatically disposed of at the end of the request.

There are many articles that already describe the Unity.Mvc3 NuGet package, so there is no need to repeat any of this information here. For background on the design and usage of Unity.Mvc3 (and therefore Unity.WebAPI), please take a look at the followings articles:

Shawn Wildermuth recently pointed out the fact that the new Web API framework that was released in conjunction with MVC4 (beta) introduces a second IDependencyResolver interface. The Web API functionality does not require MVC, so I imagine that the team did not want to add a dependency on an MVC specific type. So now, we have:




The interfaces are identical:

public interface IDependencyResolver
    object GetService(Type serviceType);
    IEnumerable<object> GetServices(Type serviceType);

Unity.WebAPI is a port of the popular Unity.Mvc3 package, specifically for ASP.NET Web API scenarios. No references to any ASP.NET MVC DLLs are necessary, though if you do host your API within an MVC application, Unity.WebAPI will work in harmony with Unity.Mvc3. See below for more details.

To install the package, enter the following command in the package console:

PM> install-package Unity.WebAPI

If you do not already have a reference to Unity, it will be downloaded automatically. As an alternative to the NuGet package, you can download the dll or view the source code on the Unity.WebAPI CodePlex Site.

Unity.WebAPI works in exactly the same way as Unity.Mvc3. The NuGet package will add a boostrapper class at the root of your application. The bootstrapper should contain your Unity registration code. You do not need to add the controllers to Unity, but any other components that are used by the controllers should be added.

private static IUnityContainer BuildUnityContainer()
    var container = new UnityContainer();

    container.RegisterType<IBlahRepository, BlahRepository>();

    return container;

If you are registering any components that implement IDisposable such as Entity Framework's DbContext, you will want to make sure that these components get disposed of at the end of the request. This is achieved by registering these components with a HierarchicalLifetimeManager.

private static IUnityContainer BuildUnityContainer()
    var container = new UnityContainer();

      .RegisterType<IBlahRepository, BlahRepository>()
      .RegisterType<DbContext, BlahDbContext>(new HierarchicalLifetimeManager());

    return container;

To hook up the bootstrapper, simply add a call to the initialise method from Application_Start() in your global.asax:

protected void Application_Start()




Under the covers, WebAPI will call the custom DependencyResolver whenever all call is made to an ApiController. The resolver will build the controller and all its dependencies and return the instantiated controller to the framework. A dynamically registered HttpModule is used to hook in to the end of the request. The module automatically disposes of all IDisposable instances that are registered as hierarchical.

Unity.WebAPI and Unity.Mvc3 in the same application

You can use Unity.WebAPI and Unity.Mvc3 in the same application but you will need to tweak the bootstrapper file that both libraries use. After installing both NuGet packages, change the Initialise method in the generated bootstrapper.cs file to the following:

public static void Initialise()
    var container = BuildUnityContainer();

    DependencyResolver.SetResolver(new Unity.Mvc3.UnityDependencyResolver(container));
      new Unity.WebApi.UnityDependencyResolver(container));

Useful or Interesting?

If you liked the article, we would really appreciate it if you would share it with your Twitter followers.

Share on Twitter »


Comments are now closed for this article.