https://autofaccn.readthedocs.io/en/latest/best-practices/

Autofac is designed to track and dispose of resources for you. To ensure this happens, make sure that long-running applications are partitioned into units of work (requests or transactions) and that services are resolved through unit of work level lifetime scopes. The per-request lifetime scope support in ASP.NET is an example of this technique.

https://autofaccn.readthedocs.io/en/latest/lifetime/disposal.html

Resources obtained within a unit of work - database connections, transactions, authenticated sessions, file handles etc. - should be disposed of when that work is complete. .NET provides the IDisposable interface to aid in this more deterministic notion of disposal.

Some IoC containers need to be told explicitly to dispose of a particular instance, through a method like ReleaseInstance(). This makes it very difficult to guarantee that the correct disposal semantics are used.

  • Switching implementations from a non-disposable to a disposable component can mean modifying client code.
  • Client code that may have ignored disposal when using shared instances will almost certainly fail to clean up when switched to non-shared instances.

Autofac solves these problems using lifetime scopes as a way of disposing of all of the components created during a unit of work.

  1. using (var scope = container.BeginLifetimeScope())
  2. {
  3. scope.Resolve<DisposableComponent>().DoSomething();
  4.  
  5. // Components for scope disposed here, at the
  6. // end of the 'using' statement when the scope
  7. // itself is disposed.
  8. }

A lifetime scope is created when a unit of work begins, and when that unit of work is complete the nested container can dispose all of the instances within it that are out of scope.

Autofac and IDisposable interface

Autofac calls Dispose for all instances of components implementing IDisposable once their parent lifetime scope ends. You don't need to do any additional work here.

To get familiar with options provided by Autofac for managing lifetime scopes, follow @dotnetstep's links.

Managing lifetime scopes is a strategy that depends on your specific application not only its type (MVC or plain ASP.NET or whatever). This article about lifetimes by the Autofac's creator gives a deep explanation of the topic.

As for MVC3 project, I'd recommend you follow the MVC3 integration guidelines. This will make all individual HTTP requests have separate lifetime scopes created for them. Once a HTTP request is finished, Autofac will finish the associated lifetime scope and dispose all disposable resources created in that scope.

An Autofac Lifetime Primer

https://nblumhardt.com/2011/01/an-autofac-lifetime-primer/

Autofac is designed to track and dispose of resources for you.的更多相关文章

  1. An Autofac Lifetime Primer

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

  2. Memory leak by misusing Autofac

    Recently I’ve found out that we can easily cause a memory leaks in our .net application by improper ...

  3. 实现 Dispose 方法

    实现 Dispose 方法 MSDN 类型的 Dispose 方法应释放它拥有的所有资源.它还应该通过调用其父类型的 Dispose 方法释放其基类型拥有的所有资源.该父类型的 Dispose 方法应 ...

  4. Implementing a Dispose method

    [Implementing a Dispose method] 1.实现System.IDsiposable.Dispose()方法.不能将此方法设置为virtual,子类不能override此方法. ...

  5. spark源码分析以及优化

    第一章.spark源码分析之RDD四种依赖关系 一.RDD四种依赖关系 RDD四种依赖关系,分别是 ShuffleDependency.PrunDependency.RangeDependency和O ...

  6. 自动执行任务管理---TaskManage

    这篇主要配合数据使用 先说数据库 名字都很标准---------------------------------- ------------------------------------------ ...

  7. 【源码笔记】Nop定时任务

    网站需要定时执行不同的任务,比如清理无效的数据.定时发送mail等,Nop的这个定时任务设计比较好,简单的说就是将所有任务相同的属性持久化,具体的执行通过继承接口来实现. 持久化对象:Schedule ...

  8. NopCommerce之任务执行

    NOP任务提供两种:手动执行(立即)和定时执行两种. 首先来说下手动任务执行过程,下图是NOP定时任务管理界面: 从上面可以看出,我们可以选择具体的任务来手动执行任务(立即执行),当点击[立即执行]按 ...

  9. Understanding Weak References

    Understanding Weak References Posted by enicholas on May 4, 2006 at 5:06 PM PDT Some time ago I was ...

随机推荐

  1. eclipse新建maven web工程

    每次建maven项目,总会有问题.决定在这整理一次,避免以后浪费时间. 最后目录为 1.首先修改pom.xml 之前老是出现明明改了 java compiler 已maven update 一下就又变 ...

  2. 搜索过滤grep(win下为findstr)

    搜索过滤grep(win下为findstr) 1.主要参数 [options]主要参数: -c:只输出匹配行的计数. -i:不区分大小写 -h:查询多文件时不显示文件名. -l:查询多文件时只输出包含 ...

  3. export,import ,export default的区别

    首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和importexport用于对外输出本模块(一个文件可以理解为一个模块)变量的接口i ...

  4. unity3d相关资源

    http://pan.baidu.com/s/1kTG9DVD   GUI源码

  5. macos没有任何来源怎么解决?

    打开终端,输入如下命令即可: sudo spctl --master-disable

  6. 008-CentOS添加环境变量

    在Linux CentOS系统上安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到环境变量之前,执行“php -v”命令查看当前php版本信息时时, ...

  7. 采购订单打印并预览PDF

    *&---------------------------------------------------------------------* *& Report Z01MMF019 ...

  8. Xilinx中解决高扇出的方法

    Fanout,即扇出,指模块直接调用的下级模块的个数,如果这个数值过大的话,在FPGA直接表现为net delay较大,不利于时序收敛.因此,在写代码时应尽量避免高扇出的情况.但是,在某些特殊情况下, ...

  9. Angular 学习笔记 :初识 $digest , $watch , $apply,浅析用法 。

    传统的浏览器事件循环 :浏览器本身一直在等待事件,并作出响应.如果你点击一个button或者在input 中输入字符,我们在 JS 中 监听这些事件并设定了回调函数,那么这些事件被触发以后,回调函数就 ...

  10. js判断background颜色明暗色调,以设置白/黑字体颜色

    整理自:jscolor.js插件   this.styleElement.style.color = this.isLight() ? '#000' : '#FFF';   this.isLight ...