Configuring dependency injection with ASP.NET Web API 2.1 Configuring dependency injection with ASP.NET Web API 2.1 asp.net asp.net

Configuring dependency injection with ASP.NET Web API 2.1


I'd like to give you a suggestion and explanation why not to go this way, and how to do it differently (I'd even say better and properly).

The full and complete explanation of the inappropriate IDependencyResolver design could be found here: Dependency Injection and Lifetime Management with ASP.NET Web API by Mark Seemann

Let me cite these essential parts:

The problem with IDependencyResolver

The main problem with IDependencyResolver is that it's essentially a Service Locator. There are many problems with the Service Locator anti-pattern, but most of them I've already described elsewhere on this blog (and in my book). One disadvantage of Service Locator that I haven't yet written so much about is that within each call to GetService there's no context at all. This is a general problem with the Service Locator anti-pattern, not just with IDependencyResolver.

And also:

...dependency graph need to know something about the context. What was the request URL? What was the base address (host name etc.) requested? How can you share dependency instances within a single request? To answer such questions, you must know about the context, and IDependencyResolver doesn't provide this information.

In short, IDependencyResolver isn't the right hook to compose dependency graphs. **Fortunately, the ASP.NET Web API has a better extensibility point for this purpose. **

ServiceActivator

So, the answer in this scenario would be the ServiceActivator. Please take a look at this answer:

An example of the ServiceActivator:

public class ServiceActivator : IHttpControllerActivator{    public ServiceActivator(HttpConfiguration configuration) {}        public IHttpController Create(HttpRequestMessage request        , HttpControllerDescriptor controllerDescriptor, Type controllerType)    {        var controller = ObjectFactory.GetInstance(controllerType) as IHttpController;        return controller;    }}

All we can do with StructureMap, is in place. The key features of the Web API framework are still in place... we do not have to hack them. And we are also rather using DI/IoC then Service locator


Just try using UnityHierarchicalDependencyResolver instead of the other one. It worked for me. This is for future reference if somebody would like to use Unity