Search code examples
masstransitsaga

Masstransit: Respond after TransitionTo


I am new with Masstransit and looking for an answer. How to respond right after all activities completed and saga stage was persited?

Is it possible?

For example.

In Controller:

private readonly IRequestClient<CreateDataRequest> client; 
private readonly TestDbContext db; // dbContext from saga

[HttpPost]
public async Task<IActionResult> CreateData(CreateData model)
{
        var id = Guid.NewGuid();
        var response = await client.GetResponse<CreateDataResponse>(new CreateDataRequest(id, 
model.UserId));

        var state= await db.Data.FirstOrDefaultAsync(s => s.CorrelationId == id);

        return Ok(state);
}

In StateMachine:

private EventActivityBinder<ApplicationState, CreateDataRequest> CreateDataReceived

Initially(
    When(CreateDataReceived)
        .Then(s =>
        {
            s.Saga.UserId = s.Message.UserId;
        })
// I tried like this, but sometimes is controler I get null
// .Respond(x => new CreateDataResponse(x.Message.CorrelationId))
        .Request(GetAdditionalData, c => new GetAdditionalDataRequest(c.Message.CorrelationId))
        .TransitionTo(GetAdditionalData.Pending)
// here I want to respond CreateDataResponse after CurrentSatge=GetData.Pending persisted
)

Solution

  • You can enable the in-memory outbox (UseInMemoryOutbox()) on the endpoint and messages will not be sent until the saga state has been persisted.