You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(docs): improve lifecycle management and DI guides
- Updated lifecycle management guide to include `ILambdaLifecycleContext` usage and DI patterns.
- Added examples for direct DI, AWS metadata access, state sharing, and keyed services in OnInit handlers.
- Enhanced dependency injection guide with source-generated DI support for OnInit and OnShutdown.
- Expanded core concepts with lifecycle context access patterns and practical usage scenarios.
Copy file name to clipboardExpand all lines: docs/getting-started/core-concepts.md
+11Lines changed: 11 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -85,6 +85,17 @@ lambda.OnShutdown(async (
85
85
});
86
86
```
87
87
88
+
### Lifecycle Context Access
89
+
90
+
Both OnInit and OnShutdown handlers support rich dependency injection patterns:
91
+
92
+
-**Inject services directly**: `(ICache cache, ILogger logger, CancellationToken ct)` – Recommended for most scenarios
93
+
-**Access AWS metadata**: `(ILambdaLifecycleContext context)` – When you need environment information like function name, region, memory size, or initialization type
94
+
-**Combine both**: `(ILambdaLifecycleContext context, ICache cache)` – Mix context access with direct service injection
95
+
-**Use keyed services**: `([FromKeyedServices("key")] IService service)` – Resolve services registered with specific keys
96
+
97
+
The `ILambdaLifecycleContext` interface provides AWS environment metadata, a `Properties` dictionary for sharing state between handlers, and access to the scoped `ServiceProvider`. See [Lifecycle Management](../guides/lifecycle-management.md) for detailed patterns and examples.
Copy file name to clipboardExpand all lines: docs/guides/lifecycle-management.md
+96-6Lines changed: 96 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -132,22 +132,112 @@ Both handlers are awaited simultaneously. Keep shutdown work small—only the re
132
132
133
133
OnInit and OnShutdown handlers support the same source-generated dependency injection experience as middleware and handlers:
134
134
135
-
- Request only what you need. The generated delegate resolves typed parameters (services, keyed services, `ILambdaHostContext`, `CancellationToken`, etc.).
135
+
- Request only what you need. The generated delegate resolves typed parameters (services, keyed services, `ILambdaLifecycleContext`, `CancellationToken`, etc.).
136
136
-`MinimalLambda` creates a new `IServiceScope` for every handler invocation, ensuring scoped services (database units of work, caches) are isolated even though you are outside the invocation pipeline.
137
-
- The `IServiceProvider` parameter gives you direct access to the scope for manual resolution when needed.
137
+
- Inject `ILambdaLifecycleContext` when you need AWS environment metadata or want to share state via the `Properties` dictionary. For simple scenarios, inject services directly.
When you need AWS environment metadata during initialization or shutdown, inject `ILambdaLifecycleContext`. This interface provides rich context information about the Lambda execution environment beyond what's available in the standard invocation pipeline.
if (context.Properties.TryGetValue("InitStartTime", outvarstartTime))
199
+
{
200
+
logger.LogInformation("First handler started at {Time}", startTime);
201
+
}
202
+
203
+
returntrue;
204
+
});
205
+
```
206
+
207
+
The `Properties` dictionary is backed by a thread-safe `ConcurrentDictionary<string, object?>` and is shared across all OnInit handlers. Properties set during OnInit are also available to invocation handlers via `ILambdaHostContext.Properties`.
208
+
209
+
### Available Context Properties
210
+
211
+
`ILambdaLifecycleContext` exposes the following properties:
212
+
213
+
| Property | Type | Description |
214
+
|----------|------|-------------|
215
+
|`CancellationToken`|`CancellationToken`| Signals cancellation when `InitTimeout` (OnInit) or `ShutdownDuration - ShutdownDurationBuffer` (OnShutdown) elapses, or when SIGTERM is received |
216
+
|`ServiceProvider`|`IServiceProvider`| The scoped service container for this handler invocation. Use for manual service resolution when direct injection isn't sufficient |
217
+
|`Properties`|`IDictionary<string, object?>`| Thread-safe dictionary for sharing data between handlers or from OnInit to invocation handlers |
218
+
|`ElapsedTime`|`TimeSpan`| Time elapsed since the Lambda execution environment started |
219
+
|`Region`|`string?`| AWS region where the function is running (from `AWS_REGION` env var) |
220
+
|`ExecutionEnvironment`|`string?`| Runtime identifier like `AWS_Lambda_dotnet8` (from `AWS_EXECUTION_ENV` env var) |
221
+
|`FunctionName`|`string?`| Name of the Lambda function (from `AWS_LAMBDA_FUNCTION_NAME` env var) |
222
+
|`FunctionMemorySize`|`int?`| Memory allocated to the function in MB (from `AWS_LAMBDA_FUNCTION_MEMORY_SIZE` env var) |
223
+
|`FunctionVersion`|`string?`| Version of the function being executed (from `AWS_LAMBDA_FUNCTION_VERSION` env var) |
224
+
|`InitializationType`|`string?`| Type of initialization: `on-demand`, `provisioned-concurrency`, `snap-start`, or `lambda-managed-instances` (from `AWS_LAMBDA_INITIALIZATION_TYPE` env var) |
225
+
|`LogGroupName`|`string?`| CloudWatch Logs group name (from `AWS_LAMBDA_LOG_GROUP_NAME` env var, not available in SnapStart) |
226
+
|`LogStreamName`|`string?`| CloudWatch Logs stream name (from `AWS_LAMBDA_LOG_STREAM_NAME` env var, not available in SnapStart) |
227
+
|`TaskRoot`|`string?`| Path to the Lambda function code (from `LAMBDA_TASK_ROOT` env var) |
228
+
229
+
All AWS environment properties are nullable because they depend on environment variables set by the Lambda runtime. Most are available during normal execution, but some (like `LogGroupName` and `LogStreamName`) are unavailable in SnapStart functions.
230
+
231
+
### When to Use ILambdaLifecycleContext
232
+
233
+
Use `ILambdaLifecycleContext` when you need:
234
+
235
+
-**AWS environment metadata** – Access region, function name, memory size, or initialization type for logging, metrics, or conditional logic based on the execution environment
236
+
-**State sharing** – Pass data between OnInit handlers or from OnInit to invocation handlers via the `Properties` dictionary
237
+
-**Manual service resolution** – Access `ServiceProvider` directly for advanced scenarios where parameter injection isn't sufficient
238
+
239
+
For simple dependency injection scenarios, prefer injecting services directly as parameters instead of using `ILambdaLifecycleContext.ServiceProvider`.
240
+
151
241
## Configuring Lifecycle Behavior
152
242
153
243
Use `ConfigureLambdaHostOptions` to shape lifecycle behavior centrally or bind the same settings from configuration:
0 commit comments