Skip to content

[2.0.0-beta6.25358.103] .UseHost(…) makes using asynchronous actions mandatory #2648

@springcomp

Description

@springcomp

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, Action1 configureHost)
at System.CommandLine.Hosting.HostingAction.SetHandlers(Command command, Func2 hostBuilderFactory, Action1 configureHost)
at System.CommandLine.Hosting.HostingExtensions.UseHost(CommandLineConfiguration config, Func2 hostBuilderFactory, Action1 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

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

    Issue actions