Cannot resolve AutoMapper.IMapper using AutoMapper 4.2 with Autofac

感情迁移 提交于 2019-12-07 08:07:35

问题


I have tried various permutations of this but my current configuration (as it relates to AutoMapper) is like this:

builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As<Profile>();

builder.Register(c => new MapperConfiguration(cfg =>
{
    foreach (var profile in c.Resolve<IEnumerable<Profile>>())
    {
        cfg.AddProfile(profile);
    }
})).AsSelf().SingleInstance();


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

builder.RegisterType<MappingEngine>().As<IMappingEngine>();

I have a constructor using IMapper mapper, however I continue to get the YSOD:

None of the constructors found with'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'
on type '' can be invoked with the available services and parameters:
 Cannot resolve parameter 'AutoMapper.IMapper mapper' of constructor 
'Void .ctor(...,...,..., AutoMapper.IMapper)'.

This class works perfectly without the automapper reference so I'm certain that the trouble lies with my automapper configuration.

I'm not sure what I'm missing here as I'm very new to both AutoFac and AutoMapper.

Edit:

I've also tried:

builder.Register(c => new MapperConfiguration(cfg =>
{
    cfg.CreateMap<IdentityUser, AspNetUser>().ReverseMap();
})).AsSelf().SingleInstance();

builder.Register(ctx => ctx.Resolve<MapperConfiguration>().CreateMapper()).As<IMapper>();
//I've tried both of these lines separately, neither work
builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope();

I've also tried manually adding the profiles per the suggestion in the comments


回答1:


As I mentioned in a comment, your AutoFac code appears to be correct (except for the assembly scanning portion).

I created the following test app, and it does in fact run without any exceptions and puts a 3 into the Output window (as intended):

using System.Diagnostics;
using Autofac;
using AutoMapper;

namespace Sandbox
{
    public partial class App
    {
        public App()
        {
            var builder = new ContainerBuilder();
            builder.Register(
                c => new MapperConfiguration(cfg =>
                {
                    cfg.AddProfile(new TestProfile());
                }))
                .AsSelf()
                .SingleInstance();

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

            builder.RegisterType<MappingEngine>()
                .As<IMappingEngine>();

            builder.RegisterType<Test>().AsSelf();

            var container = builder.Build();
            container.Resolve<Test>();
        }
    }

    public class TestProfile : Profile
    {
        protected override void Configure()
        {
            CreateMap<Source, Destination>();
        }
    }

    public class Test
    {
        public Test(IMapper mapper)
        {
            var source = new Source { Id = 3 };
            var destination = mapper.Map<Destination>(source);
            Debug.Print(destination.Id.ToString());
        }
    }

    public class Source
    {
        public int Id { get; set; }
    }

    public class Destination
    {
        public int Id { get; set; }
    }
}

I would suggest creating a new branch of your app in version control and stripping things out until it works.



来源:https://stackoverflow.com/questions/35565524/cannot-resolve-automapper-imapper-using-automapper-4-2-with-autofac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!