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

Eho*_*ret 3 c# automapper .net-core asp.net-core

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

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

自从我从版本8.1.1转移到9.0.0.

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

但他们都没有说如何

Ren*_*ena 6

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

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

1.安装 AutoMapper.Extensions.Microsoft.DependencyInjection

2.ConfigureServices在Startup.cs上注册一个服务:

services.AddAutoMapper(typeof(MapperProfile));
Run Code Online (Sandbox Code Playgroud)

参考: 如何通过依赖注入在 ASP.NET Core 上使用 AutoMapper