public async Task <T> Execute <T>(Func <SagaRepositoryContext <TSaga>, Task <T> > asyncMethod, CancellationToken cancellationToken = default) where T : class { var database = _databaseFactory(); var databaseContext = new RedisDatabaseContext <TSaga>(database, _options); try { var repositoryContext = new RedisSagaRepositoryContext <TSaga>(databaseContext, cancellationToken); return(await asyncMethod(repositoryContext).ConfigureAwait(false)); } finally { await databaseContext.DisposeAsync(CancellationToken.None).ConfigureAwait(false); } }
public async Task Send <T>(ConsumeContext <T> context, IPipe <SagaRepositoryContext <TSaga, T> > next) where T : class { var database = _databaseFactory(); var databaseContext = new RedisDatabaseContext <TSaga>(database, _options); try { if (_options.ConcurrencyMode == ConcurrencyMode.Pessimistic) { await databaseContext.Lock(context.CorrelationId.Value, context.CancellationToken).ConfigureAwait(false); } var repositoryContext = new RedisSagaRepositoryContext <TSaga, T>(databaseContext, context, _factory); await next.Send(repositoryContext).ConfigureAwait(false); } finally { await databaseContext.DisposeAsync(CancellationToken.None).ConfigureAwait(false); } }