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