Request scope¶
A “request scope” is the most common unit of work for DI in web apps.
The pattern¶
Pick a scope name (commonly
"request").Register request-local services with
lifetime=Lifetime.SCOPEDandscope="request".Enter the scope for each request and resolve your handler/service graph inside it.
Example (runnable)¶
See the full script:
See the runnable script in Patterns (HTTP request handler pattern section).
Notes¶
Scoped services are reused within a single request, but not across requests.
Use generator/async-generator factories for cleanup (DB sessions, connections). See Resources (cleanup).