During application execution, you’ll need to make use of the components you registered. You do this by resolving them from a lifetime scope.

The container itself is a lifetime scope, and you can technically just resolve things right from the container. It is not recommended to resolve from the container directly, however.

When you resolve a component, depending on the instance scope you define, a new instance of the object gets created. (Resolving a component is roughly equivalent to calling “new” to instantiate a class. That’s really, really oversimplifying it, but from an analogy perspective it’s fine.) Some components may need to be disposed (like they implement IDisposable) - Autofac can handle disposing those components for you when the lifetime scope is disposed.

However, the container lives for the lifetime of your application. If you resolve a lot of stuff directly from the container, you may end up with a lot of things hanging around waiting to be disposed. That’s not good (and you may see a “memory leak” doing that).

Instead, create a child lifetime scope from the container and resolve from that. When you’re done resolving components, dispose of the child scope and everything gets cleaned up for you.

(When you’re working with the Autofac integration libraries, this child scope creation is largely done for you so you don’t have to think about it.)

Ioc:autofac lifetime scope.的更多相关文章

  1. IOC:AutoFac使用demo

    使用autofac 实现依赖注入 1.引用 autofac.dll 和 autofac.configuration.dll 2.新增接口 IDAL using System; using System ...

  2. Ioc:Autofac Registration Concepts

    Reflection Components When using reflection-based components, Autofac automatically uses the constru ...

  3. Ioc:The basic pattern for integrating Autofac into your application

    The basic pattern for integrating Autofac into your application is: Structure your app with inversio ...

  4. .net core2.0下Ioc容器Autofac使用

    .net core发布有一段时间了,最近两个月开始使用.net core2.0开发项目,大大小小遇到了一些问题.准备写个系列介绍一下是如何解决这些问题以及对应技术.先从IOC容器Autofac开始该系 ...

  5. My.Ioc 代码示例——Lifetime 和 ILifetimeScope

    很多 Ioc 框架在创建对象的过程中,都会采取某种方式来缓存/复用/释放已构建的对象.在 My.Ioc 中,这个目的是通过 Lifetime/ILifetimeScope 来实现的.其中,Lifeti ...

  6. IoC之AutoFac(三)——生命周期

    阅读目录 一.Autofac中的生命周期相关概念 二.创建一个新的生命周期范围 三.实例周期范围 3.1   每个依赖一个实例(InstancePerDependency) 3.2  单个实例(Sin ...

  7. The request lifetime scope cannot be created because the HttpContext is not available

    项目中应用了Autofac,在Global轮询处理Job的时候,需要获取现有得Service,而这些Service已经通过Autofac进行了配置,所以理所应当的用下面的代码去获取了. Depende ...

  8. An Autofac Lifetime Primer

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

  9. Ioc容器Autofac系列(1)-- 初窥

     一.前言 第一次接触Autofac是因为CMS系统--Orchard,后来在一个开源爬虫系统--NCrawler中也碰到过,随着深入了解,我越发觉得Ioc容器是Web开发中必不可少的利器.那么,Io ...

随机推荐

  1. generator自动生成代码

    idea设置generator自动生成代码: http://blog.csdn.net/sunny243788557/article/details/45166397

  2. sonarLint--强大的代码审查工具(插件)

    idea也有的一个插件 贴上一个eclipse的sonarlint用法 http://blog.csdn.net/limm33/article/details/51166840 不过听说从2015年1 ...

  3. Sublime Text 使用介绍/全套快捷键及插件推荐

    如果说Notepad++是一款不错Code神器,那么Sublime Text应当称得上是神器滴哥.Sublime Text最大的优点就是跨平台,Mac和Windows均可完美使用:其次是强大的插件支持 ...

  4. **PHP二维数组遍历时同时赋值

    php 二维数组遍历赋值 我个人在项目中的写法: //遍历二维数组foreach($tmp_array as $key => $value){ //动态生成图片的URL $attach_url ...

  5. 浙江省“一卡通”异地就医,C#调用省一卡通动态库

    前言,最近学习调用 浙江省一卡通业务,主要就是调用一个DLL,动态库文件,这个动态库是浙大网新研发的. 借着自学的机会把心得体会都记录下来,方便感兴趣的小伙伴学习与讨论. 内容均系原创,欢迎大家转载分 ...

  6. Qt_mingw搭建opencv开发环境

    Qt在windows下共有2个版本:mingw和msvc.其中mingw使用gcc编译器,msvc使用微软的VC编译器.针对不同版本Qt,使用Opencv的方式也不同. 区别 msvc, 可以使用op ...

  7. 【AtCoder】ARC093

    C - Traveling Plan 相当于一个环,每次删掉i点到两边的距离,加上新相邻的两个点的距离 代码 #include <bits/stdc++.h> #define fi fir ...

  8. css3 matrix 2D矩阵和canvas transform 2D矩阵

    一看到“2D矩阵”这个高大上的名词,有的同学可能会有种畏惧感,“矩阵”,看起来好高深的样子,我还是看点简单的吧.其实本文就很简单,你只需要有一点点css3 transform的基础就好. 没有前戏,直 ...

  9. Mac 下的 .app文件如何生成.dmg ?

    安装 Node.js最新版. 安装方法不赘述. 安装 create-dmg: sudo npm install --global create-dmg 注意这里 sudo不能少. 终端切换到 .app ...

  10. Uncaught Error: Syntax error, unrecognized expression: [flag=]报错处理方法

    今早运行项目的时候报这个错误: 百度翻译的解释是:未命名错误:语法错误,未识别表达式:[FLAG= ]   也就是书写规范问题. 可是我查了对应的js: 字符串拼接没什么问题,经常这样写. 这时看报错 ...