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

Enable support for building services with IHostApplicationBuilder #57

Open
pedropaulovc opened this issue Mar 19, 2024 · 1 comment · May be fixed by #58
Open

Enable support for building services with IHostApplicationBuilder #57

pedropaulovc opened this issue Mar 19, 2024 · 1 comment · May be fixed by #58

Comments

@pedropaulovc
Copy link

pedropaulovc commented Mar 19, 2024

.NET 6 brought new builder types inheriting from IHostApplicationBuilder that refactor how to developers may build web / console hosts with fewer callbacks: Comparing WebApplicationBuilder to the Generic Host

WebApplicationBuilder : IHostApplicationBuilder
HostApplicationBuilder : IHostApplicationBuilder

The old IHostBuilder pattern remains supported. A comparision of how both initializations can work is:

IHostBuilder

IHostApplicationBuilder

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

builder.Configuration.AddUserSecrets<Program>();

builder.Services.Configure<TaskHubOptions>(opt =>
{
   opt.CreateIfNotExists = true;
});
builder.Services.AddSingleton<IConsole, ConsoleWrapper>();
builder.Services.AddHostedService<TaskEnqueuer>();
builder.Services.AddSingleton(UseLocalEmulator());

builder.ConfigureTaskHubWorker((context, builder) =>
{
   builder.AddClient();
   builder.UseOrchestrationMiddleware<SampleMiddleware>();
   builder.UseActivityMiddleware<SampleMiddleware>();

   builder
       .AddOrchestration<GreetingsOrchestration>()
       .AddOrchestration<GenericOrchestrationRunner>();

   builder.AddActivitiesFromAssembly<Program>();
});

IHost host = builder.Build();

return host.RunAsync();
IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration(builder => builder.AddUserSecrets<Program>())
    .ConfigureServices(services =>
    {
        services.Configure<TaskHubOptions>(opt =>
        {
            opt.CreateIfNotExists = true;
        });
        services.AddSingleton<IConsole, ConsoleWrapper>();
        services.AddHostedService<TaskEnqueuer>();
        services.AddSingleton(UseLocalEmulator());
    })
    .ConfigureTaskHubWorker((context, builder) =>
    {
        builder.AddClient();
        builder.UseOrchestrationMiddleware<SampleMiddleware>();
        builder.UseActivityMiddleware<SampleMiddleware>();

        builder
            .AddOrchestration<GreetingsOrchestration>()
            .AddOrchestration<GenericOrchestrationRunner>();

        builder.AddActivitiesFromAssembly<Program>();
    })
    .UseConsoleLifetime()
    .Build();

return host.RunAsync();
@pedropaulovc pedropaulovc linked a pull request Mar 19, 2024 that will close this issue
2 tasks
@jviau
Copy link
Owner

jviau commented Mar 22, 2024

@pedropaulovc thank you for opening this issue, and I agree that not being compatible with the IHostApplicationBuilder model is not ideal. After thinking on this, I think my preferred approach would be to move to work off exclusively IServiceCollection and no longer rely on IHostBuilder or IHostApplicationBuilder at all - this will entirely avoid the need to upgrade the Microsoft.Extensions.* packages.

If you want to take on that work it would be greatly appreciated, as I am not sure when I get around to that.

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 a pull request may close this issue.

2 participants