-
Notifications
You must be signed in to change notification settings - Fork 3
Exceptions
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: -
- If you intend to only use one of these implementations at runtime, mark the other one with the
[DoNotMap]
attribute (or fluent equivalent). - 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.
- Mark the interface as a
###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?
- Change the explicit named mapping to something else.
- Use the implicit MultimapByDefault behaviour, which will automatically generate registration names based on the concrete type being mapped.