durabletask-hosting icon indicating copy to clipboard operation
durabletask-hosting copied to clipboard

Enable support for building services with IHostApplicationBuilder

Open pedropaulovc opened this issue 11 months ago • 1 comments

.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 avatar Mar 19 '24 19:03 pedropaulovc

@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.

jviau avatar Mar 22 '24 16:03 jviau