Skip to content
isaacabraham edited this page Dec 21, 2012 · 5 revisions

The Automapper currently can throw a single type of exception (the DuplicateMappingException) under two different circumstances: -

###You have multiple concrete types that implement the same interface. e.g.

         public interface IMyService { }
         public class MyService : IMyService { }
         public class MyOtherService : IMyService { }

With the default behaviour of the Automapper, it expects to find one and only one implementation of a given interface.

How do I fix this?

You have several options: -

  1. If you intend to only use one of these implementations at runtime, mark the other one with the [DoNotMap] attribute (or fluent equivalent).
  2. If you want to access all implementations at runtime, either: -
    • Mark the interface as a [Multimap] (or fluent equivalent)
    • Use the MultimapByDefault mapping behavior when calling the Automapper. See here for more details on Multimaps.

###You have multiple concrete types mapped with the same named registration. e.g. public interface IMyService { } [MapAs("Foo")] public class MyService : IMyService { } [MapAs("Foo")] public class MyOtherService : IMyService { }

Unity will simply overwrite named registrations with the latest one it gets (kind of a "last past the post" behaviour). To prevent accidentally missing mappings, if the Automapper finds two mappings for the same interface with the same name, it throws an exception.

How do I fix this?

  1. Change the explicit named mapping to something else.
  2. Use the implicit MultimapByDefault behaviour, which will automatically generate registration names based on the concrete type being mapped.
Clone this wiki locally