Autofac register and resolve
Passing Parameters to Register
When you register components you have the ability to provide a set of parameters that can be used during the resolution of services based on that component. (If you’d rather provide the parameters at resolution time, you can do that instead.)
Available Parameter Types
Parameters with Reflection Components
Parameters with Lambda Expression Components
With lambda expression component registrations, rather than passing the parameter value at registration time you enable the ability to pass the value at service resolution time. (Read more about resolving with parameters.)
In the component registration expression, you can make use of the incoming parameters by changing the delegate signature you use for registration. Instead of just taking in an IComponentContext parameter, take in an IComponentContext and an IEnumerable<Parameter>:
// Use TWO parameters to the registration delegate:
// c = The current IComponentContext to dynamically resolve dependencies
// p = An IEnumerable<Parameter> with the incoming parameter set
builder.Register((c, p) =>
new ConfigReader(p.Named<string>("configSectionName")))
.As<IConfigReader>();
When resolving with parameters, your lambda will use the parameters passed in:
var reader = scope.Resolve<IConfigReader>(new NamedParameter("configSectionName", "sectionName"));
Resolving Services
After you have your components registered with appropriate services exposed, you can resolve services from the built container and child lifetime scopes. You do this using the Resolve() method:
Passing Parameters to Resolve
Autofac register and resolve的更多相关文章
- Autofac和DynamicProxy2搭配实现Aop动手训练
http://www.cnblogs.com/zhengwl/p/5433181.html Aop含义:aspect-oriented programming 实现工具介绍 Autofac是一个比较流 ...
- AutoFac使用方法总结
AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举了AutoFac的常用使用方法 ...
- autofac使用总结
转摘自:http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test/ 注册部分: AutoFac是.net平台下的IOC容器产品,它可以管理 ...
- AutoFac Ioc依赖注入容器
本文原著:牛毅 原文路径 http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test/ 理解IOC容器请看下图: 没有使用IOC容器的情况 ...
- AutoFac使用方法总结一:注册
AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举了AutoFac的常用使用方法 ...
- Autofac基本使用(转载)
AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举了AutoFac的常用使用方法 ...
- [转][C#]AutoFac 使用方法总结
AutoFac使用方法总结:Part I 转自:http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test/ AutoFac是.net平台下 ...
- Autofac基本使用
原文:Autofac基本使用 AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举 ...
- An Autofac Lifetime Primer
Or, “Avoiding Memory Leaks in Managed Composition” Understanding lifetime can be pretty tough when y ...
随机推荐
- Spring Context及ApplicationContext
web.xml 这是声明了一个父工厂 <context-param> <param-name>contextConfigLocation</param-name> ...
- json.dumps 和 json.dump的区别,load和loads的区别
json.dumps 和 json.dump的区别,load和loads的区别
- 2015-03-12——简析DOM2级事件
DOM2级事件 事件的几种类型:对象事件,鼠标事件,键盘事件,表单事件,W3CDOM事件,以及针对浏览器的事件. 对象事件:window对象,也是javascript对象.load 适用于windo ...
- 我的Android进阶之旅------>Android自定义View实现带数字的进度条(NumberProgressBar)
今天在Github上面看到一个来自于 daimajia所写的关于Android自定义View实现带数字的进度条(NumberProgressBar)的精彩案例,在这里分享给大家一起来学习学习!同时感谢 ...
- Pycharm在创建py文件时,自动添加文件头注释
依次File -> Settings -> Editor -> File and Code Templates -> Python Script 添加以下代码: # -*- ...
- Linux学习笔记(8)文件搜索与帮助
帮助: (1) man ls (2) info ls (3) whatis ls (4) help 搜索: (1) which ls :查看ls命令所在绝对路径 (2) locate user ...
- redis的数据类型与应用场景(二)
1. 如何学习 redis有好多数据类型,有这么多数据类型,我们不可能每个都记得完完全全.但是我们必须知道它有哪些数据类型,每个数据类型是怎样的,有什么作用.redis的每一个数据类型都有一大堆命令, ...
- 用Android NDK编译FFmpeg
附(2018-01-06): 有一个将x264及lame等库集成进去了且基于android的ffmpeg的编译方法,地址参见: https://github.com/writing ...
- java 多线程 day01 创建线程
线程,即代码的执行路径java 面向对象编程.所以线程在java中也是用对象来表示的,创建线程对象的类就是 Thread如下代码即开启一个新的线程,通过线程对象的start方法,即可启动线程 Thre ...
- win10下安装TensorFlow(CPU only)
TensorFlow安装过程 1 环境 我的安装环境:win10 + 64位 +miniconda2+miniconda创建的python3.5.5环境+pip 由于目前TensorFlow在wind ...