Autofac is designed to track and dispose of resources for you.
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.的更多相关文章
- An Autofac Lifetime Primer
Or, “Avoiding Memory Leaks in Managed Composition” Understanding lifetime can be pretty tough when y ...
- Memory leak by misusing Autofac
Recently I’ve found out that we can easily cause a memory leaks in our .net application by improper ...
- 实现 Dispose 方法
实现 Dispose 方法 MSDN 类型的 Dispose 方法应释放它拥有的所有资源.它还应该通过调用其父类型的 Dispose 方法释放其基类型拥有的所有资源.该父类型的 Dispose 方法应 ...
- Implementing a Dispose method
[Implementing a Dispose method] 1.实现System.IDsiposable.Dispose()方法.不能将此方法设置为virtual,子类不能override此方法. ...
- spark源码分析以及优化
第一章.spark源码分析之RDD四种依赖关系 一.RDD四种依赖关系 RDD四种依赖关系,分别是 ShuffleDependency.PrunDependency.RangeDependency和O ...
- 自动执行任务管理---TaskManage
这篇主要配合数据使用 先说数据库 名字都很标准---------------------------------- ------------------------------------------ ...
- 【源码笔记】Nop定时任务
网站需要定时执行不同的任务,比如清理无效的数据.定时发送mail等,Nop的这个定时任务设计比较好,简单的说就是将所有任务相同的属性持久化,具体的执行通过继承接口来实现. 持久化对象:Schedule ...
- NopCommerce之任务执行
NOP任务提供两种:手动执行(立即)和定时执行两种. 首先来说下手动任务执行过程,下图是NOP定时任务管理界面: 从上面可以看出,我们可以选择具体的任务来手动执行任务(立即执行),当点击[立即执行]按 ...
- Understanding Weak References
Understanding Weak References Posted by enicholas on May 4, 2006 at 5:06 PM PDT Some time ago I was ...
随机推荐
- 再论IBatisNet + Castle进行项目的开发
随着项目的进展,Castle和IBatisNet给我的惊喜更多.Com+很重,不需要分布式的中小项目慎用,NHibernate虽好,NHibernate的2005-9-20发布了最新版本1.0-rc1 ...
- java动态编译 (java在线执行代码后端实现原理)(二)
在上一篇java动态编译 (java在线执行代码后端实现原理(一))文章中实现了 字符串编译成字节码,然后通过反射来运行代码的demo.这一篇文章提供一个如何防止死循环的代码占用cpu的问题. 思路: ...
- CentOS下搭建LNMP+WordPress+http2.0教程
此文是本人CentOS下搭建WordPress的一些笔记,环境搭建时间::将看过的几篇文章总结下来,形成一条龙长文.不用大家再找来找去. 本文大概分为此几部分: 一.基础命令更新: 二.服务器加速(非 ...
- [LintCode] 二叉树的前序遍历
The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and ...
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 4)
(六)高效合理的使用LINQ 1.DataContext中的两个属性 为了能够使用DataContext进行数据提交,在DataContext进行数据查询和操作的过程中,内部会进行数据状态的保持和追踪 ...
- matplotlib绘制圆饼图
import matplotlib.pyplot as plt labels = ['Nokia','Samsung','Apple','Lumia'] values = [10,30,45,15] ...
- 2015-03-22——js常用的String方法
String string.charAt(pos); //返回字符串中pos位置处的字符.如果pos小于0或大于等于string.length返回空字符串.模拟实现:Function.prototy ...
- 总结! http post请求 application/x-www-form-urlencoded body体数据获取不到?
首先,简单介绍下Http请求中Content-Type类型 类型格式:type/subtype(;parameter)? type 主类型,任意的字符串,如text,如果是*号代表所有: subtyp ...
- Diango思维图
1,http 2,Django生命周期 3,Django部分命令 4,待续...
- 用仿ActionScript的语法来编写html5——第五篇,Graphics绘图
用仿ActionScript的语法来编写html5——第五篇,Graphics绘图 canvas本身就是一个Graphics,可以直接进行绘图在actionscript里面,每个Sprite都有一个G ...