How can I get the dependency resolver instance in web api? In asp.net mvc I can do DependencyResolver.Current, is there an equivalent in web api?
2 Answers
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.
IDependencyResolver. ServiceLocator could/should be seen as antipattern blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx