【发布时间】:2017-02-13 17:31:19
【问题描述】:
我有一个类库,并且在其中一个类中我正在使用 Automapper。在构造函数中,我这样做:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<ItemSummary, Sponsor>();
cfg.CreateMap<AdminService.SponsorshipType, SponsorshipType>();
});
其中 ItemSummary 由 Web 服务返回,它返回 ItemSummary 对象数组,即 ItemSummary[]。 Sponsor 是我的域模型,包含完全相同的道具(也具有相同的数据类型)。 然后,在我获取 ItemSummary 的方法之一中,我使用如下映射:
var sponsors = _service.GetItems(_accessKey);
List<Sponsor> sponsorsList = new List<Sponsor>();
sponsorsList = Mapper.Map<List<Sponsor>>(sponsors);
return sponsorsList;
这段代码在大约 15 分钟前运行良好。但现在我得到了这个例外:
[AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
ItemSummary -> Sponsor
Admin.Service.PlanOfficeAdminService.ItemSummary ->
Admin.Core.Models.Sponsors.Sponsor]
lambda_method(Closure , ItemSummary , Sponsor , ResolutionContext ) +73
AutoMapper.ResolutionContext.Map(TSource source, TDestination destination) +206
lambda_method(Closure , Object , Object , ResolutionContext ) +194
[AutoMapperMappingException: Error mapping types.
Mapping types:
ItemSummary[] -> List`1
Admin.Service.AdminService.ItemSummary[] ->
System.Collections.Generic.List`1[[Admin.Core.Models.Sponsor, Admin.Core ...
突然出现什么问题?这个类在其他任何地方都没有变化,所以没有,类中没有任何地方可以破坏它。
【问题讨论】:
-
消息很简单,没有
POAItemSummary -> Sponsor的映射,您有ItemSummary -> Sponsor的映射,看起来服务正在返回POA类。 -
没有。实际上那个 POA 前缀是一个错字。所以这个对象实际上叫做ItemSummary。我已经编辑了异常消息,所以现在它对应于实际消息,即没有拼写错误
标签: c# exception mapping automapper class-library