You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AutoMapper未使用SimpleInjector激活器致接口映射失败

解决AutoMapper映射接口时无法创建实例的问题

环境信息

  • .NET 6
  • AutoMapper v12
  • SimpleInjector v5.4.1

错误堆栈

AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types:
EntityCollection`1 -> IEnumerable`1
X.DAL.HelperClasses.EntityCollection`1[[X.DAL.EntityClasses.WFCurrencyEntity, X.DAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IEnumerable`1[[X.Model.Common.ICurrency, X.Model.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
 ---> AutoMapper.AutoMapperMappingException: Cannot create interface X.Model.Common.ICurrency

Mapping types:
WFCurrencyEntity -> ICurrency
X.DAL.EntityClasses.WFCurrencyEntity -> X.Model.Common.ICurrency

Type Map configuration:
WFCurrencyEntity -> ICurrency
X.DAL.EntityClasses.WFCurrencyEntity -> X.Model.Common.ICurrency
   at lambda_method30(Closure , Object , IEnumerable`1 , ResolutionContext )
   --- End of inner exception stack trace ---
   at lambda_method30(Closure , Object , IEnumerable`1 , ResolutionContext )
   at X.Repository.RepositoryBase`3.FindAsync(IFilter filter)`

已确认的有效配置

  • SimpleInjector容器注册正常:container.Register<ICurrency, Currency>(),通过container.GetInstance(typeof(ICurrency))能成功获取Currency实例
  • AutoMapper映射配置已加载:CreateMap<ICurrency, WFCurrencyEntity>(MemberList.Source).ReverseMap();,调试时确认该映射存在于IMapper配置中
  • 推测问题:AutoMapper未使用SimpleInjector容器的GetInstance作为实例激活器

现有Startup.cs中的Mapper创建代码

var config = new MapperConfiguration(config =>
{
    config.ConstructServicesUsing(t => container.GetInstance(t));
    config.AllowNullCollections = true;
    config.AllowNullDestinationValues = true;
    config.AddProfile(new Repository.RepositoryProfile());
});

container.Register(config.CreateMapper, Lifestyle.Singleton);

解决方案

1. 调整Mapper的注册方式(核心修复)

不要直接注册config.CreateMapper的结果,改用工厂方法延迟创建Mapper实例,确保容器已完成所有必要注册且激活器正确绑定:

container.Register<IMapper>(() => 
{
    var mapperConfig = new MapperConfiguration(cfg =>
    {
        // 明确指定用SimpleInjector创建服务实例
        cfg.ConstructServicesUsing(type => container.GetInstance(type));
        cfg.AllowNullCollections = true;
        cfg.AllowNullDestinationValues = true;
        cfg.AddProfile(new Repository.RepositoryProfile());
    });
    // 可选但推荐:验证映射配置,提前发现错误
    mapperConfig.AssertConfigurationIsValid();
    return mapperConfig.CreateMapper();
}, Lifestyle.Singleton);

2. 显式配置接口映射的实例创建逻辑(可选补充)

如果上述修复后仍有问题,可以在映射配置中明确指定如何创建ICurrency实例,确保AutoMapper调用容器的激活器:

CreateMap<WFCurrencyEntity, ICurrency>()
    .ConstructUsing((source, context) => 
        context.Mapper.ServiceCtor(typeof(ICurrency)) as ICurrency);

3. 确认容器注册顺序

确保container.Register<ICurrency, Currency>()的代码执行在创建Mapper配置之前,避免AutoMapper尝试获取实例时,容器中还未注册该类型。

原因说明

之前的代码直接调用config.CreateMapper并注册为单例,可能在创建Mapper实例时,SimpleInjector容器尚未完成所有类型的注册,或者ConstructServicesUsing的委托没有正确关联到容器的实例获取逻辑。通过工厂方法延迟创建Mapper,能确保容器已准备好提供所需的实例,同时让AutoMapper的激活器正确生效。

内容的提问来源于stack exchange,提问作者Neven Marić

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 17:03:09