private void DispatchCommandWithoutErrorHandling(ICommandInput commandInput, ICommand cmd)
        {
            if (EnableTrace)
            {
                Trace.Indent();
                Tracer.WriteCommandExecution(commandInput);
            }

            using (var context = _commandContextFactory.CreateContext(commandInput))
            {
                cmd.Execute(context);
            }

            if (EnableTrace)
            {
                Trace.Unindent();
            }
        }
示例#2
0
        protected override async Task ExecuteAsync(
            CancellationToken stoppingToken)
        {
            _logger.LogWarning(
                "Starting LineReaderService. Console output may appear " +
                "garbled.");

            // HACK: Since the command store has no way of getting *all*
            // registered commands, we have to do this currently
            var commandCount = ((List <ICommand>)_commandStore.GetType()
                                .GetField("_currentCommands",
                                          BindingFlags.NonPublic | BindingFlags.Instance) !
                                .GetValue(_commandStore) !)
                               .Count;

            _logger.LogInformation("Command store contains {count} commands",
                                   commandCount);

            // N.B. since Console.ReadLine() can't be cancelled or done
            // asynchronously, we forcibly yield here to ensure other services
            // can be started.
            await Task.Yield();

            while (true)
            {
                stoppingToken.ThrowIfCancellationRequested();

                var command = Console.ReadLine();
                _logger.LogDebug("Command: {command}", command);

                if (command != null)
                {
                    var context = _commandContextFactory.CreateContext();

                    try
                    {
                        _commandParser.Parse(context, command);
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e,
                                         "Failed to parse {command}",
                                         command);

                        continue;
                    }

                    foreach (var paramPair in context.Parameters)
                    {
                        _logger.LogInformation(
                            "Parameter {key} ({type}) = {value}",
                            paramPair.Key,
                            paramPair.Value?.GetType() ?? typeof(object),
                            paramPair.Value);
                    }

                    await _commandExecutor.ExecuteAsync(context,
                                                        stoppingToken);
                }
            }
        }