0

I'm trying to use AutoMapper in a API wrapper class library project to map from API models to our domain models. While looking at the AutoMapper documentation I ran into the inline mapping feature.

Documentation says:

AutoMapper creates type maps on the fly (new in 6.2.0). When you call Mapper.Map for the first time, AutoMapper will create the type map configuration and compile the mapping plan. Subsequent mapping calls will use the compiled map.

So I wrote the following line of code in my wrapper class library:

var data = response.Results.Select(Mapper.Map<Session, Media>).ToList();

basically just trying to map the Session object I get from the API into our Media objects. But this throws the following error:

Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.

I was under the impression that inline mapping is exactly supposed to bypass having to initialize and define configuration for AutoMapper? Am I wrong?

Also if I am indeed wrong then how are you supposed to configure and initialize AutoMapper inside a class library to where it happens only once? I would like the library to be independent, meaning I don't want the programmer using the library to have to configure AutoMapper in his project in order for the library to work properly.

Marko
  • 12,543
  • 10
  • 48
  • 58
  • The docs show the configuration method being called ` var dest = Mapper.Map(source, opt => opt.ConfigureMap()....` so you probably have to do it like that – Ric Mar 22 '18 at 20:15
  • 1
    @Ric Ok so I changed it to: var data = response.Results.Select(session => Mapper.Map(session, opt => opt.ConfigureMap())).ToList(); And still the same error. I don't want to define mapping for each member that kind of defeats the entire purpose. – Marko Mar 22 '18 at 20:24
  • 1
    ```Mapper.Initialize(_=>{});``` – Lucian Bargaoanu Mar 24 '18 at 07:59

0 Answers0