Example #1
0
        public async Task ProcessCommand <TCommand>(TCommand command) where TCommand : IAsyncCommand
        {
            var eventType = command.GetType().GetTypeInfo()
                            .ImplementedInterfaces
                            .FirstOrDefault(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == typeof(IAsyncCommandEvent <>));

            if (eventType != null)
            {
                eventType = eventType.GetTypeInfo().GenericTypeArguments.First();
            }

            if (eventType == null)
            {
                await _innerAsyncCommandBus.ProcessCommand(command);

                return;
            }
            try
            {
                await _innerAsyncCommandBus.ProcessCommand(command);

                var method = typeof(ICommandEvents).GetTypeInfo().GetDeclaredMethod("NotifyComplete");
                method = method.MakeGenericMethod(eventType);
                method.Invoke(_commandEvents, new object[] { command });
            }
            catch (Exception ex)
            {
                var method = typeof(ICommandEvents).GetTypeInfo().GetDeclaredMethod("NotifyError");
                method = method.MakeGenericMethod(eventType);
                method.Invoke(_commandEvents, new object[] { command, ex });
                throw;
            }
        }
        public Task ProcessCommand <TCommand>(TCommand command) where TCommand : IAsyncCommand
        {
            IScheduler scheduler;

            if (Schedulers.TryGetValue(typeof(TCommand), out scheduler))
            {
                return(scheduler.Run(() => _commandBus.ProcessCommand(command)));
            }
            if (DefaultScheduler != null)
            {
                return(DefaultScheduler.Run(() => _commandBus.ProcessCommand(command)));
            }
            return(_commandBus.ProcessCommand(command));
        }
Example #3
0
        public async Task ProcessCommand <TCommand>(TCommand command) where TCommand : IAsyncCommand
        {
            try
            {
                lock (this)
                {
                    _executingCommands.Add(command);
                }
                _commandStarted.OnNext(command);
                await _commandBus.ProcessCommand(command);

                _commandEnded.OnNext(command);
            }
            catch (Exception ex)
            {
                _commandError.OnNext(new Tuple <IAsyncCommand, Exception>(command, ex));
                throw;
            }
            finally
            {
                lock (this)
                {
                    _executingCommands.Remove(command);
                }
            }
        }
        private async Task ProcessCommandInternal <TCommand>(TCommand command) where TCommand : IAsyncCommand
        {
            SemaphoreSlim semaphoreSlim;

            if (_commandTypes.TryGetValue(typeof(TCommand), out semaphoreSlim))
            {
                await semaphoreSlim.WaitAsync(command.CancellationToken);

                try
                {
                    await _innerCommandBus.ProcessCommand(command);
                }
                finally
                {
                    semaphoreSlim.Release();
                }
            }
            else
            {
                await _innerCommandBus.ProcessCommand(command);
            }
        }
        public async Task ProcessCommand <TCommand>(TCommand command) where TCommand : IAsyncCommand
        {
            var stopwatch = Stopwatch.StartNew();

            try
            {
                await _innerCommandBus.ProcessCommand(command);
            }
            finally
            {
                stopwatch.Stop();
                Debug.WriteLine("Command {1} time : {0}", stopwatch.Elapsed, typeof(TCommand));
            }
        }
Example #6
0
        public async Task ProcessCommand <TCommand>(TCommand command) where TCommand : IAsyncCommand
        {
            Debug.WriteLine("[COMMAND] [START] {0}", command);
            try
            {
                await _innerCommandBus.ProcessCommand(command);

                Debug.WriteLine("[COMMAND] [END]   {0}", command);
            }
            catch (Exception ex)
            {
                //prevent writing an exception multiple times
                Exception existing;
                if (!_catchedExceptions.TryGetValue(ex, out existing))
                {
                    _catchedExceptions.Add(ex, ex);
                    Debug.WriteLine("[COMMAND] [ERROR] {0}\n{1}", command, ex);
                }
                throw;
            }
        }