Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cschuchardt88 opened this issue Apr 21, 2024 · 0 comments
Open

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

cschuchardt88 opened this issue Apr 21, 2024 · 0 comments

Comments

@cschuchardt88
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant