示例#1
0
        public async Task OrchestrateAsync(NewUserEvent message)
        {
            using (_logger.BeginScope(Guid.NewGuid().ToString()))
            {
                try
                {
                    await _cacheFactory.ConnectAsync();

                    await _databaseFactory.OpenConnectionAsync();

                    _databaseFactory.BeginTransaction();

                    await ProcessAsync(message);

                    _databaseFactory.CommitTransaction();
                }
                catch (Exception ex)
                {
                    _logger.LogCritical($"HOST | CRITICAL ERROR: {ex}");

                    _databaseFactory.RollbackTransaction();

                    throw;
                }
                finally
                {
                    await _cacheFactory.DisconnectAsync();

                    _databaseFactory.CloseConnection();
                }
            }
        }
        protected override Task ExecuteAsync(CancellationToken cancellationToken)
        {
            var channel  = _messagingFactory.Configure();
            var consumer = new AsyncEventingBasicConsumer(channel);

            consumer.Received += _messagingService.Dequeue(async(raw, message) =>
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (message == null)
                {
                    return;
                }

                using (_logger.BeginScope(Guid.NewGuid().ToString()))
                {
                    try
                    {
                        await _databaseFactory.OpenConnectionAsync();

                        await _databaseFactory.BeginTransactionAsync();

                        await _orchestratorService.OrchestrateAsync(message);

                        _databaseFactory.CommitTransaction();
                    }
                    catch (Exception ex)
                    {
                        _databaseFactory.RollbackTransaction();

                        throw ex;
                    }
                    finally
                    {
                        _databaseFactory.CloseConnection();
                    }
                }
            });

            _tag = channel.BasicConsume(_messaging.Consuming.Queue, false, consumer);

            return(Task.CompletedTask);
        }
        public async Task Invoke(HttpContext context, IDatabaseFactory databaseFactory)
        {
            await databaseFactory.OpenConnectionAsync();

            await databaseFactory.BeginTransactionAsync();

            try
            {
                await _next(context);

                databaseFactory.CommitTransaction();
            }
            catch
            {
                databaseFactory.RollbackTransaction();

                throw;
            }
            finally
            {
                databaseFactory.CloseConnection();
            }
        }