【问题标题】:AutoMapper implementation in ASP.NET Core MVCASP.NET Core MVC 中的 AutoMapper 实现
【发布时间】:2016-10-20 05:06:53
【问题描述】:

我正在尝试使用https://lostechies.com/jimmybogard/2016/07/20/integrating-automapper-with-asp-net-core-di 中描述的技术在 ASP.NET Core MVC 应用程序中实现 AutoMapper。

这里是my startup.cs

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

    services.AddAutoMapper();

…

    // Autofac configuration
    return ConfigureAutofacContainer(services);
}

这里是my AutoMapper.Profile implementation

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() 调用:

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 但未在用户代码中处理

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

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

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 内部异常:

【问题讨论】:

    标签: asp.net-core automapper


    【解决方案1】:

    好的。这里有几件事。您的 AutoMapper 配置,构建它的最简单方法就是:

    services.AddAutoMapper(typeof(Startup));
    

    从 Startup 类中扫描程序集以查找配置文件,并使用 Mapper.Initialize 自动添加它们。不要在此之后调用 Mapper.Initialize。

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

    最后,您有一个额外的 AddAutoMapperClasses 调用。不要用那个。摆脱它。您只需要“services.AddAutoMapper”。 AddAutoMapper 方法调用 Mapper.Initialize,并在您传入的程序集中找到 Profile 类。

    【讨论】:

    • 如果在基于类库的服务中使用 AutoMapper 并通过 xUnit 测试对其进行测试,那么我们应该在哪里进行“services.AddAutoMapper”注入?在 xUnit 测试夹具的开始?这是一个 .NET Core 类库 xUnit 项目,其中有许多用于 .NET Framework 的示例。谢谢。
    • 这就是我所做的:lostechies.com/jimmybogard/2016/10/24/… 基本上将其作为夹具静态设置的一部分来调用“启动”配置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多