Automapper 9.0依赖注入补充给出错误

问题描述 投票:-1回答:1

我刚刚开始使用Automapper,似乎无法正确配置。关于SO有许多相互矛盾的解释,并且至少对于我来说文档不清楚。

目前,我遇到以下错误。

Unable to resolve service for type 'AutoMapper.MapperConfiguration' while attempting to activate 'JobsLedger.API.Controllers.API.App.ODataClientController

在Startup.cs中,我有以下内容:

services.AddAutoMapper(c => c.AddProfile<AutoMapping>(), typeof(Startup));

我的个人资料设置如下:

public class AutoMapping : Profile
{
    public AutoMapping()
    {
        // Add as many of these lines as you need to map your objects
        CreateMap<Client, ClientIndexDto>();
        //CreateMap<UserDto, User>();
    }
}

并且在我的控制器中,我有以下内容:

    public class ODataClientController : ODataController, IClientController {
    private readonly MapperConfiguration _mapper;
    private IClientDATARepository _clientDATARepository;
    private readonly IClientServices _clientServices;
    private readonly DATAContext _context;

    public ODataClientController(MapperConfiguration mapper, IClientDATARepository clientDATARepository, IClientServices clientServices, DATAContext context) {
        _mapper = mapper;
        _clientDATARepository = clientDATARepository;
        _clientServices = clientServices;
        _context = context;
    }


    [HttpGet]
    [ODataRoute("AllClients()")]
    [EnableQuery()]
    public IQueryable<ClientIndexDto> Get(ODataQueryOptions<ClientIndexDto> options) 
    {
        var clients = _clientDATARepository.AllIncluding();
        var clientIndexDtos = clients.ProjectTo<ClientIndexDto>(_mapper);  // magic happens here...

        return (IQueryable<ClientIndexDto>)options.ApplyTo(clientIndexDtos);
    }

我已经安装了用于Automapper的NuGet软件包-9.0.0和用于依赖项注入7.0.0的Automapper扩展]

这是我启动并运行它的全部工作。我缺少什么或此实现有什么问题。

我使用了Code Mentor社区网站上的this,并且使用了SO中的this answer来建立投影。

UPDATE-谢谢您识别错别字。如我所提到的,实现的实现方式有很多变体,以及与版本有关的实现方式中有一些已经过时。当您不熟悉此软件包时,很难了解什么是唯一的真理。话虽如此,我已将MapperConfiguration关键字更改为IMapper,如下所示:

    public class ODataClientController : ODataController, IClientController {
    private readonly IMapper _mapper;
    private IClientDATARepository _clientDATARepository;
    private readonly IClientServices _clientServices;
    private readonly DATAContext _context;

    public ODataClientController(IMapper mapper, IClientDATARepository clientDATARepository, IClientServices clientServices, DATAContext context) {
        _mapper = mapper;
        _clientDATARepository = clientDATARepository;
        _clientServices = clientServices;
        _context = context;
    }


    [HttpGet]
    [ODataRoute("AllClients()")]
    [EnableQuery()]
    public IQueryable<ClientIndexDto> Get(ODataQueryOptions<ClientIndexDto> options) 
    {
        var clients = _clientDATARepository.AllIncluding();
        var clientIndexDtos = clients.ProjectTo<ClientIndexDto>(_mapper);  // magic happens here...

        return (IQueryable<ClientIndexDto>)options.ApplyTo(clientIndexDtos);
    }

现在我得到了错误:

cannot convert from 'AutoMapper.IMapper' to 'AutoMapper.IConfigurationProvider

在线:

var clientIndexDtos = clients.ProjectTo<ClientIndexDto>(_mapper);
c# asp.net-core automapper
1个回答
0
投票

我遇到了同样的问题,结果我们需要以不同的方式为Automapper 9设置DI。

请尝试以下方法:

var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new AutoMapping());
            });
            var mapper = mappingConfig.CreateMapper();
            services.AddSingleton(mapper);
© www.soinside.com 2019 - 2024. All rights reserved.