最开始的代码

 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. Python输入/输出

    1.在python2.x中raw_input( )和input( ),两个函数都存在,其中区别为 raw_input( )---将所有输入作为字符串看待,返回字符串类型 input( )-----只能 ...

  2. js对象的属性:数据(data)属性和访问器(accessor)属性

    此文为转载,原文: 深入理解对象的数据属性与访问器属性 创建对象的方式有两种:第一种,通过new操作符后面跟Object构造函数,第二种,对象字面量方式.如下 var person = new Obj ...

  3. tidb 记录文档

    ansible-playbook stop.yml   / start.yml 重启集群,在ansible目录下执行 SHOW STATS_META; 查看统计信息 重启集群:ansible-play ...

  4. nginx一些细节知识点补充

    1.upstream 配置代理 upstream local_tomcat { server localhost:8080; } server{ location / { proxy_pass htt ...

  5. 在android studio中集成javah, ndk-build进行JNI开发

    最近在搞一个android上控制LED灯闪烁的功能,用到了串口编程,搜索了一下,发现Google发布了一个demo,android-serialport-api.有现成的代码和APK,要想自己改JNI ...

  6. spring boot mybatis 多数据源配置

    package com.xynet.statistics.config.dataresources; import org.springframework.jdbc.datasource.lookup ...

  7. 最详细的vue-cli安装教程 &^没有之一 ^& 大神亲测。。╮( ̄▽  ̄)╭

    这里介绍使用git安装,电脑自带命令行依然可以使用进行安装 第一步 node环境安装 1.1 如果本机没有安装node运行环境,请下载node 安装包进行安装 1.2 如果本机已经安装node的运行换 ...

  8. 虚拟机hadoop集群搭建

    hadoop tar -xvf hadoop-2.7.3.tar.gz mv hadoop-2.7.3 hadoop 在hadoop根目录创建目录 hadoop/hdfs hadoop/hdfs/tm ...

  9. @Resource与@Autowired注解的区别(转)

    Spring不但支持自己定义的@Autowired注解,还支持由JSR-250规范定义的几个注解.如:@Resource.@PostConstruct及@PreDestroy 1.@Autowired ...

  10. 系统导出数据到excel,数据量过大(大约10W)条,导致服务器 cpu 100%解决方法

    系统导出数据到excel,数据量过大(大约10W)条,导致服务器 cpu 100%解决方法