无法通过 Autofac 使用 AutoMapper 4.2 解决 AutoMapper.IMapper

Cannot resolve AutoMapper.IMapper using AutoMapper 4.2 with Autofac

我已经尝试了各种排列,但我当前的配置(因为它与 AutoMapper 相关)是这样的:

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>();

我有一个使用 IMapper mapper 的构造函数,但是我继续得到 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)'.

这个 class 在没有自动映射器参考的情况下工作得很好,所以我确定问题出在我的自动映射器配置上。

我不确定我在这里遗漏了什么,因为我对 AutoFac 和 AutoMapper 都很陌生。

编辑:

我也试过:

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();

我也尝试过根据评论中的建议手动添加配置文件

正如我在评论中提到的,您的 AutoFac 代码似乎是正确的(程序集扫描部分除外)。

我创建了以下测试应用程序,它实际上 运行 没有任何例外并将 3 放入输出 window(如预期):

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; }
    }
}

我建议在版本控制中为您的应用程序创建一个新分支,然后将其删除直到它工作。

这对我有用...

 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>();