Roy*_*mir 2 c# automapper .net-core
当我使用 Automapper v6(我使用 .net core)时,我使用以下命令来验证配置:
configuration.AssertConfigurationIsValid();
Run Code Online (Sandbox Code Playgroud)
但现在,在迁移到最新版本后,我没有这个,因为我的配置完全是(文档):
private void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(AppSettingsMappingProfile)); //marker type
}
Run Code Online (Sandbox Code Playgroud)
但是,我仍然想在启动时验证所有映射。
文档说我需要这样做:
var configuration = new MapperConfiguration(cfg =>
cfg.CreateMap<Source, Destination>());
configuration.AssertConfigurationIsValid();
Run Code Online (Sandbox Code Playgroud)
但我没有它,因为我通过以下命令使用配置文件:
services.AddAutoMapper(typeof(AppSettingsMappingProfile));
Run Code Online (Sandbox Code Playgroud)
问题:
我怎样才能在启动时进行 AutoMapper 扫描以进行验证?
如果您查看AddAutoMapper的源代码,您将看到它注册IConfigurationProvider
为单例。这意味着您可以安全地将其放入您的配置方法中并在那里进行验证:
public void Configure(IConfigurationProvider pr)
{
pr.AssertConfigurationIsValid();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1114 次 |
最近记录: |