Class DispatchContextInitializer
Helper methods for creating MessageContext instances from various sources.
public static class DispatchContextInitializer
- Inheritance
-
DispatchContextInitializer
- Inherited Members
Methods
CreateDefaultContext()
Creates a minimal MessageContext using a temporary, empty service provider.
public static MessageContext CreateDefaultContext()
Returns
- MessageContext
A new MessageContext instance.
Remarks
The ambient-tenant fallback (see CreateDefaultContext(IServiceProvider)) is sourced from Current, not from the supplied service provider -- so this overload applies it exactly the same as the IServiceProvider-taking one, despite its throwaway container having nothing registered in it.
CreateDefaultContext(IServiceProvider)
Creates a MessageContext using the specified IServiceProvider.
public static MessageContext CreateDefaultContext(IServiceProvider serviceProvider)
Parameters
serviceProviderIServiceProviderService provider for resolving scoped services.
Returns
- MessageContext
A new MessageContext instance.
Remarks
Applies the ambient-tenant fallback (ApplyAmbientTenantFallback(IMessageContext)): when the calling async flow has an ambient tenant established (Current is not null), it is threaded onto the resulting context. A deployment with no ambient tenant established -- including one running single-tenant that never calls BeginScope(string?) at all -- is left untenanted; this fallback does not promote such a context to DefaultTenantId. See the fallback's own remarks for why it is deliberately not DI-registration-dependent.
CreateFromHeaders(IDictionary<string, string?>)
Creates a MessageContext from HTTP headers, reading the context fields this framework defines and ignoring the rest.
public static MessageContext CreateFromHeaders(IDictionary<string, string?> headers)
Parameters
headersIDictionary<string, string>Header dictionary containing context values.
Returns
- MessageContext
A new MessageContext.
Remarks
Only the correlation, causation, trace, user and tenant headers are read. No other header
is copied into Items. This method previously copied every
entry of the supplied dictionary, which meant a caller passing their request's headers — the use
the name invites — placed the caller's Authorization and Cookie in the context, from
where every remote transport puts them on the wire and one outbox provider persists them.
To carry an additional header beyond this process, mark it with SetPropagatedItem(IMessageContext, string, string) after this call. Setting it on Items keeps it request-local and it will not travel. Either way the decision — and its consequence — stays at the point where the value is known to be safe to forward.
Exceptions
- ArgumentNullException
Thrown when
headersis null.
CreateFromMetadata(MessageMetadata)
Creates a MessageContext directly from a strongly-typed MessageMetadata, avoiding the intermediate string dictionary the CreateFromMetadata(IDictionary<string, string?>) overload requires. Behaviourally equivalent (same identity fields + the same keys copied into Items); used on the outbox/inbox relay hot path where a per-message dictionary allocation is avoidable.
public static MessageContext CreateFromMetadata(MessageMetadata metadata)
Parameters
metadataMessageMetadataThe delivery metadata to seed the context from.
Returns
- MessageContext
A new MessageContext.
Exceptions
- ArgumentNullException
Thrown when
metadatais null.
CreateFromMetadata(IDictionary<string, string?>)
Creates a MessageContext from message metadata.
public static MessageContext CreateFromMetadata(IDictionary<string, string?> metadata)
Parameters
metadataIDictionary<string, string>Metadata dictionary containing context values.
Returns
- MessageContext
A new MessageContext.
Exceptions
- ArgumentNullException
Thrown when
metadatais null.