Handling Messages

Starting a Saga

In order to start a Saga, we need to tell OpenSleigh which message type can be used as "initiator". To do that, we need to add the IStartedBy<> interface to the Saga and implement it:

public class MyAwesomeSaga :
    Saga,
    IStartedBy<StartMyAwesomeSaga>
{
    public async ValueTask HandleAsync(IMessageContext<StartMyAwesomeSaga> context, CancellationToken cancellationToken = default)
    {
        _logger.LogInformation($"starting saga '{context.Message.CorrelationId}'...");
    }
}

Messages are simple POCO classes (or records), implementing the IMessage interface:

public record StartMyAwesomeSaga() : IMessage { }

Handling messages

In order to handle more message types, it is necessary to add and implement the IHandleMessage<> interface on the Saga:

public class MyAwesomeSaga :
    Saga,
    IStartedBy<StartMyAwesomeSaga>,
    IHandleMessage<MyAwesomeSagaCompleted>,
{
    // code omitted for brevity

    public async ValueTask HandleAsync(IMessageContext<MyAwesomeSagaCompleted> context, CancellationToken cancellationToken = default)
    {
        _logger.LogInformation($"saga '{context.Message.CorrelationId}' completed!");
    }
}

Stopping a Saga

A Saga can be marked as completed by calling the MarkAsCompleted() on its state:

This step is completely optional but signals Opensleigh to stop sending messages to that Saga instance.

Last updated

Was this helpful?