0

I'm maintaining an app which is using AutoMapper like this:

public class UserDomainService
{
    public UserDTO GetUser(int id)
    { 
         Mapper.Reset();
         Mapper.CreateMap<User, UserDTO>();
         var user = ....;
         return Mapper.Map<User, UserDTO>(user);
    }
}

This domain service is used by web-services.

I think it can be a problem when two web-service requests come in and on separate threads Reset and Map are called.

The Mapper can become in a state where the Map() fails.

I know I should probably setup CreateMap() mappings in Application_Start, but for now I am trying to do this:

public class UserDomainService
{
    public UserDTO GetUser(int id)
    { 
         var config = new AutoMapper.Configuration(new TypeMapFactory(), MapperRegistry.AllMappers());
         config.CreateMap<User, UserDTO>();
         var mapper = new MappingEngine(configuration);
         var user = ....;
         return mapper.Map<User, UserDTO>(user);
    }
}

Leaving aside performance, is it anything which could potentially make the app crash? Sometimes I am getting an exception like this:

System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at AutoMapper.TypeMapFactory.GetTypeInfo(Type type)
   at AutoMapper.TypeMapFactory.CreateTypeMap(Type sourceType, Type destinationType, IMappingOptions options)
   at AutoMapper.Configuration.CreateTypeMap(Type source, Type destination, String profileName)
   at AutoMapper.Configuration.CreateMap[TSource,TDestination](String profileName)
   at AutoMapper.Configuration.CreateMap[TSource,TDestination]()

Note that the above sample mapping is just an example.

I am using AutoMapper v1.1.0.188 in a 3.5 Net app.

EDIT: There's a specific reason why it's not easy for me to put the configuration in the Application_Start. I have different mapping requirements depending on the context. For example, for the same User to UserDTO, I need two different types of mapping.

It's the same problem described in this old question:

Link

WriteEatSleepRepeat
  • 3,083
  • 3
  • 33
  • 56

1 Answers1

0

https://github.com/AutoMapper/AutoMapper/wiki/Getting-started

Where do I configure AutoMapper?

If you're using the static Mapper method, configuration should only happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.asax file for ASP.NET applications. Typically, the configuration bootstrapper class is in its own class, and this bootstrapper class is called from the startup method.

Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121
  • I am not using the static Mapper.CreateMap Mapper.Map. I am following this recommendation: http://stackoverflow.com/questions/10649250/is-mapper-map-in-automapper-thread-safe Shouldn't MappingEngine and Configuration be thread safe? – WriteEatSleepRepeat Dec 03 '13 at 21:10
  • In 2009, Jimmy says he addressed thread safety for MappingEngine and Configuration: https://groups.google.com/forum/#!topic/automapper-users/2DW52VZCziA – WriteEatSleepRepeat Dec 03 '13 at 21:15
  • One more thing: there's a specific reason why it's not easy for me to put the configuration in the Application_Start. I have different mapping requirements depending on the context. For example, for the same User to UserDTO, I need two different types of mapping. – WriteEatSleepRepeat Dec 03 '13 at 21:21
  • The reason is the same with this old question: https://groups.google.com/forum/#!searchin/automapper-users/AM$20per$20request../automapper-users/5pfkZwSe0X0/99OuNoN8IOoJ – WriteEatSleepRepeat Dec 03 '13 at 21:43