protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await using var scope = Services.CreateAsyncScope(); var app = new CliApplicationBuilder(); { HashSet <Type> addedCommands = new(); foreach (var module in Collection.LoadedModules) { stoppingToken.ThrowIfCancellationRequested(); var cmanifest = Collection.GetManifest(module.GetType()); foreach (var cmd in cmanifest.Commands) { stoppingToken.ThrowIfCancellationRequested(); if (addedCommands.Contains(cmd)) { continue; } app.AddCommand(cmd); addedCommands.Add(cmd); } } } var exitCode = await app .UseTypeActivator(scope.ServiceProvider.GetRequiredService) .Build() .RunAsync().ConfigureAwait(false); Environment.ExitCode = exitCode; Lifetime.StopApplication(); }