-
Notifications
You must be signed in to change notification settings - Fork 403
Description
I have the following program which works fine:
public sealed class Dependency
{
public string GetName() => "Dependency Injection Example";
public static int Main(string[] args)
{
var helloCommand = new Command("hello");
helloCommand.SetAction((parseResult) => { System.Console.WriteLine("greetings!"); });
var rootCommand = new RootCommand();
rootCommand.Subcommands.Add(helloCommand);
var configuration = new CommandLineConfiguration(rootCommand)
//.UseHost((hostBuilder) =>
//{
// hostBuilder.ConfigureServices((context, services) =>
// {
// services.AddSingleton<Dependency>();
// });
//})
;
return configuration.Invoke(args);
}
}
You can see that the .UseHost(…)
call is commented out.
In that case, the action that is associated with the helloCommand
is typed AnonymousSynchronousCommandLineAction
.
If you un-comment the .UseHost(…)
call, the program crashes:
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.CommandLine.Invocation.AnonymousSynchronousCommandLineAction' to type 'System.CommandLine.Invocation.AsynchronousCommandLineAction'.
at System.CommandLine.Hosting.HostingAction.SetHandlers(Command command, Func2 hostBuilderFactory, Action
1 configureHost)
at System.CommandLine.Hosting.HostingAction.SetHandlers(Command command, Func2 hostBuilderFactory, Action
1 configureHost)
at System.CommandLine.Hosting.HostingExtensions.UseHost(CommandLineConfiguration config, Func2 hostBuilderFactory, Action
1 configureHost)
at System.CommandLine.Hosting.HostingExtensions.UseHost(CommandLineConfiguration config, Action`1 configureHost)
at Dependency.Main(String[] args) in C:\Projects\springcomp\apim\cli\src\apim\Runner.cs:line 52