I'm trying to register my AutoMapper (version 5.2.0) Profile class into my Unity container (Unity version 4.0.1), and it's giving me an exception:
Resolution of the dependency failed, type = IConsumer`1[Commands.INewUserEmailCommand]", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, AutoMapper.IConfigurationProvider, is an interface and cannot be constructed. Are you missing a type mapping?
Here's the profile class:
public class AutoMapperBootstrap : Profile
{
public AutoMapperBootstrap()
{
this.CreateMap<IUserEmailDTO, MergeUserEmailRequest>();
}
}
And here's the Unity registration snippet:
Profile typeMaps = new AutoMapperBootstrap();
var config = new MapperConfiguration(cfg => cfg.AddProfile(typeMaps));
container.RegisterInstance<IMapper>(config.CreateMapper(), new ContainerControlledLifetimeManager());
Then I have constructors that depend on the IMapper instance being injected which apparently Unity knows nothing about or it needs some extra IConfigurationProvider instance? What am I missing? When my code needs to invoke a service that relies on IMapper, Unity is throwing the exception above. Here's an example of a service that's using constructor injection trying to get an IMapper instance.
public class FooService : IFooService
{
private readonly IMapper mapper;
public Foo(IMapper mapper)
{
this.mapper = mapper;
}
}