asp.net web API - no dll is available containing Microsoft.Practices.ServiceLocation.IServiceLocator

So I want to set up dependency injection for my ASP.NET Web API project. Microsoft appear to have changed the way that DI is set up in MVC since version 3. Now I need to make a call like this:

GlobalConfiguration.Configuration.ServiceResolver.SetResolver(DependencyResolver);

SetResolver() accepts an object. What type of object?

When I run the application, I get an error telling me that the parameter passed in to SetResolver() must implement Microsoft.Practices.ServiceLocation.IServiceLocator. Sure, I could do that. I'm sure it's not difficult.

But I can't create a class that implements IServiceLocator because I don't have the dll where the bloody thing is defined. Presumably it doesn't get included when you install ASP.NET Web API. So how on earth am I supposed to get the project to use my custom container for DI?

Answers


This is how it is in beta but it is gonna change (see here):

DI is based on setting a dependency resolver (technically a service locator) on the HttpConfiguration object. This object is on the GlobalConfiguration object as a static property (web hosting) or in case of self hosting on HttpSelfHostConfiguration.

So in case of web hosting you would use:

GlobalConfiguration.Configuration.ServiceResolver.SetResolver(myResolver);

There are basically 3 options when it comes to registering a dependency resolver:

  1. An instance of an object implementing IDependencyResolver dependency which has two methods: GetService and GetServices
  2. An instance of an object which has these two public methods: GetInstance and GetAllInstances. No interface needed and Web API will use reflection to call methods.
  3. Passing two delegates that return an instance or instances for a particular type

EDIT

IDependencyResolver of Web API (not MVC) is located in System.Web.Http.dll in System.Web.Http.Services namespace.


Need Your Help

static compile time table with floating point values

c++ templates c++11 static variadic-templates

Is it possible to generate an array at compile time, like in this nice answer of G. Fritzsche:

Merge two methods into one

c# oop

How can i merge methods below? and should i actually do this?