关于Autofac中的AsSelf()具体有什么用,有点好奇查了文档了解了下

Services vs. Components

When you register components, you have to tell Autofac which services that component exposes. By default, most registrations will just expose themselves as the type registered:

// This exposes the service "CallLogger"
builder.RegisterType<CallLogger>();

Components can only be resolved by the services they expose. In this simple example it means:

// This will work because the component
// exposes the type by default:
scope.Resolve<CallLogger>();// This will NOT work because we didn't
// tell the registration to also expose
// the ILogger interface on CallLogger:
scope.Resolve<ILogger>();

You can expose a component with any number of services you like:

builder.RegisterType<CallLogger>().As<ILogger>().As<ICallInterceptor>();

Once you expose a service, you can resolve the component based on that service. Note, however, that once you expose a component as a specific service, the default service (the component type) is overridden:

// These will both work because we exposed
// the appropriate services in the registration:
scope.Resolve<ILogger>();
scope.Resolve<ICallInterceptor>();// This WON'T WORK anymore because we specified
// service overrides on the component:
scope.Resolve<CallLogger>();

If you want to expose a component as a set of services as well as using the default service, use the AsSelf method:

builder.RegisterType<CallLogger>().AsSelf().As<ILogger>().As<ICallInterceptor>();

Now all of these will work:

// These will all work because we exposed
// the appropriate services in the registration:
scope.Resolve<ILogger>();
scope.Resolve<ICallInterceptor>();
scope.Resolve<CallLogger>();

一个是一个接口有多个实现,AsSelf()的话就是设置为默认的实现了。

参考文档说明

Autofac中的AsSelf()作用相关推荐

  1. VMware虚拟机文件夹中各文件作用详解

    VMware虚拟机文件夹中各文件作用详解 虚拟机的文件管理由VMware Workstation来执行. 一个虚拟机一般以一系列文件的形式储存在宿主机中, 这些文件一般在由workstation为虚拟 ...

  2. Science:亚硝酸盐氧化细菌在黑暗海洋中的主要作用

    文章目录 亚硝酸盐氧化细菌在黑暗海洋中的主要作用 写在前面 摘要 热心肠日报 背景 结果 图1 非全长16S rRNA基因序列的系统发育树 图2 通过募集宏基因组片段的方法确定亚硝酸盐氧化细菌单扩增基 ...

  3. 丰度决定了细菌在复杂群落中的功能作用

    本文转载自"微生态笔记",已获授权 标题:Abundance determines the functional role of bacterial phylotypes in c ...

  4. tensorflow2.0中valid_data的作用是在训练的过程对对比训练数据与测试数据的准确率 损失率,便于判断模型的训练效果:是过拟合还是欠拟合(过拟合)

    tensorflow2.0中valid_data的作用是在训练的过程对对比训练数据与测试数据的准确率,便于判断模型的训练效果:是过拟合还是欠拟合 过拟合:训练数据的准确率较高而测试数据的准确率较低 欠 ...

  5. C#中构造函数的作用

    C#中构造函数的作用 共同点: 都是实例化对象,初始化数据的 默认构造是说所有的类都从祖先object那继承了空参的构造方法,你不写与写空参构造都存在,而有参数的构造一般是自己写的,写就有不写就没有, ...

  6. python里dir是什么意思_python中dir什么作用

    python中dir的作用是:1.dir函数传入数据类型返回该数据类型的所有内置方法:2.dir函数传入模块名返回该模块的所有属性和方法. dir() 内置函数的作用 python 内置方法有很多,无 ...

  7. 浅析SEO优化中标签的作用?

    目前,已经有很多企业网站有专门的优化人员来进行SEO优化,在网站SEO优化的过程中会使用到很多标签来利于搜索引擎的读取,提升网站的排名和权重.就以大连网龙科技旗下产品云霸屏为例来讲,云霸屏作为一款全网 ...

  8. Hibernate 中lazy的作用

    Hibernate 中lazy的作用 上一篇 / 下一篇  2009-06-24 20:02:20 / 个人分类:java 查看( 506 ) / 评论( 2 ) / 评分( 5 / 0 ) < ...

  9. 【Android 逆向】Android 进程注入工具开发 ( 调试进程中寄存器的作用 | 通过 EIP 寄存器控制程序运行 | EIP 寄存器的存档与恢复 )

    文章目录 一.调试进程中寄存器的作用 二.通过 EIP 寄存器控制程序运行 三.EIP 寄存器的存档与恢复 一.调试进程中寄存器的作用 内存是一个线性结构 , 将动态库加载到内存中后 , 每个动态库文 ...

最新文章

  1. IO流(二)转换流、序列化、commons-IO框架
  2. python数据分析基础 余本国_Python数据分析基础
  3. 关于Mongodb索引创建的一些体会
  4. kettle学习笔记(二)——kettle基本使用
  5. 人工神经网络相对于支持向量机有什么优势? [关闭]
  6. Python之 sklearn:sklearn中的RobustScaler 函数的简介及使用方法之详细攻略
  7. django中的ajax_post请求
  8. 【Python CheckiO 题解】Digits Multiplication
  9. 这个教程价值有点高,利用Python制作全自动化营销软件!
  10. typescript之prototype
  11. 管理感悟:一个人的工作效率是不可能提高的
  12. 简单版本CRM 客户管理系统设计
  13. 小程序tabBar右上角添加角标
  14. RabbitMQ 使用的AMQP模型解析
  15. Ural1671 Anansi's Cobweb
  16. C语言 读写锁pthread_rwlock_init
  17. 快乐技术沙龙技术分享之账户助手
  18. 我室友打了一把王者我6分钟搞会了eclipse安装与配置
  19. Quartus 添加外部modelsim仿真工具
  20. 使用批处理修改注册表的命令参数

热门文章

  1. Python数据类型,字符编码,文件处理
  2. 算法-蓝桥杯习题(2-2)
  3. 大数据”时代 电信信息安全再不能耽误了
  4. c++双向列表释放_8种高级的Python列表使用技巧,你都会了吗?
  5. 赵国鑫 java_花还有重开日吗?32 岁的罗斯又被廉价交易
  6. 计算机科学与技术国科大,朋辈说‖国科大计算机科学与技术专业漫游指南
  7. 幼儿-知识与能力【2】
  8. mstar android tv,关于MStar Android TV配置I2S数字音频输出
  9. Android官方文档翻译 十二 3.Supporting Different Devices
  10. 物理机与虚拟机共享文件