Simple Injector registering IMappingEngine (AutoMapper)

automapper, mapping, profile, simple-injector

Solution

Solved! :-)

I registered my profiles too late, after initating the MvcControllerFactory. I hope my pseudo example helps you to prevent such a mistake.

// SimpleInjector
var container = new Container();

// AutoMapper registration
container.Register<ITypeMapFactory, TypeMapFactory>();
container.RegisterCollection(MapperRegistry.Mappers);
container.RegisterSingleton<ConfigurationStore>();
container.Register<IConfiguration>(container.GetInstance<ConfigurationStore>);
container.Register<IConfigurationProvider>(container.GetInstance<ConfigurationStore>);
container.RegisterSingleton(() => Mapper.Engine);

// AutoMapper Profiles registration
container.RegisterCollection<Profile>(new MappingAProfile(), 
                                      new MappingBProfile(), 
                                      new MappingCProfile());

// Adding AutoMapper profiles
Mapper.Initialize(x =>
    {
        var profiles = container.GetAllInstances<Profile>();

        foreach (var profile in profiles)
        {
            x.AddProfile(profile);
        }
    });

Mapper.AssertConfigurationIsValid();

container.Verify();

container.RegisterAsMvcControllerFactory();

*RegisterAsMvcControllerFactory() to find at: Simple Injector MVC Integration Guide.

Problem

I used Autofac before but now I want give SimpleInjector a try. My problem is, on calling the mappingEngine within my method I get the following error: Missing type map configuration or unsupported mapping. Mapping types: Something -> SomethingDto Destination path: IEnumerable`1[0] Source value: ``` _mappingEngine.Map<IEnumerable<SomethingDto>>(IEnumerableOfSomething); ^-- doesn't work Mapper.Map<IEnumerable<SomethingDto>>(IEnumerableOfSomething); ^-- works (That's not what I want) ``` Mapper.Map is not that what I want. Im registering Automapper based on this here: Replace Ninject with Simple Injector ``` container.Register<ITypeMapFactory, TypeMapFactory>(); container.RegisterAll<IObjectMapper>( MapperRegistry.AllMappers()); container.RegisterSingle<ConfigurationStore>(); container.Register<IConfiguration>(() => container.GetInstance<ConfigurationStore>()); container.Register<IConfigurationProvider>(() => container.GetInstance<ConfigurationStore>()); container.Register<IMappingEngine, MappingEngine>(); ``` ``` Mapper.Initialize(x => { var profiles = container.GetAllInstances<Profile>(); foreach (var profile in profiles) { x.AddProfile(profile); } }); Mapper.AssertConfigurationIsValid(); ``` My question ist, how do I register IMappingEngine in SimpleInjector and add my Profiles correctly? Thanks in advance! Greets mtrax

Original source

Related problems