构造时的 Automapper 异常
Automapper Exception on construction
我最近将一个项目从 .net5 更新到 .net6,现在我在尝试从 DI 解析 automapper(11.0.1)(或实例化它)时遇到错误。
注册:
services.AddAutoMapper(typeof(MatchProfile).Assembly);
实例化:
var mapper = services.GetService<IMapper>();
堆栈跟踪:
System.InvalidOperationException: Sequence contains no matching element
at System.Linq.ThrowHelper.ThrowNoMatchException()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
at AutoMapper.Configuration.MemberConfigurationExpression`3.Configure(TypeMap typeMap)
at AutoMapper.Configuration.MappingExpressionBase.Configure(TypeMap typeMap)
at AutoMapper.ProfileMap.BuildTypeMap(IGlobalConfiguration configurationProvider, ITypeMapConfiguration config)
at AutoMapper.ProfileMap.Register(IGlobalConfiguration configurationProvider)
at AutoMapper.MapperConfiguration.Seal()
at AutoMapper.MapperConfiguration..ctor(MapperConfigurationExpression configurationExpression)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.<>c.<AddAutoMapperClasses>b__12_2(IServiceProvider sp)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
有没有办法找出错误所在的配置文件?我可以通过某种方式调试它吗?
at AutoMapper.Configuration.MemberConfigurationExpression`3.Configure(TypeMap typeMap)
上面的错误并不是找到错误的有用提示,因为项目非常大,我真的不想为 fow 注释掉行。
为此创建了一个单元测试,它引发了完全相同的错误:
[TestClass]
public class MapperTests
{
[TestMethod]
public void AssertConfigIsValid()
{
var configuration = new MapperConfiguration(cfg =>
{
cfg.AddMaps(typeof(ReportsProfile).Assembly);
});
configuration.AssertConfigurationIsValid();
}
}
编辑:
视图模型:
public class AnswerModel
{
public ICollection<AnswerFieldModel> Fields { get; set; }
}
public abstract class AnswerFieldModel
{
public Guid FieldId { get; set; }
public abstract object GetValue();
}
public sealed class AnswerFieldModel<TValue> : AnswerFieldModel
{
public TValue Value { get; set; }
public override object GetValue() => Value;
}
映射配置文件:
CreateMap<Domain.Entities.Reports.ValueObjects.Answer, AnswerModel>()
.ForMember(dest => dest.Fields, opt => opt.MapFrom(src => src.Fields))
.ReverseMap();
CreateMap<Domain.Entities.Reports.ValueObjects.AnswerField, AnswerFieldModel>()
.ForMember(dest => dest.FieldId, opt => opt.MapFrom(src => src.FieldId))
.Include(typeof(Domain.Entities.Reports.ValueObjects.AnswerField<>), typeof(AnswerFieldModel<>))
.ReverseMap();
CreateMap(typeof(Domain.Entities.Reports.ValueObjects.AnswerField<>), typeof(AnswerFieldModel<>))
.ForMember("Value", opt => opt.MapFrom("Value"))
.ReverseMap();
由于错误起源于MemberConfigurationExpression.Configure()
,我在以下映射表达式中发现了问题:
CreateMap(typeof(Domain.Entities.Reports.ValueObjects.AnswerField<>), typeof(AnswerFieldModel<>))
//.ForMember("Value", opt => opt.MapFrom("Value"))
.ReverseMap();
当这个 .ForMember("Value", opt => opt.MapFrom("Value"))
被注释掉时,一切都按预期工作。
在 AM 10.1.1 中,这并没有引发错误。
这里是documentation如何映射开放泛型。
我最近将一个项目从 .net5 更新到 .net6,现在我在尝试从 DI 解析 automapper(11.0.1)(或实例化它)时遇到错误。
注册:
services.AddAutoMapper(typeof(MatchProfile).Assembly);
实例化:
var mapper = services.GetService<IMapper>();
堆栈跟踪:
System.InvalidOperationException: Sequence contains no matching element
at System.Linq.ThrowHelper.ThrowNoMatchException()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
at AutoMapper.Configuration.MemberConfigurationExpression`3.Configure(TypeMap typeMap)
at AutoMapper.Configuration.MappingExpressionBase.Configure(TypeMap typeMap)
at AutoMapper.ProfileMap.BuildTypeMap(IGlobalConfiguration configurationProvider, ITypeMapConfiguration config)
at AutoMapper.ProfileMap.Register(IGlobalConfiguration configurationProvider)
at AutoMapper.MapperConfiguration.Seal()
at AutoMapper.MapperConfiguration..ctor(MapperConfigurationExpression configurationExpression)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.<>c.<AddAutoMapperClasses>b__12_2(IServiceProvider sp)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
有没有办法找出错误所在的配置文件?我可以通过某种方式调试它吗?
at AutoMapper.Configuration.MemberConfigurationExpression`3.Configure(TypeMap typeMap)
上面的错误并不是找到错误的有用提示,因为项目非常大,我真的不想为 fow 注释掉行。
为此创建了一个单元测试,它引发了完全相同的错误:
[TestClass]
public class MapperTests
{
[TestMethod]
public void AssertConfigIsValid()
{
var configuration = new MapperConfiguration(cfg =>
{
cfg.AddMaps(typeof(ReportsProfile).Assembly);
});
configuration.AssertConfigurationIsValid();
}
}
编辑:
视图模型:
public class AnswerModel
{
public ICollection<AnswerFieldModel> Fields { get; set; }
}
public abstract class AnswerFieldModel
{
public Guid FieldId { get; set; }
public abstract object GetValue();
}
public sealed class AnswerFieldModel<TValue> : AnswerFieldModel
{
public TValue Value { get; set; }
public override object GetValue() => Value;
}
映射配置文件:
CreateMap<Domain.Entities.Reports.ValueObjects.Answer, AnswerModel>()
.ForMember(dest => dest.Fields, opt => opt.MapFrom(src => src.Fields))
.ReverseMap();
CreateMap<Domain.Entities.Reports.ValueObjects.AnswerField, AnswerFieldModel>()
.ForMember(dest => dest.FieldId, opt => opt.MapFrom(src => src.FieldId))
.Include(typeof(Domain.Entities.Reports.ValueObjects.AnswerField<>), typeof(AnswerFieldModel<>))
.ReverseMap();
CreateMap(typeof(Domain.Entities.Reports.ValueObjects.AnswerField<>), typeof(AnswerFieldModel<>))
.ForMember("Value", opt => opt.MapFrom("Value"))
.ReverseMap();
由于错误起源于MemberConfigurationExpression.Configure()
,我在以下映射表达式中发现了问题:
CreateMap(typeof(Domain.Entities.Reports.ValueObjects.AnswerField<>), typeof(AnswerFieldModel<>))
//.ForMember("Value", opt => opt.MapFrom("Value"))
.ReverseMap();
当这个 .ForMember("Value", opt => opt.MapFrom("Value"))
被注释掉时,一切都按预期工作。
在 AM 10.1.1 中,这并没有引发错误。
这里是documentation如何映射开放泛型。