I'm using AutoMapper 10.1.1, because this is the last version with .NET Framework support. And I am using Microsoft dependency injection.
I have a type converter I want constructed using dependency injection (it refers to a cache repo in memory).
If I use this code, it does not work.
var services = new ServiceCollection()
...
.AddAutoMapper((sp, cfg) =>
{
cfg.ConstructServicesUsing(t => ActivatorUtilities.GetServiceOrCreateInstance(sp, t));
cfg.AddProfiles();
}, Array.Empty()) // <-- to disambiguate the calls if nothing is provided
.BuildServiceProvider();
mMapper = services.GetRequiredService();
This, on the contrary, does work.
var services = new ServiceCollection()
...
.BuildServiceProvider();
var config = new MapperConfiguration(cfg =>
{
cfg.ConstructServicesUsing(t => ActivatorUtilities.GetServiceOrCreateInstance(services, t));
cfg.AddProfiles();
});
mMapper = config.CreateMapper();
By not working I mean that there is an exception that it cannot construct my type converter. It seems that ConstructServicesUsing
is simply ignore, because even inserting a throw
statement there does not change the outcome.
To be able to inject IMapper
I would really like to use the second version.
What do I do wrong?