Application_Start中CreateMap时的Automapper异常

时间:2013-07-02 10:46:01

标签: c# automapper webmethod

Jquery ajax post调用我的WebMethod,它获取外部Web服务内容,转换它并返回客户端。

我的班级:ConfiguratorModel : List<OptionGroup>

Application_Start内:Mapper.CreateMap<choiceList, OptionGroup>()

在CreateMap之后,我也成功运行了Mapper.AssertConfigurationIsValid()

在我的Converter类中执行Mapper.Map<List<choiceList>, ConfiguratorModel>(choiceLists)时,automapper失败并返回以json为客户端的错误:

{"Message":"TryingtomapSystem.Collections.Generic.List`1[[Constructor.OFML.Services.Basket.choiceList,Constructor.OFML,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]]toConstructor.OFML.ConfiguratorModel.\nExceptionoftype\u0027AutoMapper.AutoMapperMappingException\u0027wasthrown.","StackTrace":"atAutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContextcontext)\r\natAutoMapper.MappingEngine.Map(Objectsource,TypesourceType,TypedestinationType)\r\natAutoMapper.MappingEngine.Map[TSource,TDestination](TSourcesource)\r\natAutoMapper.Mapper.Map[TSource,TDestination](TSourcesource)\r\natConstructor.OFML.ConfiguratorConverter.ConvertToConfiguratorModel(List`1choiceLists)inc:\\EPiServer\\Sites\\bouvet_sbe\\Constructor\\trunk7\\libraries\\Constructor.FOML\\ConfiguratorConverter.cs:line58\r\natConstructor.OFML.ConfiguratorConverter.BuildConfiguratorModel(List`1choiceLists,articleDataarticleData)inc:\\EPiServer\\Sites\\bouvet_sbe\\Constructor\\trunk7\\libraries\\Constructor.FOML\\ConfiguratorConverter.cs:line68\r\natConstructor.OFML.ConfiguratorConverter.BuildConfiguratorResultModel(List`1choiceLists,articleDataarticleData,StringimageUrl)inc:\\EPiServer\\Sites\\bouvet_sbe\\Constructor\\trunk7\\libraries\\Constructor.FOML\\ConfiguratorConverter.cs:line90\r\natConstructor.OFML.OfmlService.GetConfigurationResult(StringsessionId,StringarticleId)inc:\\EPiServer\\Sites\\bouvet_sbe\\Constructor\\trunk7\\libraries\\Constructor.FOML\\OfmlService.cs:line89\r\natConstructor.Services.ConfiguratorService.GetInitialJson(StringsessionId,StringarticleId)inc:\\EPiServer\\Sites\\bouvet_sbe\\Constructor\\trunk7\\www\\Services\\ConfiguratorService.asmx.cs:line32","ExceptionType":"AutoMapper.AutoMapperMappingException"}

如果我在Mapper.Map之前移动Mapper.CreateMap,一切都像魅力一样。但是CreateMap不是线程安全的,每次运行它都是一个糟糕的解决方法。你知道我应该怎么做吗?我的IIS是顺便说一句。完全信任。

1 个答案:

答案 0 :(得分:0)

只要在静态类中调用它,

CreateMap位置应该不是问题。

这样的事情应该有效:

 public static class AutoMapperConfiguration
 {
    public static void Configure()
    {
      ConfigureSomeMapping();
    }

    private static void ConfigureSomeMapping()
    {
       Mapper.CreateMap<...>();
     } 
  }

在Application_Start中,调用

 AutoMapperConfiguration.Configure();

您还可以使用AutoMapper配置文件使其更清晰。