记录一次AutoMapper注册报错

通常,.Net5我们注册服务是这样的。

//添加AutoMapper
var automapperConfog = new MapperConfiguration(config =>
{
config.AddProfile(new AutoMapperProFile());
});

    services.AddSingleton(automapperConfog.CreateMapper());

//而在.Net6中,添加AutoMapper是这样的

    var autoMapperConfig = new MapperConfiguration(config =>
    {
        config.AddProfile(new AutoMapperProFile());
    });

    //通过单例注册进来
    builder.Services.AddSingleton<IMapper>(autoMapperConfig.CreateMapper());

2、这从报错是InvalidOperationException: Unable to resolve service for type 'AutoMapper.Mapper' while attempting to activate 'MyMemo.Api.Services.TodoService'意思是我注册失败,通过排查,发现在服务层

        private readonly IMapper _mapper;
		public TodoService(IUnitOfWork work,Mapper mapper)
        {
            _unitOfWork = work;
            _mapper=mapper;
        }
		注册时写成了Mapper,
posted @ 2023-06-14 14:56  孤沉  阅读(267)  评论(0编辑  收藏  举报