Skip to content

Instantly share code, notes, and snippets.

@ctesene
Last active December 14, 2015 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctesene/5095879 to your computer and use it in GitHub Desktop.
Save ctesene/5095879 to your computer and use it in GitHub Desktop.
When using ASP.NET MVC 4 and UNITY, you might randomly encounter the error The type Unity.Mvc3.UnityDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.
WARNING : This code is in VB.NET.. I know, it sucks, but I have to work in it for my 9to5. Hopefully you don't, but if you do, here is a fix.
Original fix idea was at uri : http://netmvc.blogspot.com/2012/04/dependency-injection-in-aspnet-mvc-4.html posted by soonwong
Public Class CustomUnityDependencyResolver
Implements IServiceLocator
Private ReadOnly _container As IUnityContainer
Public Sub New(ByVal container As IUnityContainer)
_container = container
End Sub
Public Function GetService(ByVal serviceType As Type) As Object Implements IServiceProvider.GetService
Return _container.Resolve(serviceType)
End Function
Public Function GetInstance(ByVal serviceType As Type) As Object Implements IServiceLocator.GetInstance
Return _container.Resolve(serviceType)
End Function
Public Function GetInstance(ByVal serviceType As Type, ByVal key As String) As Object Implements IServiceLocator.GetInstance
Return _container.Resolve(serviceType, key)
End Function
Public Function GetAllInstances(ByVal serviceType As Type) As IEnumerable(Of Object) Implements IServiceLocator.GetAllInstances
Return _container.ResolveAll(serviceType)
End Function
Public Function GetInstance(Of TService)() As TService Implements IServiceLocator.GetInstance
Return _container.Resolve(Of TService)()
End Function
Public Function GetInstance(Of TService)(ByVal key As String) As TService Implements IServiceLocator.GetInstance
Return _container.Resolve(Of TService)(key)
End Function
Public Function GetAllInstances(Of TService)() As IEnumerable(Of TService) Implements IServiceLocator.GetAllInstances
Return _container.ResolveAll(Of TService)()
End Function
End Class
Public Class UnityConfiguration
Public Shared Sub Init(ByVal configuration As HttpConfiguration)
Dim container = New UnityContainer()
RegisterTypes(container)
DependencyResolver.SetResolver(New CustomUnityDependencyResolver(container))
configuration.DependencyResolver = New Unity.WebApi.UnityDependencyResolver(container)
End Sub
Private Shared Sub RegisterTypes(ByVal container As IUnityContainer)
container.RegisterType(Of ILogger, DatabaseLogger)()
container.RegisterType(Of ICourseProgressReportRepository, CourseProgressDatabaseRepository)()
container.RegisterType(Of IConfigurationRepository, ConfigurationRepository)()
container.RegisterType(Of IUserRepository, UserRepository)()
container.RegisterType(Of IUserPersistence, UserPersistence)()
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment