grokky grokky - 13 days ago 5
C# Question

Configuration of AutoMapper with ASP.NET Core

In ASP.NET Core, AutoMapper is now configured like this (in

Startup.ConfigureServices
):

var config = new MapperConfiguration(cfg => {
cfg.AddProfile(new MyConfig());
});
services.AddSingleton<IMapper>(sp => config.CreateMapper());


Why not the following:

var config = new MapperConfiguration(cfg => {
cfg.AddProfile(new MyConfig());
});
var mapper = config.CreateMapper();
services.AddSingleton(mapper);


Is there a difference? Something to do with lazy loading maybe?

Answer

First one creates the instance on first request, second during bootup.