Table of Contents

Class MessageHeaders

Namespace
PatternKit.Messaging
Assembly
PatternKit.Core.dll

Immutable message metadata for in-process messaging and enterprise integration patterns.

public sealed class MessageHeaders : IReadOnlyDictionary<string, object?>, IReadOnlyCollection<KeyValuePair<string, object?>>, IEnumerable<KeyValuePair<string, object?>>, IEnumerable
Inheritance
MessageHeaders
Implements
Inherited Members
Extension Methods

Remarks

Header names are compared with OrdinalIgnoreCase so transport-style names such as Correlation-Id and correlation-id resolve to the same value.

Constructors

MessageHeaders()

Creates an empty header collection.

public MessageHeaders()

MessageHeaders(IEnumerable<KeyValuePair<string, object?>>)

Creates a header collection from existing values.

public MessageHeaders(IEnumerable<KeyValuePair<string, object?>> values)

Parameters

values IEnumerable<KeyValuePair<string, object>>

The values to copy into the immutable collection.

Exceptions

ArgumentNullException

Thrown when values is null.

ArgumentException

Thrown when a header name is null, empty, or whitespace.

Properties

CausationId

Gets the well-known causation identifier header when present.

public string? CausationId { get; }

Property Value

string

ContentType

Gets the well-known content type header when present.

public string? ContentType { get; }

Property Value

string

CorrelationId

Gets the well-known correlation identifier header when present.

public string? CorrelationId { get; }

Property Value

string

Count

Gets the number of elements in the collection.

public int Count { get; }

Property Value

int

The number of elements in the collection.

Empty

An empty immutable header collection.

public static MessageHeaders Empty { get; }

Property Value

MessageHeaders

IdempotencyKey

Gets the well-known idempotency key header when present.

public string? IdempotencyKey { get; }

Property Value

string

this[string]

Gets the element that has the specified key in the read-only dictionary.

public object? this[string key] { get; }

Parameters

key string

The key to locate.

Property Value

object

The element that has the specified key in the read-only dictionary.

Exceptions

ArgumentNullException

key is null.

KeyNotFoundException

The property is retrieved and key is not found.

Keys

Gets an enumerable collection that contains the keys in the read-only dictionary.

public IEnumerable<string> Keys { get; }

Property Value

IEnumerable<string>

An enumerable collection that contains the keys in the read-only dictionary.

MessageId

Gets the well-known message identifier header when present.

public string? MessageId { get; }

Property Value

string

ReplyTo

Gets the well-known reply address header when present.

public string? ReplyTo { get; }

Property Value

string

Timestamp

Gets the well-known timestamp header when present and parseable.

public DateTimeOffset? Timestamp { get; }

Property Value

DateTimeOffset?

Values

Gets an enumerable collection that contains the values in the read-only dictionary.

public IEnumerable<object?> Values { get; }

Property Value

IEnumerable<object>

An enumerable collection that contains the values in the read-only dictionary.

Methods

ContainsKey(string)

Determines whether the read-only dictionary contains an element that has the specified key.

public bool ContainsKey(string key)

Parameters

key string

The key to locate.

Returns

bool

true if the read-only dictionary contains an element that has the specified key; otherwise, false.

Exceptions

ArgumentNullException

key is null.

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<string, object>>

An enumerator that can be used to iterate through the collection.

GetString(string)

Gets a string header value when present. Non-string values are converted with ToString().

public string? GetString(string name)

Parameters

name string

Returns

string

TryGetDateTimeOffset(string, out DateTimeOffset)

Attempts to read a header as a DateTimeOffset or parse a string header.

public bool TryGetDateTimeOffset(string name, out DateTimeOffset value)

Parameters

name string
value DateTimeOffset

Returns

bool

TryGetGuid(string, out Guid)

Attempts to read a header as a Guid or parse a string header as a Guid.

public bool TryGetGuid(string name, out Guid value)

Parameters

name string
value Guid

Returns

bool

TryGetValue(string, out object?)

Gets the value that is associated with the specified key.

public bool TryGetValue(string key, out object? value)

Parameters

key string

The key to locate.

value object

When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

Returns

bool

true if the object that implements the IReadOnlyDictionary<TKey, TValue> interface contains an element that has the specified key; otherwise, false.

Exceptions

ArgumentNullException

key is null.

TryGet<T>(string, out T?)

Attempts to read a header as T.

public bool TryGet<T>(string name, out T? value)

Parameters

name string
value T

Returns

bool

Type Parameters

T

With(string, object?)

Returns a new header collection with name set to value.

public MessageHeaders With(string name, object? value)

Parameters

name string
value object

Returns

MessageHeaders

WithCausationId(string)

Returns a new collection with CausationId set.

public MessageHeaders WithCausationId(string causationId)

Parameters

causationId string

Returns

MessageHeaders

WithContentType(string)

Returns a new collection with ContentType set.

public MessageHeaders WithContentType(string contentType)

Parameters

contentType string

Returns

MessageHeaders

WithCorrelationId(string)

Returns a new collection with CorrelationId set.

public MessageHeaders WithCorrelationId(string correlationId)

Parameters

correlationId string

Returns

MessageHeaders

WithIdempotencyKey(string)

Returns a new collection with IdempotencyKey set.

public MessageHeaders WithIdempotencyKey(string idempotencyKey)

Parameters

idempotencyKey string

Returns

MessageHeaders

WithMessageId(string)

Returns a new collection with MessageId set.

public MessageHeaders WithMessageId(string messageId)

Parameters

messageId string

Returns

MessageHeaders

WithReplyTo(string)

Returns a new collection with ReplyTo set.

public MessageHeaders WithReplyTo(string replyTo)

Parameters

replyTo string

Returns

MessageHeaders

WithTimestamp(DateTimeOffset)

Returns a new collection with Timestamp set.

public MessageHeaders WithTimestamp(DateTimeOffset timestamp)

Parameters

timestamp DateTimeOffset

Returns

MessageHeaders

Without(string)

Returns a new header collection without name.

public MessageHeaders Without(string name)

Parameters

name string

Returns

MessageHeaders