回到MVC3,我有一个自定义ControllerFactory
,它将使用我自己的容器(由我的业务逻辑抽象)来实例化控制器并传递它们所需的服务。
我正在尝试使用新的ApiController
在MVC4中实现类似的东西。
我有一个静态/共享方法Core.DependencyResolverFactory.Resolver
,它返回IDependencyResolver
。这反过来又有许多Resolve(...)
和ResolveAll(...)
方法和重载。
那么......我怎样才能在MVC4中实现相同的东西?
我已尝试设置自己的ServiceLocator,但无法在任何框架程序集中找到接口Microsoft.Practices.ServiceLocation.IServiceLocator
。
我并不太担心被绑定到底层容器,因为它已经被BL抽象了,所以我真的需要一种快速而又脏的方法将我的类注入到MVC的DI中。
有人可以指点我一个好的教程吗?
我现在得到的......
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
答案 0 :(得分:3)
ASP.NET Web API beta附带了自己的IDependencyResolver
,与MVC分开。
有一个关于asp.net的教程:Using the Web API Dependency Resolver