예제 #1
0
        public async Task <object> Handle(INextHandler next, object message)
        {
            if (message is DialNumber)
            {
                var command = message as DialNumber;
                // 1) Pre-process of message
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine($"[Wiretap open for {command.Number}] ");
                Console.ResetColor();

                // 2) Send message on it's way...
                var waiting = await next.Handle(message);

                // 3) Post-process of message
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine($"[Wiretap closed for {command.Number}] ");
                Console.ResetColor();

                return(waiting);
            }
            else
            {
                return(await next.Handle(message));
            }
        }
    public async Task <object> Handle(INextHandler next, object message)
    {
        var result = await next.Handle(message);

        if (!(message is ICacheInvalidationCommand))
        {
            return(result);
        }

        var cts = new CancellationTokenSource();

#if DEBUG
        cts.CancelAfter(60 * 1000);
#else
        cts.CancelAfter(3000);
#endif

        var tasks = new List <Task>();

        var command = message as ICacheInvalidationCommand;

        foreach (var key in command.GetKeys())
        {
            tasks.Add(_cache.InvalidateAsync(key, cts.Token));
        }

        return(Task.WhenAll(tasks));
    }
예제 #3
0
        public async Task <object> Handle(INextHandler next, object message)
        {
            var result = await next.Handle(message);

            if (!(message is ICommand))
            {
                return(result);
            }

            var events = _unitOfWork.GetUncommittedEvents().ToList();

            try
            {
                await _unitOfWork.CommitAsync();
            }
            catch (Exception)
            {
                _unitOfWork.Rollback();
                throw;
            }

            foreach (var domainEvent in events)
            {
                await _mediator.PublishAsync(domainEvent);
            }

            return(result);
        }
        public async Task <object> Handle(INextHandler next, object message)
        {
            var result = await next.Handle(message);

            context.SaveChanges();

            return(result);
        }
예제 #5
0
            public Task <object> Handle(INextHandler next, object message)
            {
                if (message is IPipeTestMessage pipeMessage)
                {
                    pipeMessage.HandlerIds.Add(id);
                }

                return(next.Handle(message));
            }
예제 #6
0
 public Task <object> Handle(INextHandler next, object message)
 {
     if (detector.IsOuterPipeline)
     {
         var pipeMessage = message as IPipeTestMessage;
         pipeMessage?.HandlerIds.Add(Guid.NewGuid());
     }
     return(next.Handle(message));
 }
예제 #7
0
            public async Task <object> Handle(INextHandler next, object message)
            {
                (message as Message).AssertStage("Inner-In");

                var result = await next.Handle(message);

                (message as Message).AssertStage("Inner-Out");

                return(result);
            }
            public Task <object> Handle(INextHandler next, object message, CancellationToken cancellation)
            {
                (message as CancelableMessage)?.Tick();

                if (cancellation.IsCancellationRequested)
                {
                    return(Task.FromResult <object>(CancelStage.DelegatingHandler));
                }

                return(next.Handle(message));
            }
예제 #9
0
    public async Task <object> Handle(INextHandler next, object message)
    {
        var result = await next.Handle(message);

        var events = DomainEventTracker.GetAllEvents();

        while (events.TryDequeue(out var domainEvent))
        {
            await _mediator.SendAsync(domainEvent);
        }
        return(result);
    }
예제 #10
0
            public async Task <object> Handle(INextHandler next, object message)
            {
                var command = (Command)message;

                command.CallerId.Should().Be(1);
                command.CallerId += 1;

                var result = await next.Handle(message);

                command.CallerId.Should().Be(2);
                command.CallerId -= 1;

                return(result);
            }
예제 #11
0
    public async Task <object> Handle(INextHandler next, object message)
    {
        try
        {
            var result = await next.Handle(message);

            await PublishEventsByCommitTimeAsync(CommitTime.BeforeCommit);

            if (!(message is ICommand | message is IntegrationEvent))
            {
                return(result);
            }
            var unitOfWork = _provider.GetService <IUnitOfWork>();

            if (unitOfWork == null)
            {
                var exception = new NullReferenceException("IUnitOfWork is null on UnitOfWorkDelegatingHandler");
                _logger.LogError(exception, "IUnitOfWork is null on UnitOfWorkDelegatingHandler");
                throw exception;
            }

            try
            {
                await unitOfWork.CommitAsync();
                await PublishEventsByCommitTimeAsync(CommitTime.AfterCommit);
            }
            catch (DomainException domainException)
            {
                unitOfWork.Rollback();
                _logger.LogError(domainException, domainException.Message);
                throw;
            }
            catch (Exception exception)
            {
                unitOfWork.Rollback();
                _logger.LogError(exception, exception.Message);
                throw;
            }

            return(result);
        }
        catch (Exception e)
        {
            _logger.LogError(e, "GENERAL EXCEPTION! " + e.Message);
            throw;
        }
    }
        public async Task <object> Handle(INextHandler next, object message)
        {
            var events = _unitOfWork.GetUncommittedEvents().ToList();

            var count = events.Count;

            foreach (var domainEvent in events)
            {
                await _mediator.PublishEventAsync(domainEvent);
            }

            var result = await next.Handle(message);

            //domain event not occured
            if (count <= 0)
            {
                // command event not occured too
                if (!(message is ICommand))
                {
                    // so we dont need to Commit to db
                    return(result);
                }
            }

            try
            {
                await _unitOfWork.CommitAsync();
            }
            catch (Exception)
            {
                _unitOfWork.Rollback();
                throw;
            }

            return(result);
        }
예제 #13
0
 public Task <object> Handle(INextHandler next, object message)
 {
     return(handler.Handle(async x => await next.Handle(x), message as IMessage));
 }