.NET – How to use my own dependency parser in the Apicontroller of MVC4

Back to MVC3, I have a custom ControllerFactory, which will use my own container (abstracted by my business logic) to instantiate controllers and deliver the services they need .

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

I have a static/shared method Core.DependencyResolverFactory.Resolver, which returns an IDependencyResolver .This in turn has many Resolve(…) and ResolveAll(…) methods and overloads.

So… how can I achieve the same functionality in MVC4?

I have tried to set up my own ServiceLocator but I cannot find the Microsoft.Practices.ServiceLocation.IServiceLocator interface in any framework assembly.

I am not too worried about being bound to the underlying container , Because it has been abstracted by BL, so I really need a quick and dirty way to inject my class into MVC DI.

Can someone point me to a good tutorial?

What I get now…

Sub Application_Start()
...Snip...

'Suggested in the link on jrummel's 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's 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 Obj ect) Implements IDependencyResolver.GetServices
Return Resolver.ResolveAll(serviceType)
End Function
End Class

ASP.NET Web API beta comes with its own IDependency Resolver, separate from MVC.

There is a tutorial on asp.net: Using the Web API Dependency Resolver< /p>

You can get ServiceLocator from NuGet.

Back to MVC3, I have a custom ControllerFactory, which will use my own container (by my Business logic abstraction) to instantiate controllers and pass the services they need.

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

I have a static/shared method Core.DependencyResolverFactory.Resolver, which returns an IDependencyResolver. This in turn has many Resolve(…) and ResolveAll(…) methods and overloads.

So… how do I Can the same function be realized in MVC4?

I have tried to set up my own ServiceLocator but I cannot find the Microsoft.Practices.ServiceLocation.IServiceLocator interface in any framework assembly.

I am not too worried about being bound to the underlying container , Because it has been abstracted by BL, so I really need a quick and dirty way to inject my class into MVC DI.

Can someone point me to a good tutorial?

What I get now…

Sub Application_Start()
...Snip...

'Suggested in the link on jrummel's 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's 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

ASP.NET Web API beta comes with its own IDependency Resolver, separate from MVC.

There is a tutorial on asp.net: Using the Web API Dependency Resolver

You can get ServiceLocator from NuGet.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 207 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.