首页 新闻 会员 周边

Mapper already initialized. You must call Initialize once per application domain/process.

0
悬赏园豆:10 [待解决问题]

使用AutoMapper 类库时出现下面这个错误:

Mapper already initialized. You must call Initialize once per application domain/process.

请问哪里出了问题,需要怎么调整

问题补充:

/// <summary>
/// AutoMapper IOC容器
/// </summary>
public static class MapperUtil
{
/// <summary>
/// 集合列表类型映射,自定义配置
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="self"></param>
/// <param name="result"></param>
/// <param name="config"></param>
/// <returns></returns>
public static List<TDestination> MapToList<TDestination>(this IEnumerable<object> self, Action<IMapperConfigurationExpression> config)
{
if (self == null)
throw new ArgumentNullException();
Mapper.Reset();
Mapper.Initialize(config); // 这行代码报的错
return Mapper.Map<List<TDestination>>(self);
}
}

谷樵的主页 谷樵 | 初学一级 | 园豆:36
提问于:2019-06-11 09:56

.net framework 还是 .net core ?

dudu 4年前

@dudu: .net framework

龍四 4年前
< >
分享
所有回答(2)
0

Mapper.Initialize 只能执行一次,你的代码中多次执行了 Mapper.Initialize

dudu | 园豆:30994 (高人七级) | 2019-06-11 10:50

自己封装的代码 你看下上面的代码

支持(0) 反对(0) 谷樵 | 园豆:36 (初学一级) | 2019-06-11 11:26

建议在 Application_Start 中调用 Mapper.Initialize ,参考 Automapper - Mapper already initialized error

或者通过 PreApplicationStartMethod

[assembly: PreApplicationStartMethod(typeof(BootStrapper.Initializer), "Initialize")]
支持(0) 反对(0) dudu | 园豆:30994 (高人七级) | 2019-06-11 11:28
0

Mapper.Initialize(config);
这个只能在全局运行一次。之后就直接用Mapper就可以了
所以提前你就要把FormType和ToType注册好

Giant150 | 园豆:1165 (小虾三级) | 2019-06-11 16:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册