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 ...
随机推荐
- JZOJ.5285【NOIP2017模拟8.16】排序
Description
- Javascript调用WinForm方法
window.external.MyMessageBox('javascript访问C#代码')
- Java 常用工具类之 String 类
String 类的特点: 字符串对象一旦被初始化就不会被改变. //以下代码的区别: String s = "abc"; // 在常量池中创建一个字符串对象, 池中没有就建立, 池 ...
- flannel源码分析---backend为vxlan
// backend/vxlan/vxlan.go func (be *VXLANBackend) RegisterNetwork(ctx context.Context, network strin ...
- javascript教程5:--BOM操作
1.BOM 简介 所谓的 BOM 即浏览器对象模型(Browser Object Model).BOM 赋予了 JS 操作浏览器的能力,即 window 操作.DOM 则用于创建删除节点,操作 HTM ...
- linux内核打印级别
1.printk()是一个内核的一个记录日志的机制,经常用来记录信息或者警告.printk可以指定输出日志的优先级,在include/linux/kern_levels.h中有相应的宏定义 #defi ...
- HDU1069:Monkey and Banana(最长上升子序列的应用)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1069 这题挺简单的,给定一个箱子的长宽高,要求啰箱子,但必须保证下面箱子的长和宽必须大于上面的箱子. 一个 ...
- pandas(四)唯一值、值计数以及成员资格
针对Series对象,从中抽取信息 unique可以得到Series对象的唯一值数组 >>> obj = Series(['c','a','d','a','a','b','b','c ...
- 《深度学习》Textbook第十章学习笔记
深度学习 第10章 序列建模:循环和递归网络 1.循环神经网络介绍 相比卷积神经网络:专门用于处理网格化的数据(如图像),可以很容易扩展到更具有很大宽度和高度的图像,以及处理大小可变的图像: 循环神经 ...
- windows8系统安装MongoDB 2.6.3配置服务启动
转自:http://winfan.net/655.html 1. 下载mongodb包2. 解压zip至 D:\MongoDB3. 创建目录 D:\MongoDB\data\db4. 创建日志目录 D ...