Where to validate AutoMapper Configuration in ASP.Net Core application?
The recommended approach (see JBogard's response) is to move this test into a unit test:
public class MappingTests
{
private readonly IMapper _sut;
public MappingTests() => _sut = new MapperConfiguration(cfg => { cfg.AddProfile<MyAutomapperProfile>(); }).CreateMapper();
[Fact]
public void All_mappings_should_be_setup_correctly() => _sut.ConfigurationProvider.AssertConfigurationIsValid();
}
After digging around in the IMapper
interface (and thanks to the documentation link provided by @LucianBargaoanu), I found exactly what I needed.
In ConfigureServices()
:
// Adds AutoMapper to DI configuration and automagically scans the
// current assembly for any classes that inherit Profile
// and registers their configuration in AutoMapper
services.AddAutoMapper();
The secret sauce is to add IMapper mapper
as a parameter to Configure()
- the parameter list is dependency-injected so you can reference any service registered in ConfigureServices()
public void Configure(IApplicationBuilder app, ... , IMapper mapper)
{
...
mapper.ConfigurationProvider.AssertConfigurationIsValid();
}
Works exactly as expected.