5

having some trouble with adding profiles after some external assembly already added mapperconfiguration on the DI-setup.

first i just added some code to add the profiles

       var mapperConfiguration = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile<DataMappingProfile>();
        });
        mapperConfiguration.AssertConfigurationIsValid();
        services.AddSingleton<IMapper>(new Mapper(mapperConfiguration));

but then i overwrote some other mappingprofiles.

so i was thinking, i should try to add mine to the existing mappingconfiguration.

so i was going this way

        var sp = services.BuildServiceProvider();
        var autoMapper = sp.GetService<IMapper>();
        var mapperConfiguration = autoMapper?.ConfigurationProvider as MapperConfiguration;

        var configuration = new MapperConfigurationExpression();
        configuration.AddProfile<LpisMappingProfile>();


        if (mapperConfiguration == null)
        {
            mapperConfiguration = new MapperConfiguration(configuration);
        }
        else
        {
            //add the previous as well
            //?? add this `configuration` ?
        }

        mapperConfiguration.AssertConfigurationIsValid();
        services.AddSingleton<IMapper>(new Mapper(mapperConfiguration));

but i am a little stuck on the else flow. Any advice?

thnx!

1 Answer 1

3

That's not possible. If an assembly uses a private MapperConfiguration, then that's its own business. If it wants to collaborate with the app, it should not define a MapperConfiguration, it should define profiles to be scanned by the app and added to the singleton MapperConfiguration owned by the app. The AM configuration is read-only, after the init phase it's not possible to change it.

2
  • makes sense. thnx! how should/could i make that happen? if i have my own assemblies..
    – Roelant M
    Commented Jul 11, 2018 at 12:16
  • "If it wants to collaborate with the app, it should not define a MapperConfiguration, it should define profiles to be scanned by the app and added to the singleton MapperConfiguration owned by the app." Commented Jul 11, 2018 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.