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

标签 c# asp.net-core .net-core automapper

在我的 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 .

我试图检查那些特定的链接:
  • AutoMapper Migrating from static API
  • https://lostechies.com/jimmybogard/2016/01/21/removing-the-static-api-from-automapper

  • 但他们都没有说如何

    最佳答案

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

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

    1.安装 AutoMapper.Extensions.Microsoft.DependencyInjection

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

    services.AddAutoMapper(typeof(MapperProfile));
    

    引用:
    How to using AutoMapper on ASP.NET Core via Dependency Injection

    关于c# - 如何替换 AutoMapper 9.0 中的 `AutoMapper.Mapper.Initialize` 静态调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59274000/

    相关文章:

    c# - asp.net core 中的网络请求

    c# - 串口连续实时数据C#

    c# - ASP.NET Core API 发送用双引号引起来的字符串

    javascript - 跨语言字符串转义

    asp.net-core - ASP.NET 核心高内存消耗

    c# - 安装 Visual Studio 2017 RC 会破坏 dotnet core 1.0.1?

    azure - Service Fabric (ASP.NET Core) 中的应用程序设置

    .net - 将 SPA 中的 cookie 身份验证升级到 .NET Core 2.0

    C# BinaryWriter 内存泄漏

    c# - 无法从 UnityEngine.Vector3 转换为 System.Collections.Generic.List<UnityEngine.Vector3>