How to get instance of dependency resolver in ASP.NET web API
Ignore what people are saying about being an anti-pattern. You won't get full DI coverage, especially with these young technologies. For example, at the time of writing, NInject
has no support for injecting into middlewares.
To answer your question, the dependency resolver for a request is available through HttpRequestMessage.GetDependencyScope()
. You can also use HttpConfiguration.DependencyResolver
however beware that this one is not properly scoped for the request being executed.
I would recommend checking the documentation for the specific IOC implementation.
When using Ninject in Web API, you can use GlobalConfiguration.Configuration. For instance for IUserService:
(IUserService)System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IUserService))
Hope this helps you.