How to inject AutoMapper with Autofac?

前端 未结 2 2041
遥遥无期
遥遥无期 2021-01-19 17:25

What is the proper way to inject AutoMapper to other layers?

I read this blog post , but this code cause exception below

An exception of type

2条回答
  •  野性不改
    2021-01-19 18:14

    .netcore 3 Autofac 5.1.2 AutoMapper 9.0.0 AutoMapperProfiles -> My profile name

    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType().As();
        builder.Register(c => new MapperConfiguration(cfg =>
        {
            foreach (var profile in c.Resolve>())
            {
                cfg.AddProfile(profile);
            }
        })).AsSelf().SingleInstance();
    
        builder.Register(c => c.Resolve().CreateMapper(c.Resolve)).As().InstancePerLifetimeScope();
    }
    

提交回复
热议问题