コード例 #1
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken,
                                             RequestHandlerDelegate <TResponse> next)
        {
            TResponse response = default;

            try
            {
                await _dbContext.RetryOnExceptionAsync(async() =>
                {
                    _logger.LogInformation($"Begin transaction {typeof(TRequest).Name}");

                    await _dbContext.BeginTransactionAsync();

                    response = await next();

                    await _dbContext.CommitTransactionAsync();

                    _logger.LogInformation($"Committed transaction {typeof(TRequest).Name}");
                });

                return(response);
            }
            catch (Exception e)
            {
                _logger.LogInformation($"Rollback transaction executed {typeof(TRequest).Name}");

                _dbContext.RollbackTransaction();

                _logger.LogError(e.Message, e.StackTrace);

                throw e;
            }
        }
コード例 #2
0
        private static async Task InterceptAsync(Task task, IDbContext dbContext)
        {
            try
            {
                await task.ConfigureAwait(false);

                await dbContext.CommitTransactionAsync();
            }
            catch (Exception)
            {
                dbContext.RollbackTransaction();
                throw;
            }
        }
コード例 #3
0
        private static async Task <T> InterceptWithResultAsync <T>(Task <T> task, IDbContext dbContext)
        {
            try
            {
                var result = await task.ConfigureAwait(false);

                if (result is Result returnValue && returnValue.Failed)
                {
                    dbContext.RollbackTransaction();
                }
                else
                {
                    await dbContext.CommitTransactionAsync();
                }

                return(result);
            }
コード例 #4
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
        {
            var response = default(TResponse);
            var typeName = request.GetGenericTypeName();

            try
            {
                if (dbContext.HasActiveTransaction)
                {
                    return(await next());
                }

                await dbContext.Execute(async() =>
                {
                    using var transaction = await dbContext.BeginTransactionAsync();
                    var transactionId     = dbContext.CurrentTransactionId;

                    using (LogContext.PushProperty("TransactionContext", transactionId))
                    {
                        logger.LogInformation("----- Begin transaction {TransactionId} for {CommandName} ({@Command})", transactionId, typeName, request);

                        response = await next();

                        logger.LogInformation("----- Commit transaction {TransactionId} for {CommandName}", transactionId, typeName);

                        await dbContext.CommitTransactionAsync(transaction);

                        await salesOrderIntegrationEventService.PublishEventsThroughEventBusAsync(transactionId);
                    }
                });

                return(response);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "ERROR Handling transaction for {CommandName} ({@Command})", typeName, request);

                throw;
            }
        }
コード例 #5
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            try
            {
                await _dbContext.BeginTransactionAsync();

                var actionExecuted = await next();

                if (actionExecuted.Exception != null && !actionExecuted.ExceptionHandled)
                {
                    _dbContext.RollbackTransaction();
                }
                else
                {
                    await _dbContext.CommitTransactionAsync();
                }
            }
            catch (Exception)
            {
                _dbContext.RollbackTransaction();
                throw;
            }
        }