繁体   English   中英

Automapper 错误说映射器未初始化

[英]Automapper error saying mapper not initialized

我正在使用 Automapper 5.2。 以此链接为基础。 我将逐步描述我经历的设置 Automapper 的过程。

首先,我将 Automapper 添加到 Project.json,如下所示:

PM> Install-Package AutoMapper

其次,我创建了一个文件夹来保存与映射相关的所有文件,称为“Mappings”

第三,我在 mappings 文件夹中自己的文件中设置了 Automapper 的配置:

public class AutoMapperConfiguration
{
    public MapperConfiguration Configure()
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile<ViewModelToDomainMappingProfile>();
            cfg.AddProfile<DomainToViewModelMappingProfile>();
            cfg.AddProfile<BiDirectionalViewModelDomain>();
        });
        return config;
    }
}

第四也在映射文件夹中它自己的文件中,我设置映射配置文件如下:

public class DomainToViewModelMappingProfile : Profile
{
    public DomainToViewModelMappingProfile()
    {
        CreateMap<Client, ClientViewModel>()
           .ForMember(vm => vm.Creator, map => map.MapFrom(s => s.Creator.Username))
           .ForMember(vm => vm.Jobs, map => map.MapFrom(s => s.Jobs.Select(a => a.ClientId)));
    }
}

第五,我在 mappings 文件夹中添加了一个扩展方法作为它自己的文件......接下来在 startup.cs 中使用了它:

public static class CustomMvcServiceCollectionExtensions
{
    public static void AddAutoMapper(this IServiceCollection services)
    {
        if (services == null)
        {
            throw new ArgumentNullException(nameof(services));
        }
        var config = new AutoMapperConfiguration().Configure();
        services.AddSingleton<IMapper>(sp => config.CreateMapper());
    }
}

第六,我将这一行添加到 Startup.cs 中的 ConfigureServices 方法中

        // Automapper Configuration
        services.AddAutoMapper();

最后我在 controller 中使用它来转换一个集合......

 IEnumerable<ClientViewModel> _clientVM = Mapper.Map<IEnumerable<Client>, IEnumerable<ClientViewModel>>(_clients);

根据我之前提到的问题,我以为我已经设置好了一切,所以我可以使用它,但是我在 Controller 的上面一行中遇到了错误...

“映射器未初始化。使用适当的配置调用初始化。如果您尝试通过容器或其他方式使用映射器实例,请确保您没有调用 static Mapper.Map 方法,并且如果您使用的是 ProjectTo 或 UseAsDataSource扩展方法,请确保传入适当的 IConfigurationProvider 实例。”

我错过了什么.. 这是设置 Automapper 的正确方法吗? 我怀疑我在这里错过了一步......非常感谢任何帮助。

AutoMapper 有两种用途:依赖注入和用于向后兼容的旧静态。 您正在将其配置为依赖注入,但随后尝试使用静态。 那是你的问题。 您只需要选择一种方法或另一种方法即可。

如果你想用依赖注入来做到这一点,你的控制器应该将映射器作为构造函数参数保存到一个成员中,然后你需要使用该成员来进行映射:

public class FooController : Controller
{
    private readonly IMapper mapper;

    public FooController(IMapper mapper)
    {
        this.mapper = mapper;
    }

然后:

IEnumerable<ClientViewModel> _clientVM = mapper.Map<IEnumerable<Client>, IEnumerable<ClientViewModel>>(_clients);

对于静态方法,您需要使用您的配置初始化 AutoMapper:

public MapperConfiguration Configure()
{
    AutoMapper.Mapper.Initialize(cfg =>
    {
        cfg.AddProfile<ViewModelToDomainMappingProfile>();
        cfg.AddProfile<DomainToViewModelMappingProfile>();
        cfg.AddProfile<BiDirectionalViewModelDomain>();
    });
}

然后你只需要在 Startup.cs 中调用这个方法; 您将不再注册单身人士。

从 autoMapper V9.0 开始,静态 API 不再可用。 或者您可以从 NuGet 包管理器将版本更改为 6.2.2 请参阅此文档配置 AutoMapper

我有同样的问题。 Automapper 在我的电脑上运行良好。 当我部署时它不工作,因为我错过了 System.ValueTupple.dll。

更好的解决方案是创建这样的扩展方法

  public static DestinationType CastObject<SourceType, DestinationType>(this SourceType obj)
            {
                var config = new MapperConfiguration(cfg =>
                cfg.CreateMap<SourceType, DestinationType>());

                var mapper = config.CreateMapper();

                var entity = mapper.Map<DestinationType>(obj);

                return entity;
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM