Class MessageContext
High-performance implementation of IMessageContext optimized for zero-allocation scenarios. Uses lazy ID generation and direct field storage for the 7 core properties. Cross-cutting concerns (identity, routing, processing state, etc.) are accessed via the Features dictionary.
public class MessageContext : IMessageContext
- Inheritance
-
MessageContext
- Implements
- Inherited Members
- Extension Methods
Constructors
MessageContext()
Parameterless constructor for object pooling.
public MessageContext()
MessageContext(IDispatchMessage, IServiceProvider)
High-performance implementation of IMessageContext optimized for zero-allocation scenarios. Uses lazy ID generation and direct field storage for the 7 core properties. Cross-cutting concerns (identity, routing, processing state, etc.) are accessed via the Features dictionary.
public MessageContext(IDispatchMessage message, IServiceProvider requestServices)
Parameters
messageIDispatchMessageThe message being processed.
requestServicesIServiceProviderThe service provider for dependency resolution during message processing.
Properties
AuthorizationResult
Gets or sets the authorization result for the message.
public IAuthorizationResult AuthorizationResult { get; set; }
Property Value
CausationId
Gets or sets the causation identifier linking this message to its cause.
public string? CausationId { get; set; }
Property Value
Remarks
PERF-6: When lazy-enabled, defaults to CorrelationId on first access.
CorrelationId
Gets or sets the correlation identifier for tracking related messages.
public string? CorrelationId { get; set; }
Property Value
Remarks
PERF-6: Uses lazy generation. UUID7 string is only generated on first access.
Features
Gets the typed feature collection for accessing cross-cutting concerns.
public IDictionary<Type, object> Features { get; }
Property Value
- IDictionary<Type, object>
The features dictionary keyed by feature interface type.
Remarks
Features provide typed access to processing state, identity, routing, validation, timeout, rate limiting, and transaction contexts. Use extension methods from MessageContextFeatureExtensions for convenient access:
var processing = context.GetOrCreateProcessingFeature();
processing.ProcessingAttempts++;
Contract an implementation must keep — the same one Items states, and now load-bearing here too. Returns the one collection owned by this context. Never null. The same instance on every call, for the life of the context. Mutable, and a write is observable to every subsequent read. Returning a fresh collection per call satisfies the signature and breaks this framework silently: the propagated set (SetPropagatedItem(IMessageContext, string, string)) is stored here, so a context that forgets a feature write loses every marked value with nothing raised and nothing logged.
Items
Gets this context's request-local scratch collection.
public IDictionary<string, object> Items { get; }
Property Value
- IDictionary<string, object>
The request-local items collection owned by this message context.
Remarks
Request-local. Nothing in this framework serializes, persists, transmits or emits these entries. To carry a value beyond this process, mark it with SetPropagatedItem(IMessageContext, string, string) instead. The two collections are separate for the reason Activity keeps Tags and Baggage apart: whether a write crosses a trust boundary must be decided by the call, never by the spelling of a key.
Contract an implementation must keep. Returns the one collection owned by this context. Never null. The same instance on every call, for the life of the context. Mutable, and a write is observable to every subsequent read. Returning a fresh collection per call satisfies the signature and breaks this framework silently — several components write an entry through one call and read it back through another.
Message
Gets or sets the message being processed.
public IDispatchMessage? Message { get; set; }
Property Value
- IDispatchMessage
The message instance or null.
MessageId
Gets or sets the unique identifier for this message instance.
public string? MessageId { get; set; }
Property Value
- string
The unique identifier assigned to the message.
Remarks
PERF-5: Uses lazy string conversion. If MessageId is never set and never accessed, no string allocation occurs.
Metadata
Gets or sets the message metadata.
public IMessageMetadata? Metadata { get; set; }
Property Value
RequestServices
Gets or sets the scoped service provider for this message processing context.
public IServiceProvider RequestServices { get; set; }
Property Value
- IServiceProvider
The scoped service provider for this context.
Result
Gets or sets the result of processing the message.
public object? Result { get; set; }
Property Value
Success
Gets a value indicating whether all processing steps were successful.
public bool Success { get; }
Property Value
Remarks
Both _validationResult and _authorizationResult are volatile
fields. This getter reads them without acquiring a lock; its latency depends on the workload
and runtime. The two reads do not constitute an atomic snapshot of both results.
ValidationResult
Gets or sets the validation result for the message.
public IValidationResult ValidationResult { get; set; }
Property Value
VersionMetadata
Gets or sets the version metadata for the message.
public IMessageVersionMetadata VersionMetadata { get; set; }
Property Value
Methods
CreateForDeserialization(IServiceProvider)
Creates a MessageContext for deserialization scenarios where no actual message is available.
public static MessageContext CreateForDeserialization(IServiceProvider serviceProvider)
Parameters
serviceProviderIServiceProvider
Returns
Initialize(IServiceProvider)
Initializes the context with a service provider after being retrieved from the pool.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Initialize(IServiceProvider requestServices)
Parameters
requestServicesIServiceProvider
Reset()
High-performance reset that reuses cached instances to eliminate allocations.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset()