Skip to main content
Table of Contents

Interface IMessageBus

Namespace
Stratara.Abstractions.Messaging
Assembly
Stratara.Abstractions.dll

Provider-agnostic pub/sub abstraction over the underlying message bus (RabbitMQ in dev, Azure Service Bus in prod). Topic + subscription names follow IMessagingIdentifier.

public interface IMessageBus
Extension Methods

Methods

EnsureSubscriptionAsync(string, string, CancellationToken)

Establish subscription on topic so that messages published from this point on are retained for it, without dispatching any of them yet.

Task EnsureSubscriptionAsync(string topic, string subscription, CancellationToken cancellationToken = default)

Parameters

topic string

The topic the subscription belongs to.

subscription string

The subscription to establish.

cancellationToken CancellationToken

Cancels the operation.

Returns

Task

A task that completes once the subscription exists.

Remarks

Call this during start-up, before anything in the system can publish. A subscription that is established only when its handler attaches receives nothing published in the meantime, and on a topic carrying more than one subscription that loss is silent: the publisher is told the publication succeeded as soon as any subscription takes it, so a missing one is indistinguishable from a delivered one.

Idempotent — establishing a subscription that already exists changes nothing and loses nothing already held for it. SubscribeAsync<T>(string, string, Func<T, Task>, CancellationToken) establishes the subscription too, so a caller that only ever subscribes stays correct; this member exists so that establishing can happen earlier than the handler is ready.

An implementation whose subscriptions exist before the application runs — one where they are provisioned administratively — satisfies this by doing nothing. An implementation that cannot establish a particular subscription ahead of its consumer SHOULD throw rather than return successfully, because a caller cannot otherwise tell that the guarantee it asked for is absent.

PublishAsync<T>(string, T, CancellationToken)

Publish message to topic.

Task PublishAsync<T>(string topic, T message, CancellationToken cancellationToken = default)

Parameters

topic string
message T
cancellationToken CancellationToken

Returns

Task

Type Parameters

T

SubscribeAsync<T>(string, string, Func<T, Task>, CancellationToken)

Subscribe to topic under subscription and dispatch every incoming message to handler. Establishes the subscription if EnsureSubscriptionAsync(string, string, CancellationToken) has not already done so.

Task SubscribeAsync<T>(string topic, string subscription, Func<T, Task> handler, CancellationToken cancellationToken = default)

Parameters

topic string
subscription string
handler Func<T, Task>
cancellationToken CancellationToken

Returns

Task

Type Parameters

T