最开始的代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection; using AutoMapper; using Happy.ExtentionMethods;
using Happy.Bootstrap; namespace Happy.Bootstrap.AutoMapper
{
/// <summary>
/// 自动添加配置。
/// </summary>
public class AutoAddProfilePlugin : IBootstrapPlugin
{
/// <inheritdoc />
public void Start(IBootstrapService bootstrapService, Assembly assembly)
{
bootstrapService.MustNotNull("bootstrapService");
assembly.MustNotNull("assembly"); foreach (var type in assembly.GetTypes())
{
if (type.IsAbstract || type.IsInterface)
{
continue;
} if (typeof(Profile).IsAssignableFrom(type))
{
Mapper.Initialize(x => {
x.AddProfile(Activator.CreateInstance(type) as Profile);
});
}
}
}
}
}

问题

我的项目中,每个 dll 都是自描述的,系统启动的时候,对每个 dll 对执行上面的插件,结果, Mapper.Initialize() 只有最后一次配置才有效,前面的配置会丢失。

最后的代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection; using AutoMapper; using Happy.ExtentionMethods;
using Happy.Bootstrap; namespace Happy.Bootstrap.AutoMapper
{
/// <summary>
/// 自动添加配置。
/// </summary>
public class AutoAddProfilePlugin : IBootstrapPlugin
{
/// <inheritdoc />
public void Start(IBootstrapService bootstrapService, Assembly assembly)
{
bootstrapService.MustNotNull("bootstrapService");
assembly.MustNotNull("assembly"); foreach (var type in assembly.GetTypes())
{
if (type.IsAbstract || type.IsInterface)
{
continue;
} if (typeof(Profile).IsAssignableFrom(type))
{
Mapper.AddProfile(Activator.CreateInstance(type) as Profile);
}
}
}
}
}

AutoMapper: Mapper.Initialize() 只能调用一次,Why?的更多相关文章

  1. AutoMapper.Mapper.CreateMap报“System.NullReferenceException: 未将对象引用设置到对象的实例。”异常复现

    >>Agenda: >>Ⅰ.国庆假期问题出现 >>Ⅱ.双休日异常再次出现 >>Ⅲ.排障 >>Ⅳ.异常复盘 >>Ⅴ.修复后监测 & ...

  2. load 与initialize的调用顺序小结

    开发中实用方法固然是最贴近应用的,当一些程序原理还是要先搞清晰,根据查找的一些资料,总结了一些load与initialize的调用. APP启动到执行main函数之前,程序就执行了很多代码   执行顺 ...

  3. OkHttp踩坑记:为何 response.body().string() 只能调用一次?

    想必大家都用过或接触过 OkHttp,我最近在使用 Okhttp 时,就踩到一个坑,在这儿分享出来,以后大家遇到类似问题时就可以绕过去. 只是解决问题是不够的,本文将 侧重从源码角度分析下问题的根本, ...

  4. jsonp 跨域只能调用一次ajax(无法多次调用或者循环调用)

    jsonp 跨域只能掉用一次ajax(无法多次调用或者循环调用) 百度搜索关键字:jsonp 只能调用一次ajax 解决方法 //回调函数设置,给后台执行        window[callback ...

  5. mapreduce中reduce中的迭代器只能调用一次。其实迭代器就只能调用一次

    亲测,只能调用一次,如果想想在一次reduce重复使用迭代器中的数据,得先取出来放在list中然后在从list中取出来!!多次读取reduce函数中迭代器的数据 public static void ...

  6. 拦截器中,request中getReader()和getInputStream()只能调用一次,构建可重复读取inputStream的request.

    由于 request中getReader()和getInputStream()只能调用一次 在项目中,可能会出现需要针对接口参数进行校验等问题. 因此,针对这问题,给出一下解决方案 实现方法:先将Re ...

  7. +load和+initialize方法调用时机

    一.+load方法什么时候调用 +load方法会在runtime加载类.分类时调用(程序运行起来会先去加载调用+load 跟你引用没有引用其头文件没有关系).每个类.分类的+load,在程序运行过程中 ...

  8. ServletRequest中getReader()和getInputStream()只能调用一次的解决办法

    转载:http://blog.sina.com.cn/s/blog_870cd7b90101fg58.html 最近使用spring mvc做项目,数据格式是json,有一个功能是实现记录请求的参数, ...

  9. ServletRequest中getReader()和getInputStream()只能调用一次的解决办法(转)

    原文地址:http://liwx2000.iteye.com/blog/1542431 原文作者:liwx2000 为了提高项目安全性,拦截非法访问,要给项目增加了一个过滤器,拦截所有的请求,校验是否 ...

随机推荐

  1. ExampleConfigurationSectionHandler

    ExampleConfigurationSectionHandler.cs using System; using System.Collections.Generic; using System.C ...

  2. C# Zip解压缩,规避 [content_types].xml 文件

    使用 System.IO.Packaging.Package 进行压缩和解压时,会自动生成 [content_types].xml 文件. The Structure of the [Content_ ...

  3. 个人博客www.0x002.com

    cnblogs只是相当于一个备份,感兴趣的读者赏脸关注下.

  4. vsftp 虚拟用户+MySQL认证独立家目录

    centos7 系统 安装包 yum -y install mariadb vsftpd openssl-devel  mysql-devel  pam-devel yum -y groupinsta ...

  5. vdp配置

    转:http://jiangjianlong.blog.51cto.com/3735273/1902879 本文将介绍VDP 6.1.2的部署与配置,主要内容包括部署VDP的OVA模板.初始化配置VD ...

  6. SQL注入实验

    看到他们黑站感觉很有意思的样子,于是我也玩了一下午,虽然都是些狠狠狠简单的东西,不过还是记录下来啦. 虽然和我现在做的没啥关系,不过,,,挺好 浏览器的“工具”——“internet选项”——“高级” ...

  7. thinkphp5使用redis

    1.设置应用配置文件config.php type可以是很多分类File.Redis等等 2.thinkphp5使用redis新建application/index/controller/index. ...

  8. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    登录服务器,使用root用户连接mysql时出现错误提示: $ bin/mysql -uroot -p Enter password: ERROR (HY000): Can't connect to ...

  9. BZOJ 4066 简单题(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4066 [题目大意] 要求维护矩阵内格子加点和矩阵查询 [题解] 往KD树上加权值点,支 ...

  10. 【堆优化Dijkstra+字典序最短路方案】HDU1385-Minimum Transport Cost

    [题目大意] 给出邻接矩阵以及到达各个点需要付出的代价(起点和终点没有代价),求出从给定起点到终点的最短路,并输出字典序最小的方案. [思路] 在堆优化Dijkstra中,用pre记录前驱.如果新方案 ...