Publishing messages

A message can be published by calling the Publish() method directly on the Saga instance:

public class MyAwesomeSaga :
    Saga,
    IStartedBy<StartMyAwesomeSaga>
{
    public async ValueTask HandleAsync(IMessageContext<StartMyAwesomeSaga> context, CancellationToken cancellationToken = default)
    {
        var message = new MyAwesomeSagaCompleted(Guid.NewGuid(), context.Message.CorrelationId);
        this.Publish(message);
    }
}

OpenSleigh uses the Outbox pattern to ensure messages are properly published and the Saga State is persisted.

Publish-only applications

An application can be also configured as "publish-only": it will only take care of dispatching new messages but won't be able to consume any. Useful when creating a Web API that offloads the actual execution to a separate worker service.

services.AddOpenSleigh(cfg =>
{
    // code omitted
    cfg.SetPublishOnly();
});

Last updated