-1

I have just started to use Automapper and cannot seem to get the configuration correct. There are a number of conflicting explanations on SO and the documentation is not that clear at least for me.

At the moment I am getting the following error.

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

In Startup.cs I have the following:

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

My profile has been set up as follows:

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

and in my controller I have the following:

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

I have installed NuGet packages for Automapper - 9.0.0 and Automapper extensions for dependency injection 7.0.0

This is all I have done to get this up and running. What am I missing or what is wrong with this implementation.

I used this from the Code Mentor Community website and I have used this answer from SO to set up the projection.

UPDATE - Thank you for identifying a typo.. as I mentioned there are a number of variations of implementations as well as a number of implementations relating to versions some of which are out of date. Its difficult when you are new to this package to get an idea of what is a single source of truth. That being said I have changed that MapperConfiguration keyword to IMapper as follows:

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

Now I get the error:

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

on the line:

var clientIndexDtos = clients.ProjectTo<ClientIndexDto>(_mapper);
si2030
  • 3,895
  • 8
  • 38
  • 87
  • 1
    And you inject a MapperConfiguration instance instead of IMapper instance because of ...? – Sir Rufo Apr 27 '20 at 04:15
  • Good Point.. I changed that last.. as I followed that code mentor article.. will rectify that now.. see if it helps. – si2030 Apr 27 '20 at 04:26
  • I changed it to IMapper as per the code mentor article and now have an error on IMapper in this line: var clientIndexDtos = clients.ProjectTo(_mapper); So now this line doesnt work what should this be then. – si2030 Apr 27 '20 at 04:30
  • 1
    Well, you have two links to articles. Read them **carefully** and **really** do what they suggest (and not some creative but wrong interpretations) – Sir Rufo Apr 27 '20 at 04:35
  • Don't pass `_mapper` to the `ProjectTo` method. – JLe Apr 27 '20 at 04:43

2 Answers2

0

I had the same issue and it turned out we need to set DI for Automapper 9 in a different way.

Please try this:

var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new AutoMapping());
            });
            var mapper = mappingConfig.CreateMapper();
            services.AddSingleton(mapper);
shobhit vaish
  • 951
  • 8
  • 22
0

I think you need to replace

var clientIndexDtos = clients.ProjectTo<ClientIndexDto>(_mapper);  // magic happens here...

With

var dtos = new List<ClientIndexDto>();
foreach(var client in clients)
{
    dtos.Add(_mapper.Map<ClientIndexDto>(client));
}

Try to read through the documentation. Most of the time all the info is already available in official docs.

h-rai
  • 3,636
  • 6
  • 52
  • 76