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

No UseHost(this CommandLineBuilder builder, Func<string[], HostApplicationBuilder> appBuilderFactory, Action<HostApplicationBuilder> configureAppHost) overload #2097

Open
fourpastmidnight opened this issue Mar 15, 2023 · 2 comments

Comments

@fourpastmidnight
Copy link

Apparently, a relatively recent decision was made to change how application host construction is done. The "old" way was a series of chained method calls on classes that implement the IHostBuilder interface. Apparently, there were issues with that, and so a complete cut was made—a new HostApplicationBuilder class was created which does not implement IHostBuilder.

The current suggested documentation for hosting a Windows Service in .NET Core (https://learn.microsoft.com/en-us-dotnet/core/extensions/windows-service) recommends using something like this:

var hostBuilder = Host.CreateApplicationBuilder();
hostBuilder.Services.AddWindowsService();
hostBuilder.Services.AddSingleton<YourService>();
hostBuilder.Services.AddHostedService<WindowsBackgroundService>();

var host = hostBuilder.Build();
host.Run()

So, translating this into System.CommandLineBuilder, I tried:

var rootCommand = new RootCommand("The root command.");
var commandLineBuilder = new CommandLineBuilder(rootCommand)
    .UseDefaults()
    .UseHost(Host.CreateApplicationBuilder /* BOOM: Not an IHostBuilder!! */, hostBuilder => {
        // Configure your host here...
    })
    .Build();

But as you can see, with the new "recommended" pattern, this fails because there is no overload of UseHost() that accepts an HostApplicationBuilder builder.

@fourpastmidnight
Copy link
Author

A PR will be forthcoming. I have most of the code written, I need to add a few more overloads to cover all the functionality, and then see if there are any specific tests that need to be added for these overloads.

@fredrikhr
Copy link
Contributor

I have started work on PR #2450 where I propose new overloads to UseHost that accept HostApplicationBuilder. This will however require adding the net8.0 TFM to System.CommandLine.Hosting, but we'll have to see whether this is an approach we'd like to go for.

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

No branches or pull requests

2 participants