Skip to content

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

@cschuchardt88

Description

@cschuchardt88

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

Activity

fredrikhr

fredrikhr commented on Jul 7, 2024

@fredrikhr
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @cschuchardt88@fredrikhr

        Issue actions

          How to Prevent `IHost` from Starting on Command-Line · Issue #2390 · dotnet/command-line-api