0
votes

Autofac Registration:

builder.RegisterType<RelatedTransportMangerResolver>().AsSelf();
builder.Register(context => new MapperConfiguration(cfg =>
{
    cfg.AddProfile<AssetMapperProfile>();
})).AsSelf().SingleInstance();

builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve))
.As<IMapper>()
.InstancePerLifetimeScope();

Map that uses the custom value resolver:

CreateMap<TrafficArea, TrafficAreaViewModel>()
            .ForMember(ta => ta.TransportManagers,
                opt => opt.MapFrom(ta =>
                    ta.TrafficAreaKeyContacts
                        .Where(kc => kc.KeyContactGroup.HasFlag(KeyContactGroup.TransportManager))
                        .Select(atr => atr.KeyContact)))
            .ForMember(ta => ta.RelatedTransportManagers,
                opt => opt.MapFrom<RelatedTransportMangerResolver>());

Error being returned is:

This resolve operation has already ended. When registering components using lambdas, the IComponentContext 'c' parameter to the lambda cannot be stored. Instead, either resolve IComponentContext again from 'c', or resolve a Func<> based factory to create subsequent components from.

Any ideas on how to fix this error?

1

1 Answers

0
votes

Should of done a bit more digging myself first...

Fix here if anybody has this same issue:

builder.Register(c =>
{
    //This resolves a new context that can be used later.
    var context = c.Resolve<IComponentContext>();
    var config = context.Resolve<MapperConfiguration>();
    return config.CreateMapper(context.Resolve);
})
.As<IMapper>()
.InstancePerLifetimeScope();