Initializing Automapper

I struggled for while with getting Automapper to register the Profiles of my sub layers (Service layer for instance), when it is setup in the App start process. I found this on StackOverflow and it was the correct answer. Saving here so I can find easily next time.

Additionally. It was not necessary to override the ProfileNames. All profiles in all layers can have the same class name as far as I can tell. No issues in my case.

Original ref: https://stackoverflow.com/questions/38555702/how-to-initialize-automapper-profiles-in-referenced-project-dlls-in-asp-net-weba/38572088#38572088

 

 var assembliesToScan = AppDomain.CurrentDomain.GetAssemblies();
        var allTypes = assembliesToScan.SelectMany(a => a.ExportedTypes).ToArray();

        var profiles =
            allTypes
                .Where(t => typeof(Profile).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))
                .Where(t => !t.GetTypeInfo().IsAbstract);

        Mapper.Initialize(cfg =>
        {
            foreach (var profile in profiles)
            {
                cfg.AddProfile(profile);
            }
        });