Class MessageEnvelope<TMessage>
- Namespace
- DotCompute.Abstractions.Messaging
- Assembly
- DotCompute.Abstractions.dll
Wraps a message with TTL metadata without requiring the message to implement IExpirableMessage.
public sealed class MessageEnvelope<TMessage> : IExpirableMessage, IRingKernelMessage, IDisposable where TMessage : IRingKernelMessage
Type Parameters
TMessageThe message type.
- Inheritance
-
MessageEnvelope<TMessage>
- Implements
- Inherited Members
- Extension Methods
Remarks
Use this wrapper to add TTL semantics to messages that don't natively support it.
Constructors
MessageEnvelope(TMessage, TimeSpan?)
Creates a new message envelope.
public MessageEnvelope(TMessage message, TimeSpan? timeToLive = null)
Parameters
messageTMessageThe message to wrap.
timeToLiveTimeSpan?Optional TTL override. Default: 30 seconds.
Properties
CorrelationId
Gets or sets the optional correlation identifier for request/response patterns.
public Guid? CorrelationId { get; set; }
Property Value
- Guid?
Remarks
Used to correlate responses with their originating requests. Set to the MessageId of the request message when sending a response.
Null for standalone messages that don't require correlation.
CreatedAt
Gets or sets the time when this message was created.
public DateTimeOffset CreatedAt { get; set; }
Property Value
Remarks
Used in conjunction with TimeToLive to determine when the message expires. Default: UtcNow
ExpiresAt
Gets the absolute expiration time of this message.
public DateTimeOffset? ExpiresAt { get; }
Property Value
IsExpired
Gets a value indicating whether the message has expired.
public bool IsExpired { get; }
Property Value
Message
Gets the wrapped message.
public TMessage Message { get; }
Property Value
- TMessage
MessageId
Gets or sets the unique message identifier.
public Guid MessageId { get; set; }
Property Value
Remarks
Used for message tracking, logging, and deduplication. Should be unique across all messages in the system. Default: NewGuid()
MessageType
Gets the message type identifier for deserialization.
public string MessageType { get; }
Property Value
Remarks
Used by the runtime to determine which concrete type to instantiate during deserialization. Must be consistent across all instances of the same message type.
Typically returns the type name (e.g., "ComputeRequest").
PayloadSize
Gets the message payload size in bytes.
public int PayloadSize { get; }
Property Value
Remarks
Returns the total size of the serialized message including:
- MessageId (16 bytes)
- Priority (1 byte)
- CorrelationId (16 bytes if present, 1 byte null marker otherwise)
- Application data (variable)
Used for memory allocation and capacity planning. Source generator calculates this automatically.
Priority
Gets or sets the message priority (0-255).
public byte Priority { get; set; }
Property Value
Remarks
Higher values indicate higher priority. Used by priority queues to determine processing order.
- 0-63: Low priority (batch processing)
- 64-127: Normal priority (default)
- 128-191: High priority (interactive)
- 192-255: Critical priority (system messages)
Default: 128 (normal priority)
TimeToLive
Gets or sets the message's time-to-live duration.
public TimeSpan TimeToLive { get; set; }
Property Value
Remarks
The message expires when CreatedAt + TimeToLive is less than the current time.
Set to InfiniteTimeSpan for no expiration. Default: 30 seconds
Methods
Deserialize(ReadOnlySpan<byte>)
Deserializes the message from a byte span.
public void Deserialize(ReadOnlySpan<byte> data)
Parameters
dataReadOnlySpan<byte>The serialized message bytes.
Remarks
Reads data using BinaryPrimitives with explicit endianness (little-endian).
The data span must be at least PayloadSize bytes.
Auto-generated by source generator for optimal performance.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Serialize()
Serializes the message to a byte span.
public ReadOnlySpan<byte> Serialize()
Returns
- ReadOnlySpan<byte>
A read-only span containing the serialized message bytes.
Remarks
Uses BinaryPrimitives for efficient serialization with explicit endianness (little-endian).
The returned span is valid only until the next call to Serialize(). Callers should copy the data if longer retention is needed.
Auto-generated by source generator for optimal performance.