I have a Saga that processes batch work. My solution is based on the article referenced in the documentation. Thus I wanted to mitigate optimistic concurrency errors. But unfortunately, even though I don’t update saga data when batch completed event is handled, saga data is persisted anyway. This leads to numbers of concurrency errors when many workers finished. I use SQL Persistence.
The code looks like:
public class InvoiceAndReportSaga : Saga<InvoiceAndReportSagaData>,
IAmStartedByMessages<InboundShipmentUpdated>,
IHandleMessages<SyncSkusWithItemMasterComplete>
public async Task Handle(ShipmentProcessingCompleted message, IMessageHandlerContext context)
{
foreach (SeasonalProduct seasonalProduct in seasonalProducts)
{
await context.Send(new SyncSkusWithItemMaster
{
SagaCorrelationId = this.Data.SagaCorrelationId
}).ConfigureAwait(false);
}
}
public Task Handle(SyncSkusWithItemMasterComplete message, IMessageHandlerContext context)
{
return Task.CompletedTask;
}
In general, I would recommend to not elide the async/await keyword unless you are on a hot path. When going with the async/await keywords you avoid introducing subtle bugs like not awaiting a task. Eliding the keyword in the business code is most of the time not worth the risk you are exposing yourself.