ASP.NET Core MVC中的AutoMapper实现 [英] AutoMapper implementation in ASP.NET Core MVC

查看:126
本文介绍了ASP.NET Core MVC中的AutoMapper实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用这里是我的startup.cs

public IServiceProvider ConfigureServices(IServiceCollection services)
{
 …
    services.AddMvc();

    services.AddAutoMapper();

…

    // Autofac configuration
    return ConfigureAutofacContainer(services);
}

这里是我的AutoMapper.Profile实现

public class AutoMapperProfile_NetCore_DtoFromDao : Profile
{
    #region ctor

    public AutoMapperProfile_NetCore_DtoFromDao()
    {
        CreateMaps();
    }

    #endregion

    #region Methods

    protected void CreateMaps()
    {
        if (Mapper.Configuration.FindTypeMapFor(typeof(AddressType),
                                                typeof(AddressTypeDto)) == null)
            CreateMap<AddressType, AddressTypeDto>();

        Mapper.Configuration.AssertConfigurationIsValid();
    }
}

AutoMapperProfile_NetCore_DtoFromDao.CreateMaps()由ServiceCollectionExtensions.AddAutoMapperClasses()调用:

AutoMapperProfile_NetCore_DtoFromDao.CreateMaps() is being called by ServiceCollectionExtensions.AddAutoMapperClasses():

public static class ServiceCollectionExtensions
{
    …
    private static void AddAutoMapperClasses(IServiceCollection services,
               Action<IMapperConfigurationExpression> additionalInitAction, 
               IEnumerable<Assembly> assembliesToScan)
    {
        …
        Mapper.Initialize(cfg =>
        {
            additionalInitAction(cfg);

           foreach (var profile in profiles)
            {
                cfg.AddProfile(profile);
            }
        });
        …
    }
}

我遇到以下例外情况:

类型为"System.InvalidOperationException"的异常发生在 AutoMapper.dll,但未在用户代码中处理

An exception of type 'System.InvalidOperationException' occurred in AutoMapper.dll but was not handled in user code

Q-这是由于配置文件在Mapper.Initialization()期间调用Mapper.Configuration.FindTypeMapFor()吗?

Q - Is this due to the profile calling Mapper.Configuration.FindTypeMapFor() during Mapper.Initialization()?

问-在初始化期间添加一个映射配置之前,是否可以测试现有映射配置?

Q - Is it possible to test for an existing mapping configuration before adding one during initialzation?

System.InvalidOperationException由用户代码未处理
HResult = -2146233079消息=映射器未初始化.通话初始化 具有适当的配置.如果您尝试使用映射器 通过容器或其他方式的实例,请确保您没有 对静态Mapper.Map方法的任何调用,以及是否正在使用 ProjectTo或UseAsDataSource扩展方法,请确保您传入 适当的IConfigurationProvider实例.来源= AutoMapper
堆栈跟踪: 在AutoMapper.Mapper.get_Configuration() 在Dna.NetCore.Core.BLL.Mappers.AutoMapperProfile_NetCore_DtoFromDao.CreateMaps() 在 C:\ Src \ AutoMapper.Extensions.Microsoft.DependencyInjection \ src \ Dna.NetCore.Core.BLL \ Mappers \ AutoMapperProfile_NetCore_DtoFromDao.cs:line 22 在Dna.NetCore.Core.BLL.Mappers.AutoMapperProfile_NetCore_DtoFromDao..ctor() 在 C:\ Src \ AutoMapper.Extensions.Microsoft.DependencyInjection \ src \ Dna.NetCore.Core.BLL \ Mappers \ AutoMapperProfile_NetCore_DtoFromDao.cs:line 13 InnerException:

System.InvalidOperationException was unhandled by user code
HResult=-2146233079 Message=Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance. Source=AutoMapper
StackTrace: at AutoMapper.Mapper.get_Configuration() at Dna.NetCore.Core.BLL.Mappers.AutoMapperProfile_NetCore_DtoFromDao.CreateMaps() in C:\Src\AutoMapper.Extensions.Microsoft.DependencyInjection\src\Dna.NetCore.Core.BLL\Mappers\AutoMapperProfile_NetCore_DtoFromDao.cs:line 22 at Dna.NetCore.Core.BLL.Mappers.AutoMapperProfile_NetCore_DtoFromDao..ctor() in C:\Src\AutoMapper.Extensions.Microsoft.DependencyInjection\src\Dna.NetCore.Core.BLL\Mappers\AutoMapperProfile_NetCore_DtoFromDao.cs:line 13 InnerException:

推荐答案

确定.这里有几件事.您的AutoMapper配置,最简单的配置方法是:

OK. A few things here. Your AutoMapper config, the easiest way to build this is just:

services.AddAutoMapper(typeof(Startup));

这将从Startup类的程序集中扫描Profiles,并使用Mapper.Initialize自动添加它们.请勿致电Mapper.此后初始化.

That scans the assembly from the Startup class for Profiles, and automatically adds them using Mapper.Initialize. DO NOT call Mapper.Initialize after this.

接下来,您的个人资料.您正在做很多不应该做的事情.首先,您的个人资料正在调用AssertConfigurationIsValid-不会.接下来,它正在检查现有的TypeMap-不用.只需调用基本的CreateMap方法即可.

Next, your profile. You're doing a lot of things you shouldn't. First, your profile is calling AssertConfigurationIsValid - don't. Next, it's checking for existing TypeMaps - don't. Just call the base CreateMap method, that's it.

最后,您还有一个额外的AddAutoMapperClasses调用.不要使用它.摆脱它.您只需要"services.AddAutoMapper". AddAutoMapper方法使用传入的程序集中的Profile类调用Mapper.Initialize.

Finally, you've got an extra AddAutoMapperClasses call. Don't use that. Get rid of it. You just need the "services.AddAutoMapper". The AddAutoMapper method calls Mapper.Initialize, with the Profile classes found in the assembly you've passed in.

这篇关于ASP.NET Core MVC中的AutoMapper实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆