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:
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?
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:
There are basically 3 options when it comes to registering a dependency resolver:
- An instance of an object implementing IDependencyResolver dependency which has two methods: GetService and GetServices
- 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.
- Passing two delegates that return an instance or instances for a particular type
IDependencyResolver of Web API (not MVC) is located in System.Web.Http.dll in System.Web.Http.Services namespace.