如何使用 .net core 2.2 实例 api 向 Automapper v8 添加全局配置选项

How to add global configuration options to Automapper v8 using instance api with .net core 2.2

我正在使用:

在我的解决方案中我有:

我按照

中所述的指南使用依赖注入设置了自动映射器

所以:在 startup.cs 的 TVC_PORTAL 中,在 ConfigureServices 方法中我有:

services.AddAutoMapper(typeof(AutomapProfileGen));

我使用 AutoMapProfileGen 的地方是 AddAutoMapper 的标记类型之一,用于定位其他配置文件所在的 TVC_DATA 程序集。

在我的 ObjectController 中我注入了 IMapper:

public ObjectController(IHostingEnvironment environment, IMapper mapper)
{
        _hostingEnvironment = environment;            
        _mapper = mapper;
}

我稍后会使用映射器:

IEnumerable<ObjectViewType> vList = _mapper.Map<IEnumerable<ObjectType>, IEnumerable<ObjectViewType>>(mList);

我的个人资料非常简单,例如:

public class AutomapProfileSrc : Profile
{
    public AutomapProfileSrc()
    {
        //source data
        CreateMap<AirlineView, Airline>().ReverseMap();
        CreateMap<AirlineListView, Airline>().ReverseMap();
        CreateMap<AirportView, Airport>().ReverseMap();
        CreateMap<AirportListView, Airport>().ReverseMap();
        CreateMap<CountryView, Country>().ReverseMap();
        CreateMap<CountryListView, Country>().ReverseMap();
    }
}

我的问题:我想为自动映射器设置一些全局配置选项,但无法弄清楚 where/how 来设置它们。例如:我想将 ValidateInlineMaps 设置为 false(因为当 AssertConfigurationIsValid 抛出 'member not mapped' 异常时,这是作为解决方案提到的)。我还想将所有地图的 MaxDepth 设置为 1 以避免循环引用。我尝试了什么:

1) 在所有配置文件构造函数中将 ValidateInlineMaps 设置为 false:不起作用。

public class AutomapProfileCfg : Profile
{
    public AutomapProfileCfg()
    {
        ValidateInlineMaps = false;
...

2) 在 ConfigureServices 中创建一个 MapperConfiguration 对象,例如:

'var config = new MapperConfiguration(cfg =>
    {
        cfg.ForAllMaps((typeMap, mappingExpression) => {  mappingExpression.MaxDepth(1); });
        cfg.AllowNullCollections = true;
        cfg.ValidateInlineMaps = false;
        //cfg.Advanced.MaxExecutionPlanDepth = 1;
    });'

但我不知道如何link它到映射器实例:只是创建并不会改变映射器的行为。

已经浏览文档并搜索该网站将近一天了:越来越令人沮丧,因为这看起来一定很简单....但不知何故我无法让它工作。任何帮助将不胜感激

配置服务时可以直接指定任何全局配置操作:

services.AddAutoMapper(cfg =>
    {
        cfg.ValidateInlineMaps = true;
        ...other config stuff

    }, typeof(AutomapProfileGen));