Is Mapper.Map in AutoMapper thread-safe?

The linked issue more or less answers your questions:

Mapper.CreateMap is not threadsafe, nor will it ever be. However,
Mapper.Map is thread-safe. The Mapper static class is just a thin
wrapper on top of the MappingEngine and Configuration objects.

So only use Mapper.CreateMap if you do your configuration in one central place in a threadsafe manner.

Your comment was:

I’m asking this because I’d like to configure automatter in-place,
i.e. right before usage. I planned to configure it in non-concurrent
context, i.e. ~ lock (mapperConfigLock) { Mapper.CreateMap()….; },
and I fear this is not enough now.

If you are doing in-place configuration just don’t use the static Mapper class. As the comment on the github issue suggest use the mapping engine directly:

var config = 
    new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers());
config.CreateMap<Source, Destination>();
var engine = new MappingEngine(config);

var source = new Source();
var dest = engine.Map(source);

It’s a little bit of more code but you can create your own helpers around it.
But everything is local in a given method so no shared state no need to worry about thread safety.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)