Request Scope — Aquilia Documentation
Comprehensive guide and documentation for Request Scope in the Aquilia framework. View API reference, examples, and implementation patterns.
MIDDLEWARE / REQUEST DI SCOPE Request Scope DI Middleware The RequestScopeMiddleware bridges the dependency injection container and the request handler pipeline. It isolates resources per request by spawning isolated child containers and managing their teardown. Request Scoping Lifecycle For each incoming HTTP request, the middleware intercepts execution and performs these operations: 1. Container Resolution Retrieves the parent, module-level dependency container from the active RuntimeRegistry . 2. Child Scope Spawning Spawns an isolated child container by executing container.create_request_scope(). This keeps request-scoped instances isolated from other requests. 3. Instance Registration Binds the active Request instance directly to the child container using container.register_instance(Request, request, scope="request"), making it inject-ready. 4. Context Injection Stores references in request state and the handler context ctx.container = request_container. 5. Teardown & Disposal Upon response completion (inside a finally block), calls request_container.shutdown() or dispose() to release db connections and cached variables. Wiring Middleware from aquilia.middleware_ext import RequestScopeMiddleware, SimplifiedRequestScopeMiddleware # 1. Custom ASGI application level: # app.add_middleware(RequestScopeMiddleware, runtime=runtime) # 2. Simplified HTTP-level middleware setup: server.middleware(SimplifiedRequestScopeMiddleware(runtime=runtime)) Security Headers Sessions )
Go to Homepage