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

How can I provide the field to use in a function?

c# .net lambda

Something working on the likes of Linq's OrderBy, in which you can specify the key with which it is supposed to do the ordering.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.