【发布时间】:2013-07-02 10:46:00
【问题描述】:
Jquery ajax post 调用我的 WebMethod,它获取外部 Web 服务内容,将其转换并返回给客户端。
我的班级:ConfiguratorModel : List<OptionGroup>。
在Application_Start: Mapper.CreateMap<choiceList, OptionGroup>() 内。
在 CreateMap 之后,我也成功运行了Mapper.AssertConfigurationIsValid()。
在我的 Converter 类中执行 Mapper.Map<List<choiceList>, ConfiguratorModel>(choiceLists) 时,自动映射器失败并将以下错误作为 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 是顺便说一句。完全信任地运行。
【问题讨论】:
-
您为什么担心
Application_Start.. 中的线程安全?一次只有一个线程会调用它。也就是说,除非您正在进行某种奇怪的 IIS 设置。 -
"Mapper.CreateMap 不是线程安全的,以后也不会。但是,Mapper.Map 是线程安全的。Mapper 静态类只是 MappingEngine 和 Configuration 对象之上的一个薄包装器。因此,如果您以线程安全的方式在一个中心位置进行配置,请仅使用 Mapper.CreateMap。”参考:stackoverflow.com/questions/10649250/…
-
因此我只想运行一次 CreateMap。
-
Application_Start在应用程序启动时由单个线程调用一次。把CreateMap放在那里,你的问题就解决了.. -
这就是问题所在。如果我把它放在那里,就会出现异常。
标签: c# automapper webmethod