コード例 #1
0
        public async Task <CommandResult <TResult> > DispatchAsync <TResult>(ICommand <TResult> command, CancellationToken cancellationToken = new CancellationToken())
        {
            Stopwatch sw = Stopwatch.StartNew();
            CommandResult <TResult> result = await _underlyingDispatcher.DispatchAsync(command, cancellationToken);

            sw.Stop();
            _telemetry.RecordExecutionTime(command.GetType(), sw.ElapsedMilliseconds);
            return(result);
        }
コード例 #2
0
        public async Task <CommandResult <TResult> > DispatchAsync <TResult>(ICommand <TResult> command, CancellationToken cancellationToken = new CancellationToken())
        {
            try
            {
                CommandResult <TResult> result = await _underlyingCommandDispatcher.DispatchAsync(command, cancellationToken);

                if (result.Result == null)
                {
                    throw new RestApiException(HttpStatusCode.NotFound);
                }
                return(result);
            }
            catch (CommandModelException ex)
            {
                ModelStateDictionary modelStateDictionary = new ModelStateDictionary();
                modelStateDictionary.AddModelError(ex.Property, ex.Message);
                throw new RestApiException(HttpStatusCode.BadRequest, modelStateDictionary);
            }
        }
コード例 #3
0
        public async Task <CommandResult <TResult> > DispatchAsync <TResult>(
            ICommand <TResult> command,
            CancellationToken cancellationToken = new CancellationToken())
        {
            Guid commandId = await PersistCommand(command);

            try
            {
                var result = await _underlyingDispatcher.DispatchAsync(command, cancellationToken);

                _logger.LogInformation("Executed command {commandType} with ID {commandId}",
                                       command.GetType().Name,
                                       commandId);
                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogError("Error executing command {commandType} with ID {commandId}",
                                 command.GetType().Name,
                                 commandId);
                throw;
            }
        }
コード例 #4
0
        public async Task <CommandResult <TResult> > DispatchAsync <TResult>(ICommand <TResult> command, CancellationToken cancellationToken = new CancellationToken())
        {
            IMetricCollector metricCollector = _metricCollectorFactory.Create(command.GetType());

            try
            {
                LogPreDispatchMessage(command);
                CommandResult <TResult> result = await _underlyingDispatcher.DispatchAsync(command, cancellationToken);

                LogSuccessfulPostDispatchMessage(command);
                metricCollector.Complete();
                return(result);
            }
            catch (Exception ex)
            {
                LogFailedPostDispatchMessage(command, ex);
                metricCollector.CompleteWithError();
                throw new DispatcherException($"Error occurred performing operation {command.GetType().Name}", ex);
            }
        }
コード例 #5
0
 public async Task <CommandResult <TResult> > DispatchAsync <TResult>(ICommand <TResult> command, CancellationToken cancellationToken = new CancellationToken())
 {
     _logger.LogInformation("Dispatching command {commandType}", command.GetType().Name);
     return(await _decoratedDispatcher.DispatchAsync(command, cancellationToken));
 }
コード例 #6
0
        public async Task <CommandResult <TResult> > DispatchAsync <TResult>(ICommand <TResult> command, CancellationToken cancellationToken)
        {
            CacheOptions options = _cacheOptionsProvider.Get(command);

            if (options == null)
            {
                return(await _commandDispatcher.DispatchAsync(command, cancellationToken));
            }

            var cacheKey = CacheKey(command);

            TResult result = await _cacheAdapter.Get <TResult>(cacheKey);

            if (result != null)
            {
                return(new CommandResult <TResult>(result, false));
            }

            CommandResult <TResult> executedResult;

            if (options.Semaphore != null)
            {
                await options.Semaphore.WaitAsync(cancellationToken);

                try
                {
                    result = await _cacheAdapter.Get <TResult>(cacheKey);

                    if (result != null)
                    {
                        return(new CommandResult <TResult>(result, false));
                    }
                    else
                    {
                        executedResult = await _commandDispatcher.DispatchAsync(command, cancellationToken);
                    }
                }
                finally
                {
                    options.Semaphore.Release();
                }
            }
            else
            {
                executedResult = await _commandDispatcher.DispatchAsync(command, cancellationToken);
            }

            if (options.LifeTime != null)
            {
                await _cacheAdapter.Set(cacheKey, executedResult.Result, options.LifeTime());
            }
            else if (options.ExpiresAtUtc != null)
            {
                await _cacheAdapter.Set(cacheKey, executedResult.Result, options.ExpiresAtUtc());
            }
            else
            {
                // shouldn't happen but lets make sure we spit out a sensible error if it does
                throw new CacheConfigurationException("Either a lifetime or expiry date must be set for a cached command");
            }

            return(executedResult);
        }
コード例 #7
0
        private static async Task <bool> IsTerminated(IFrameworkCommandDispatcher commandDispatcher)
        {
            var isTerinatedCommandResult = await commandDispatcher.DispatchAsync(new IsTerminatedCommand());

            return(isTerinatedCommandResult.Result);
        }