Open
Description
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
Metadata
Metadata
Assignees
Labels
No labels