How do I work with per-request lifetime scope?
How do I work with per-request lifetime scope?
In applications that have a request/response semantic (e.g., ASP.NET MVC or Web API), you can register dependencies to be “instance-per-request,” meaning you will get a one instance of the given dependency for each request handled by the application and that instance will be tied to the individual request lifecycle.
In order to understand per-request lifetime, you should have a good understanding of how dependency lifetime scopes work in general. Once you understand how dependency lifetime scopes work, per-request lifetime scope is easy.
Note on ASP.NET Core
As noted in the ASP.NET Core integration docs, ASP.NET Core doesn’t have a specific per-request scope. Everything is registered InstancePerLifetimeScope()
instead of InstancePerRequest()
for ASP.NET Core.
Registering Dependencies as Per-Request
When you want a dependency registered as per-request, use the InstancePerRequest()
registration extension:
var builder = new ContainerBuilder(); builder.RegisterType<ConsoleLogger>() .As<ILogger>() .InstancePerRequest(); var container = builder.Build();
You’ll get a new instance of the component for every inbound request for your application. The handling of the creation of the request-level lifetime scope and the cleanup of that scope are generally dealt with via the Autofac application integration libraries for your application type.
这里需要详细了解一下
How Per-Request Lifetime Works
Per-request lifetime makes use of tagged lifetime scopes and the “Instance Per Matching Lifetime Scope” mechanism.
Autofac application integration libraries hook into different application types and, on an inbound request, they create a nested lifetime scope with a “tag” that identifies it as a request lifetime scope:
+--------------------------+
| Autofac Container |
| |
| +----------------------+ |
| | Tagged Request Scope | |
| +----------------------+ |
+--------------------------+
When you register a component as InstancePerRequest()
, you’re telling Autofac to look for a lifetime scope that is tagged as the request scope and to resolve the component from there. That way if you have unit-of-work lifetime scopes that take place during a single request, the per-request dependency will be shared during the request:
+----------------------------------------------------+
| Autofac Container |
| |
| +------------------------------------------------+ |
| | Tagged Request Scope | |
| | | |
| | +--------------------+ +--------------------+ | |
| | | Unit of Work Scope | | Unit of Work Scope | | |
| | +--------------------+ +--------------------+ | |
| +------------------------------------------------+ |
+----------------------------------------------------+
The request scope is tagged with a constant value Autofac.Core.Lifetime.MatchingScopeLifetimeTags.RequestLifetimeScopeTag
, which equates to the string AutofacWebRequest
. If the request lifetime scope isn’t found, you’ll get a DependencyResolutionException
that tells you the request lifetime scope isn’t found.
There are tips on troubleshooting this exception below in the Troubleshooting Per-Request Dependencies section.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-03-27 外观模式Facade pattern
2019-03-27 Forms Authentication and Role based Authorization: A Quicker, Simpler, and Correct Approach
2019-03-27 svn强制commit写log
2017-03-27 win7笔记本设置wifi热点
2016-03-27 依赖倒置