如何替换 AutoMapper 9.0 中的 `AutoMapper.Mapper.Initialize` 静态调用?

How to replace the `AutoMapper.Mapper.Initialize` static call in AutoMapper 9.0?

在我的 Startup.cs 文件中,我的 ConfigureServices 方法中有一个 AutoMapper 配置:

AutoMapper.Mapper.Initialize(c => c.AddMaps(typeof(Models.MapperProfile), typeof(Data.Ef.MapperProfile)));
namespace Rm.Combo.Api.Models
{
    public class MapperProfile : Profile
    {
        public MapperProfile()
        {
            CreateMap<NewCashoutModel, App.Cashouts.InitiateCashoutCommand>();
        }
    }
}
namespace Rm.Combo.Data.Ef
{
    public class MapperProfile : Profile
    {
        public MapperProfile()
        {
            CreateMap<Domain.Cashouts.Cashout, Data.Cashouts.CashoutModel>();
        }
    }
}

自从我从版本 8.1.1 转移到 9.0.0 后,似乎有一定数量的重大更改。

我试图检查那些特定的链接:

但是其中 none 说了如何

从 9.0 开始,静态 API 不再可用。

您可以通过依赖注入使用 AutoMapper,如下所示:

1.Install AutoMapper.Extensions.Microsoft.DependencyInjection

2.Register Startup.cs ConfigureServices 中的一项服务:

services.AddAutoMapper(typeof(MapperProfile));

参考: How to using AutoMapper on ASP.NET Core via Dependency Injection