【问题标题】:Automapper exception started showing suddenlyAutomapper 异常突然开始显示
【发布时间】: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 -&gt; Sponsor 的映射,您有 ItemSummary -&gt; Sponsor 的映射,看起来服务正在返回 POA 类。
  • 没有。实际上那个 POA 前缀是一个错字。所以这个对象实际上叫做ItemSummary。我已经编辑了异常消息,所以现在它对应于实际消息,即没有拼写错误

标签: c# exception mapping automapper class-library


【解决方案1】:

没关系。有人在域模型中引入了 AutoMapper 似乎根本不喜欢的参数化构造函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多