Interface IRequestContext
- Namespace
- SimpleMediator
- Assembly
- SimpleMediator.dll
Carries ambient context metadata through the mediator pipeline.
public interface IRequestContext
Examples
// Creating context
var context = RequestContext.Create()
.WithUserId("user-123")
.WithTenantId("tenant-abc")
.WithIdempotencyKey("idempotency-xyz");
// Accessing in behaviors
public async ValueTask<Either<MediatorError, TResponse>> Handle(
TRequest request,
IRequestContext context,
RequestHandlerCallback<TResponse> nextStep,
CancellationToken cancellationToken)
{
_logger.LogInformation(
"Handling {Request} for user {UserId} (correlation: {CorrelationId})",
typeof(TRequest).Name,
context.UserId,
context.CorrelationId);
return await nextStep();
}
Remarks
Context is created once per request and flows through all behaviors, processors, and handlers. It provides access to cross-cutting concerns like correlation IDs, user identity, idempotency keys, and tenant information without polluting request types.
Immutable design: use With* methods to create modified copies.
Common Use Cases:
- Distributed tracing with correlation IDs
- Multi-tenant applications (tenant isolation)
- Idempotency checking (duplicate request detection)
- User context propagation (audit logs, authorization)
- Custom metadata for extensibility
Properties
CorrelationId
Correlation ID for distributed tracing.
string CorrelationId { get; }
Property Value
Remarks
Always present - auto-generated from Current or Guid if not provided. Use this for linking logs, traces, and metrics across services.
IdempotencyKey
Idempotency key for duplicate detection.
string? IdempotencyKey { get; }
Property Value
Remarks
null if idempotency is not applicable for this request.
Used by idempotency behaviors to prevent duplicate processing of the same logical request.
Typically extracted from HTTP headers (e.g., Idempotency-Key).
Metadata
Custom metadata for extensibility.
IReadOnlyDictionary<string, object?> Metadata { get; }
Property Value
Remarks
Allows behaviors and processors to attach additional context that doesn't fit standard properties. Use WithMetadata(string, object?) to add entries.
TenantId
Tenant ID for multi-tenant applications.
string? TenantId { get; }
Property Value
Remarks
null if the application is not multi-tenant or tenant cannot be determined.
Used for data isolation and tenant-specific logic.
Timestamp
Request timestamp (UTC).
DateTimeOffset Timestamp { get; }
Property Value
Remarks
Captured when the context is created, represents the start of request processing. Useful for time-based logic, audit trails, and latency measurements.
UserId
User ID initiating the request.
string? UserId { get; }
Property Value
Remarks
null if the request is unauthenticated.
Typically extracted from claims principal in ASP.NET Core applications.
Methods
WithIdempotencyKey(string?)
Creates a new context with updated idempotency key.
IRequestContext WithIdempotencyKey(string? idempotencyKey)
Parameters
idempotencyKeystringIdempotency key to set.
Returns
- IRequestContext
New context instance with the idempotency key updated.
Remarks
Follows immutable pattern - original context is not modified. Useful in pre-processors that extract idempotency headers.
WithMetadata(string, object?)
Creates a new context with additional metadata.
IRequestContext WithMetadata(string key, object? value)
Parameters
Returns
- IRequestContext
New context instance with the metadata added.
Remarks
Follows immutable pattern - original context is not modified.
WithTenantId(string?)
Creates a new context with updated tenant ID.
IRequestContext WithTenantId(string? tenantId)
Parameters
tenantIdstringTenant ID to set.
Returns
- IRequestContext
New context instance with the tenant ID updated.
Remarks
Follows immutable pattern - original context is not modified. Useful in pre-processors that determine tenant from request data or claims.
WithUserId(string?)
Creates a new context with updated user ID.
IRequestContext WithUserId(string? userId)
Parameters
userIdstringUser ID to set.
Returns
- IRequestContext
New context instance with the user ID updated.
Remarks
Follows immutable pattern - original context is not modified. Useful in pre-processors that extract user identity.