dotnet / command-line-api

Command line parsing, invocation, and rendering of terminal output.
https://github.com/dotnet/command-line-api/wiki
MIT License
3.38k stars 381 forks source link

How to Prevent `IHost` from Starting on Command-Line #2390

Open cschuchardt88 opened 5 months ago

cschuchardt88 commented 5 months ago

How to do you prevent IHost from starting when executing a command on the command-line? It starts no matter what when using UseHost. What's the best way to allow all Command class ICommandHandler interfaces to execute without starting the middleware that runs IHostBuilder.StartAsync? But still have all the IHost objects; example IConfiguration available DI in the ICommandHandler interface?

Running as:

> myApp.exe run

Program.cs

public sealed partial class Program
{
    static async Task<int> Main(string[] args)
    {
        var rootCommand = new DefaultRootCommand();
        var parser = new CommandLineBuilder(rootCommand)
            .UseHost(DefaultNeoHostBuilderFactory, builder =>
            {
                    builder.ConfigureServices((builder, services) =>
                    {
                        services.AddSingleton<PromptSystemHostedService>();
                    });
                builder.UseCommandHandler<DefaultRootCommand, DefaultRootCommand.Handler>();
            })
            .UseDefaults()
            .Build();

        return await parser.InvokeAsync(args);
    }
}

DefaultRootCommand.cs

internal sealed class DefaultRootCommand: Command
{
    public RunCommand() : base("run")
    {
        IsHidden = true;
    }

    public new sealed class Handler : ICommandHandler
    {
        public Handler(IConfiguration config) // want this from IHost
        {
        }

        public async Task<int> InvokeAsync(InvocationContext context)
        {
            var host = context.GetHost();
            // What code do I write to prevent 'host' from StartAsync

            return await RunConsolePrompt(ref context);
        }

        public int Invoke(InvocationContext context)
        {
            throw new NotImplementedException();
        }

        public static Task<int> RunConsolePrompt(ref InvocationContext context)
        {
            while (true)
            {
                lock (context.Console.Out)
                {
                    context.Console.Clear();
                    context.Console.SetTerminalForegroundColor(ConsoleColor.DarkBlue);

                    context.Console.Write("Prompt> ");
                    var line = context.Console.ReadLine();

                    context.Console.ResetTerminalForegroundColor();

                    return await PromptCommandsInvokeAsync(line);
                }
            }
        }
    }
}

Console Output

Prompt>
[2024-04-21 15:00:32.317] dbug: Microsoft.Extensions.Hosting.Internal.Host[3] Hosting stopping
[2024-04-21 15:00:32.317] dbug: Microsoft.Extensions.Hosting.Internal.Host[4] Hosting stopped
fredrikhr commented 2 months ago

I have started work on PR #2450 which proposes some refactoring to the Hosting library. In that PR, if you use Hosted Services for all commands the you would not need to call UseHost anymore, and thus you could have some commands that use the IHost and some that do not.