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的更多相关文章

  1. Autofac和DynamicProxy2搭配实现Aop动手训练

    http://www.cnblogs.com/zhengwl/p/5433181.html Aop含义:aspect-oriented programming 实现工具介绍 Autofac是一个比较流 ...

  2. AutoFac使用方法总结

    AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举了AutoFac的常用使用方法 ...

  3. autofac使用总结

    转摘自:http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test/ 注册部分: AutoFac是.net平台下的IOC容器产品,它可以管理 ...

  4. AutoFac Ioc依赖注入容器

    本文原著:牛毅  原文路径 http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test/ 理解IOC容器请看下图: 没有使用IOC容器的情况 ...

  5. AutoFac使用方法总结一:注册

    AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举了AutoFac的常用使用方法 ...

  6. Autofac基本使用(转载)

    AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举了AutoFac的常用使用方法 ...

  7. [转][C#]AutoFac 使用方法总结

    AutoFac使用方法总结:Part I 转自:http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test/ AutoFac是.net平台下 ...

  8. Autofac基本使用

    原文:Autofac基本使用 AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举 ...

  9. An Autofac Lifetime Primer

    Or, “Avoiding Memory Leaks in Managed Composition” Understanding lifetime can be pretty tough when y ...

随机推荐

  1. CAS SSO单点登录框架介绍

    1.了解单点登录  SSO 主要特点是: SSO 应用之间使用 Web 协议(如 HTTPS) ,并且只有一个登录入口. SSO 的体系中有下面三种角色: 1) User(多个) 2) Web 应用( ...

  2. leetcode 326 Power of Three (python)

    原题: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...

  3. linux ftp 上传与下载命令解析

    month=`date -d "last month" +"%Y%m"` year=`date +"%Y"` rm /home/yourDi ...

  4. Java基础系列(八)序列化与反序列化

    先来看两个例子 示例一:将对象保存成字节数组,再把对象的字节数组还原为对象 示例中用到的Bean package com.huawei.beans; import java.io.Serializab ...

  5. 指定运行Exchange Powershell的Server

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command ". 'C:\Program Files ...

  6. 关于ajax里边不能识别$(this)的解决方法

    在ajax外面弄个变量var mythis = $(this),然后在里面用就行

  7. 修改/etc/hosts 云服务器 没有做外网转内网的优化

    [root@a mapReduceLog]# scp /data/visitlog/* root@d:/data/mapReduceVisitorLog/a/root@d's password:vis ...

  8. amazonservices api 抽象类 Class Abstraction

    http://php.net/manual/zh/language.oop5.abstract.php MWSOrdersPHPClientLibrary-2013-09-01._V533357711 ...

  9. IO 包中的其他类

    打印流 PrintWriter 和 PrintWriter 直接操作输入流和文件 序列流 SequenceInputStream 对多个输入流进行合并 操作对象 ObjectInputStream 和 ...

  10. C++设计模式 -- 解析和实现

    原文地址  http://c.chinaitlab.com/special/sjms/Index.html#a 导航目录 ※ 设计模式解析和实现之一-Factory模式 ※ 设计模式解析和实现之八-C ...