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.

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

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. 《从零开始学Swift》学习笔记(Day 58)—— Swift编码规范之变量或常量声明规范

    原创文章,欢迎转载.转载请注明:关东升的博客 声明是在声明变量.常量.属性.方法或函数和自定义类型时候需要遵守的规范. 首先变量或常量时每行声明变量或常量的数量推荐一行一个,因为这样以利于写注释.示例 ...

  2. java的static final和final的区别

    转自:https://www.cnblogs.com/EasonJim/p/7841990.html 说明:不一定准确,但是最快理解. final: final可以修饰:属性,方法,类,局部变量(方法 ...

  3. 洛谷 P1641 [SCOI2010]生成字符串

    洛谷 这题一看就是卡塔兰数. 因为\(cnt[1] \leq cnt[0]\),很显然的卡塔兰嘛! 平时我们推导卡塔兰是用一个边长为n的正方形推的, 相当于从(0,0)点走到(n,n)点,向上走的步数 ...

  4. 我的Android进阶之旅------>Android通用流行框架大全

    Android通用流行框架大全 缓存 图片加载 图片处理 网络请求 网络解析 数据库 依赖注入 图表 后台处理 事件总线 响应式编程 Log框架 测试框架 调试框架 性能优化 本文转载于lavor的博 ...

  5. Hessian矩阵与多元函数极值

    Hessian矩阵与多元函数极值 海塞矩阵(Hessian Matrix),又译作海森矩阵,是一个多元函数的二阶偏导数构成的方阵.虽然它是一个具有悠久历史的数学成果.可是在机器学习和图像处理(比如SI ...

  6. 从微观到宏观,遍历网络安全这幅有向图——By Me

    “可视化”是网络安全领域的前沿技术与可靠保障.笔者所在的西电捷通是一家领先的网络安全基础技术国际研究机构.一直从事网络安全基础技术研发与技术转移.笔者在2011年底入职典型技术之上的西电捷通公司,那时 ...

  7. jQuery.extend()、jQuery.fn.extend()扩展方法具体解释

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/dreamsunday/article/details/25193459 jQuery自己定义了jQu ...

  8. oracle 禁用索引

    同步数据的时候 有索引会比较慢 可以暂时禁用索引 --禁用索引 ALTER INDEX PK_T_AUTH_USERROLE_ID UNUSABLE; --恢复索引ALTER INDEX UK_T_A ...

  9. Python(文件处理)

    二.基本操作 #r''------------------>> r:原生字符串,不判断符号的含义#文件处理 f=open(r’c:\a.txt’,’r’,encoding=’utf-8’) ...

  10. SqlServer分页总结-摘抄

    sqlserver2008不支持关键字limit ,所以它的分页sql查询语句将不能用mysql的方式进行,幸好sqlserver2008提供了top,rownumber等关键字,这样就能通过这几个关 ...