AutoMapper.IMapper使用AutoMapper 4.2 Autofac无法解析 [英] Cannot resolve AutoMapper.IMapper using AutoMapper 4.2 with Autofac

查看:3178
本文介绍了AutoMapper.IMapper使用AutoMapper 4.2 Autofac无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试过这各种排列,但我目前的配置(因为它涉及到AutoMapper)是这样的:

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



我有使用构造 IMapper映射,但是我继续得到YSOD:

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)'.

这类工作完全没有automapper参考,所以我敢肯定,麻烦在于我automapper配置

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

我不知道我在这里失踪,因为我很新的这两个AutoFac和AutoMapper。

I'm not sure what I'm missing here as I'm very new to both AutoFac and 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();



我也试过手动添加每个建议的配置文件中的注释

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

推荐答案

在我的评论中提到,你AutoFac代码似乎是正确的(除了装配扫描部分)。

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

我创建了下面的测试程序,它实际上来看确实没有任何异常,并把一个3到输出窗口(按预期):

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.

这篇关于AutoMapper.IMapper使用AutoMapper 4.2 Autofac无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆