You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
internalsealedclassDefaultRootCommand:Command{publicRunCommand():base("run"){IsHidden=true;}publicnewsealedclassHandler:ICommandHandler{publicHandler(IConfigurationconfig)// want this from IHost{}publicasyncTask<int>InvokeAsync(InvocationContextcontext){varhost=context.GetHost();// What code do I write to prevent 'host' from StartAsyncreturnawaitRunConsolePrompt(refcontext);}publicintInvoke(InvocationContextcontext){thrownewNotImplementedException();}publicstaticTask<int>RunConsolePrompt(refInvocationContextcontext){while(true){lock(context.Console.Out){context.Console.Clear();context.Console.SetTerminalForegroundColor(ConsoleColor.DarkBlue);context.Console.Write("Prompt> ");varline=context.Console.ReadLine();context.Console.ResetTerminalForegroundColor();returnawaitPromptCommandsInvokeAsync(line);}}}}}
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.
How to do you prevent
IHost
from starting when executing a command on the command-line? It starts no matter what when usingUseHost
. What's the best way to allow allCommand
classICommandHandler
interfaces to execute without starting the middleware that runsIHostBuilder.StartAsync
? But still have all theIHost
objects; exampleIConfiguration
availableDI
in theICommandHandler
interface?Running as:
> myApp.exe run
Program.cs
DefaultRootCommand.cs
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
The text was updated successfully, but these errors were encountered: