【问题标题】:Cannot resolve AutoMapper.IMapper using AutoMapper 4.2 with Autofac无法使用带有 Autofac 的 AutoMapper 4.2 解析 AutoMapper.IMapper
【发布时间】:2016-02-22 22:50:57
【问题描述】:

我已经尝试了各种排列,但我当前的配置(因为它与 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)'.

这个类在没有自动映射器引用的情况下完美运行,所以我确定问题出在我的自动映射器配置上。

我不确定我在这里缺少什么,因为我对 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();

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

【问题讨论】:

  • 如果您临时手动添加个人资料而不是使用IEnumerable&lt;Profile&gt;,会发生什么情况?
  • @devuxer 查看我的更新。这也不起作用:(
  • 我认为您的汇编扫描代码可能不太正确,但我在测试应用程序中尝试了您的其余代码,它工作正常。因此,一定是其他原因导致了问题。您是否在注册 IMapper 的行放置了断点以确保它确实被命中?
  • 是的,断点命中没有问题。就像我说的,我剩下的 autofac 东西都在工作。
  • 我创建了一个答案,以便向您展示我的测试代码。底线:您的 AutoFac 代码似乎没问题。还有其他问题。

标签: c# automapper autofac automapper-4


【解决方案1】:

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

我创建了以下测试应用程序,它实际上运行时没有任何异常,并将 3 放入输出窗口(如预期的那样):

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

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

【讨论】:

  • 虽然不完全是答案,但我在 Startup 中有两个区域,一个用于 autofac,一个用于 automapper。 builder.Build() 被隐藏了,我完全因为不移动它而放屁。您对 AutoMapper 以外的其他地方的回答让我意识到这一点。
  • 很高兴我能以某种方式提供帮助:)
  • 在您的示例中 IMapper 没有注册为 SingleInstance 是否有原因?
【解决方案2】:

这对我有用...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多