Beispiel #1
0
        public async Task Send(ConsumeContext <TMessage> context, IPipe <ConsumeContext <TMessage> > next)
        {
            try
            {
                await _transactionProvider.BeginTransactionAsync();

                await next.Send(context);

                await _transactionProvider.CommitTransactionAsync();
            }
            catch (Exception ex)
            {
                //TODO: Logging
                Console.WriteLine(ex.ToString());
                await _transactionProvider.RollbackTransactionAsync();

                throw;
            }
        }
Beispiel #2
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
        {
            if (_transactionalBehaviorValidator.IsCommand <TRequest>())
            {
                await _transactionProvider.BeginTransactionAsync();

                try
                {
                    var response = await next();

                    if (response is IResult result)
                    {
                        if (result.IsSuccess)
                        {
                            await _transactionProvider.CommitTransactionAsync();
                        }
                        else
                        {
                            await _transactionProvider.RollbackTransactionAsync();
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException("The transaction status for the command could not be determined");
                    }

                    return(response);
                }
                catch
                {
                    await _transactionProvider.RollbackTransactionAsync();

                    //TODO: Logging
                    throw;
                }
            }

            return(await next());
        }