I'm getting an error when attempting to resolve an AutoMapper.IMapper instance:
Autofac.Core.Registration.ComponentNotRegisteredException : The requested service 'System.Object' has not been registered.
Here's my registration:
Builder.Register(Function(Context)
Return New MapperConfiguration(Sub(Config)
Config.AddProfile(Of MappingProfile)()
End Sub).CreateMapper
End Function).As(Of IMapper)()
...and here's the resolution:
Using oScope As ILifetimeScope = Me.Container.BeginLifetimeScope
oMapper = oScope.Resolve(Of IMapper)
End Using
Here's my mapping profile:
Public Class MappingProfile
Inherits Profile
Public Sub New()
Me.CreateMap(Of Dto.Release, Model.Release)()
Me.CreateMap(Of Model.Release, Dto.Release)()
End Sub
End Class
I've tried suggestions from these answers and more, but none of them result in anything but even more complex errors:
- https://stackoverflow.com/a/35566443
- https://stackoverflow.com/a/50276315
- https://stackoverflow.com/a/20432276
- https://stackoverflow.com/a/57526713
...as well as this blog post.
I've found many Q&As here on using AutoMapper with Autofac, a lot of them with accepted answers, but none of them seem to apply to my situation (at least that I'm able to understand/recognize). But then again, discussions here on using the two together are pretty dated. Not much under the context of the latest versions is showing up in searches.
I did notice a pattern in some of the accepted answers, and that would be to pass a Func(Of Object, IMapper) to the Builder.Register() function. Perhaps that's where the error originates: the parameter of type Object of that Func.
It's worth noting that the Context parameter here—for me at least—is always of type System.Object:
Builder.Register(Function(Context)
End Function)
So I'm not seeing how folks are getting code like this to work:
builder.Register(c =>
{
//This resolves a new context that can be used later.
var context = c.Resolve<IComponentContext>();
})
Obviously there is no Resolve<IComponentContext>() function on System.Object. Has something changed in the Autofac API with regard to the Builder.Register() function since those answers were written?
Assuming usage of the latest versions of both packages, how can I successfully register and resolve an AutoMapper.IMapper instance?