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

Refactor Hosting extensions #2450

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

fredrikhr
Copy link
Contributor

@fredrikhr fredrikhr commented Jul 7, 2024

  • Remove InvocationLifetime
    Originally, System.CommandLine.Hosting was designed to depend only on Microsoft.Extensions.Hosting.Abstractions which made the InvocationLifetime class necessary. Currently however, the library depends on the full Microsoft.Extensions.Hosting library, so InvocationLifetime is no longer required as the functionality of the ConsoleLifetime class fully (and more extensively) covers its functionality.
    We should however provide guidance that you want set the process termination timeout of the CliConfiguration to null when using System.CommandLine.Hosting (the UseHost extension method does that as part of its implementation).
  • Introduce HostedServiceAction, a CliAction that using a .NET Generic Host hosted service as the execution for a CLI action.
    • Originally, one of the issues with using a Hosted Service as the Action for a CLI Command was that the .NET Generic Host does not automatically shutdown when the hosted service completes its execution task (that is by design, as Hosted services generally run in the background for the entire liftetime of the application).
      HostedServiceAction will automatically shut down the .NET Generic Host when the Hosted Service completes its work, i.e. the command finishes.
    • Add CliHostedService, a simple derivison from BackgroundService, which provides a CliAsyncAction-similar abstract InvokeAsync method to override which also allows returning back an integer result value. HostedServiceAction will use the return value from CliHostedService to return the result integer for the invocation.
      This class also avoids confusion regarding the name BackgroundService as the main handler for a CLI Command.
  • Rename HostingAction to HostingWrapperAction
    • Create a new base class HostingAction and make both HostingWrapperAction and HostedServiceAction derive from it.
    • Move the logic of creating the Host and starting it part of the base class, the HostingWrapperAction simply wraps the underlying action, while HostedServiceAction simply deals with waiting for the hosted service to complete.
  • Prepare extension points for using the modern generic host builder introduced in .NET 8.
    .NET 8 introduces the HostApplicationBuilder class as an alternative to the HostBuilder and IHostBuilder interfaces. Annoyingly, the HostApplicationBuilder does not implement the IHostBuilder interface necessary for the original implementation of the System.CommandLine.Hosting extensions.
    • Add an internal very limited wrapper around HostApplicationBuilder that (partially) implements IHostBuilder.
    • Add a generic type argument to the HostingAction base class that supports multiple different Host builders.
    • Adding this functionality will require a new TFM for the System.CommandLine.Hosting library. Current proposed implementation uses #ifdef set up for multi-targeting.
  • Add a publically settable ConfigureHost property on the HostingAction base class.
    • Since the new HostedServiceAction is publically exposed and constructable is is useful to be able to allow for per-command separate ConfigureHost methods.
    • UseHost will now combine its configureHost argument with already existing ConfigureHost delegates that have previously been set on HostingActions set for Cli commands in the CLI configuration.
  • Add a new UseHost extension method for AsynchronousCliAction instances that allows for per-command specific Host configuration actions
  • Provide more examples for a complex multi-command set-up in HostedPlayground
    • Show how multiple HostedServiceActions can have their individual command-specific ConfigureHost delegates with a shared whole app ConfigureHost delegate.
    • Show differences between using just a regular CommandHandler with a method with hosting vs. using a CliHostedService.
  • Add tests for new introduced features in this PR
    • Tests for combining ConfigureHost from UseHost and HostingAction
    • Tests for using HostApplicationBuilder
    • Tests for HostedServiceAction

@fredrikhr
Copy link
Contributor Author

#2390 shows that there might be a use-case for an UseHost extension method on BindingHandler that would allow to create per-command Host configurations instead of the current UseHost extension that is applied for the entire configuration.

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

Successfully merging this pull request may close these issues.

1 participant