public async Task Run(CancellationToken cancellationToken) { var tasks = _container .ImplementationsFor <ICommandLineTask>() .Concat(_container.ImplementationsForGenericType(typeof(ICommandLineTask <>))) .Where(t => t != GetType()) .ToArray(); while (!cancellationToken.IsCancellationRequested) { for (var index = 1; index < tasks.Length + 1; index++) { var taskType = tasks[index - 1]; _logger.Information("{0}. {1}", index, taskType.Name); } _logger.Information("Enter a number of a task or empty to exit"); var readLine = Console.ReadLine(); if (string.IsNullOrEmpty(readLine)) { return; } int taskIndex; try { taskIndex = readLine.To <int>() - 1; if (tasks.Length <= taskIndex || taskIndex < 0) { _logger.Error("Invalid task number"); continue; } } catch (FormatException e) { _logger.Error(e, ""); continue; } var type = tasks[taskIndex]; using (var scope = _container.BeginLifetimeScope("task")) { var task = scope.Resolve(type); try { await RunTask(cancellationToken, task); _logger.Information("Succesfully ran task {task}", task); } catch (TaskCanceledException) { _logger.Warning("Task canceled by user {task}", task); } catch (OperationCanceledException) { _logger.Warning("Operation canceled by user {task}", task); } catch (Exception ex) { _logger.Error(ex, "Failed to execute task {task}", task); } } Console.WriteLine(); } }