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/
相关文章
- QT-找开工程后,最上方提示the code model could not parse an included file, which might lead to incorrect code completion and highlighting, for example.
- 磁盘爆满导致MySQL无法启动:Disk is full writing './mysql-bin.~rec~' (Errcode: 28). Waiting for someone to free space...
- [ubuntu]E: The package firmware-upgrade needs to be reinstalled, but I can't find an archive for it.
- Navicat for MySQL无法连接到数据库怎么办
- ORA-00824: cannot set sga_target due to existing internal settings, see alert log for more information
- [Svelte 3] Use await block to wait for a promise and handle loading state in Svelte 3
- [Angular 2] ng-model and ng-for with Select and Option elements
- [AngularJS + Webpack] Using Webpack for angularjs
- Unable to determine application id: com.android.tools.idea.run.ApkProvisionException: No outputs for the main artifact of variant: debug
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
- max number of threads [1024] for user [lish] likely too low, increase to at least [2048]
- Neither BindingResult nor plain target object for bean name 'command' available as request attribute
- [React] Configure a React & Redux Application For Production Deployment and Deploy to Now
- [Node] Setup an Nginx Proxy for a Node.js App
- Cloud for Customer客户主数据重复检查duplicate check的前台实现
- Spire.Doc for Java 10.12.2 update Word to PDF/HTML to Word
- 【目标检测】9、Feature Selective Anchor-Free Module for Single-Shot Object Detection
- MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
- 已解决One or more issues caused the setup to fail.Please fix the issues and then retry setup.For more i
- A Taxonomy for Performance
- 怎么查 ODBC Driver for SQL Server
- 转:ChatGPT for Automated Testing: From Conversation to Code
- How to compute f1 score for each epoch in Keras
- NoSQLBooster for MongoDB v7.1.15 注册版
- (Mac Android Studio)Unable to connect to ADB.Check the Event Log for possible issues.Verify that you
- Execution failed for task ‘:app:processDebugResources‘. > com.android.ide.common.process.ProcessExce