How to use the native dependency converter in MVC4 APIController

Back in MVC3, I had a custom ControllerFactory that would use my own container (as abstracted by my business logic) to instantiate the controllers and transfer the services they needed.

I am trying to implement something similar in MVC4 using the new ApiController .

I have a static / generic Core.DependencyResolverFactory.Resolver method that returns an IDependencyResolver . This, in turn, has several Resolve(...) and ResolveAll(...) and overload methods.

So ... How can I implement the same thing in MVC4?

I tried setting up my own ServiceLocator, but cannot find the Microsoft.Practices.ServiceLocation.IServiceLocator interface in any of the frame collectors.

I'm not too worried about binding to the base container since it is already abstracting BL, so I just need a quick and dirty way to input my classes into MVC DI.

Can someone point me to a good tutorial?

What I have at the moment ....

 Sub Application_Start() ...Snip... 'Suggested in the link on jrummel answer... GlobalConfiguration.Configuration.DependencyResolver = New WebResolver 'It fails due to DependencyResolver not being defined 'I do have a GlobalConfiguration.Configuration.ServiceResolver = New WebResolver 'but it read-only and it is of type System.Web.Http.Services.DependencyResolver not IDependencyResolver End Sub Private Class WebResolver Implements IDependencyResolver Private Resolver As Common.Interfaces.IDependencyResolver = Core.DependencyResolverFactory.QuickResolver Public Function GetService(serviceType As Type) As Object Implements IDependencyResolver.GetService Return Resolver.Resolve(serviceType) End Function Public Function GetServices(serviceType As Type) As IEnumerable(Of Object) Implements IDependencyResolver.GetServices Return Resolver.ResolveAll(serviceType) End Function End Class 
+4
source share
1 answer

The beta version of ASP.NET Web API comes with its own IDependencyResolver , separate from MVC.

Here is an asp.net tutorial: Using the Web API Dependency Resolution Tool

You can get ServiceLocator from NuGet .

+3
source

All Articles