From 3398757154932bcb24490610d8c98376332d4510 Mon Sep 17 00:00:00 2001 From: "Pedro Paulo Vezza Campos (from Dev Box)" Date: Tue, 19 Mar 2024 12:01:58 -0700 Subject: [PATCH 1/2] Enable IHostApplicationBuilder --- .../DurableTask.Extensions.Samples/Program.cs | 35 +++--- .../Program.cs | 33 +++--- samples/DurableTask.Samples/Program.cs | 52 ++++----- .../TaskHubHostBuilderExtensions.cs | 78 +++++++++++++ .../TaskHubHostBuilderExtensionsTests.cs | 106 +++++++++++++++--- 5 files changed, 229 insertions(+), 75 deletions(-) diff --git a/samples/DurableTask.Extensions.Samples/Program.cs b/samples/DurableTask.Extensions.Samples/Program.cs index c351141..be147c1 100644 --- a/samples/DurableTask.Extensions.Samples/Program.cs +++ b/samples/DurableTask.Extensions.Samples/Program.cs @@ -10,24 +10,23 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -IHost host = Host.CreateDefaultBuilder(args) - .ConfigureServices(services => - { - // Can register DataConvert in service container, or in options below. - // services.AddSingleton(new StjDataConverter()); - services.AddSingleton(); - services.AddHostedService(); - }) - .ConfigureTaskHubWorker((context, builder) => - { - builder.WithOrchestrationService(new LocalOrchestrationService()); - builder.AddDurableExtensions(opt => opt.DataConverter = new StjDataConverter()); - builder.AddClient(); - builder.AddOrchestrationsFromAssembly(includePrivate: true); - builder.AddActivitiesFromAssembly(includePrivate: true); - }) - .UseConsoleLifetime() - .Build(); +HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); + +// Can register DataConvert in service container, or in options below. +// builder.Services.AddSingleton(new StjDataConverter()); +builder.Services.AddSingleton(); +builder.Services.AddHostedService(); + +builder.ConfigureTaskHubWorker((context, builder) => +{ + builder.WithOrchestrationService(new LocalOrchestrationService()); + builder.AddDurableExtensions(opt => opt.DataConverter = new StjDataConverter()); + builder.AddClient(); + builder.AddOrchestrationsFromAssembly(includePrivate: true); + builder.AddActivitiesFromAssembly(includePrivate: true); +}); + +IHost host = builder.Build(); await host.RunAsync(); diff --git a/samples/DurableTask.Instrumentation.Samples/Program.cs b/samples/DurableTask.Instrumentation.Samples/Program.cs index 3b4c444..c315a43 100644 --- a/samples/DurableTask.Instrumentation.Samples/Program.cs +++ b/samples/DurableTask.Instrumentation.Samples/Program.cs @@ -22,23 +22,22 @@ .AddZipkinExporter() .Build(); -IHost host = Host.CreateDefaultBuilder(args) - .ConfigureServices(services => - { - services.AddSingleton(); - services.AddHostedService(); - }) - .ConfigureTaskHubWorker((context, builder) => - { - builder.WithOrchestrationService(GetOrchestrationService()); - builder.AddDurableExtensions(); - builder.AddDurableInstrumentation(); - builder.AddClient(); - builder.AddOrchestrationsFromAssembly(includePrivate: true); - builder.AddActivitiesFromAssembly(includePrivate: true); - }) - .UseConsoleLifetime() - .Build(); +HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); + +builder.Services.AddSingleton(); +builder.Services.AddHostedService(); + +builder.ConfigureTaskHubWorker((context, builder) => +{ + builder.WithOrchestrationService(GetOrchestrationService()); + builder.AddDurableExtensions(); + builder.AddDurableInstrumentation(); + builder.AddClient(); + builder.AddOrchestrationsFromAssembly(includePrivate: true); + builder.AddActivitiesFromAssembly(includePrivate: true); +}); + +IHost host = builder.Build(); await host.RunAsync(); diff --git a/samples/DurableTask.Samples/Program.cs b/samples/DurableTask.Samples/Program.cs index 65504cb..a3ba379 100644 --- a/samples/DurableTask.Samples/Program.cs +++ b/samples/DurableTask.Samples/Program.cs @@ -26,32 +26,32 @@ public class Program /// A task that completes when this program is finished running. public static Task Main(string[] args) { - IHost host = Host.CreateDefaultBuilder(args) - .ConfigureAppConfiguration(builder => builder.AddUserSecrets()) - .ConfigureServices(services => - { - services.Configure(opt => - { - opt.CreateIfNotExists = true; - }); - services.AddSingleton(); - services.AddHostedService(); - services.AddSingleton(UseLocalEmulator()); - }) - .ConfigureTaskHubWorker((context, builder) => - { - builder.AddClient(); - builder.UseOrchestrationMiddleware(); - builder.UseActivityMiddleware(); - - builder - .AddOrchestration() - .AddOrchestration(); - - builder.AddActivitiesFromAssembly(); - }) - .UseConsoleLifetime() - .Build(); + HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); + + builder.Configuration.AddUserSecrets(); + + builder.Services.Configure(opt => + { + opt.CreateIfNotExists = true; + }); + builder.Services.AddSingleton(); + builder.Services.AddHostedService(); + builder.Services.AddSingleton(UseLocalEmulator()); + + builder.ConfigureTaskHubWorker((context, builder) => + { + builder.AddClient(); + builder.UseOrchestrationMiddleware(); + builder.UseActivityMiddleware(); + + builder + .AddOrchestration() + .AddOrchestration(); + + builder.AddActivitiesFromAssembly(); + }); + + IHost host = builder.Build(); return host.RunAsync(); } diff --git a/src/DurableTask.Hosting/src/Extensions/TaskHubHostBuilderExtensions.cs b/src/DurableTask.Hosting/src/Extensions/TaskHubHostBuilderExtensions.cs index 8e65860..cfe1a61 100644 --- a/src/DurableTask.Hosting/src/Extensions/TaskHubHostBuilderExtensions.cs +++ b/src/DurableTask.Hosting/src/Extensions/TaskHubHostBuilderExtensions.cs @@ -28,6 +28,21 @@ public static IHostBuilder ConfigureTaskHubWorker( return builder.ConfigureTaskHubWorker(configure, _ => { }); } + /// + /// Configures the task hub worker background service. + /// + /// The host builder, not null. + /// The action to configure the worker, not null. + /// The original host builder with task hub worker configured. + public static IHostApplicationBuilder ConfigureTaskHubWorker( + this IHostApplicationBuilder builder, Action configure) + { + Check.NotNull(builder); + Check.NotNull(configure); + + return builder.ConfigureTaskHubWorker(configure, _ => { }); + } + /// /// Configures the task hub worker background service. /// @@ -47,6 +62,25 @@ public static IHostBuilder ConfigureTaskHubWorker( return builder.ConfigureTaskHubWorker((_, b) => configure(b), configureOptions); } + /// + /// Configures the task hub worker background service. + /// + /// The host builder, not null. + /// The action to configure the worker, not null. + /// The action to configure the task hub host options. + /// The original host builder with task hub worker configured. + public static IHostApplicationBuilder ConfigureTaskHubWorker( + this IHostApplicationBuilder builder, + Action configure, + Action configureOptions) + { + Check.NotNull(builder); + Check.NotNull(configure); + Check.NotNull(configureOptions); + + return builder.ConfigureTaskHubWorker((_, b) => configure(b), configureOptions); + } + /// /// Configures the task hub worker background service. /// @@ -61,6 +95,20 @@ public static IHostBuilder ConfigureTaskHubWorker( return builder.ConfigureTaskHubWorker(configure, _ => { }); } + /// + /// Configures the task hub worker background service. + /// + /// The host builder, not null. + /// The action to configure the worker, not null. + /// The original host builder with task hub worker configured. + public static IHostApplicationBuilder ConfigureTaskHubWorker( + this IHostApplicationBuilder builder, Action configure) + { + Check.NotNull(builder); + Check.NotNull(configure); + return builder.ConfigureTaskHubWorker(configure, _ => { }); + } + /// /// Configures the task hub worker background service. /// @@ -93,4 +141,34 @@ public static IHostBuilder ConfigureTaskHubWorker( return builder; } + + /// + /// Configures the task hub worker background service. + /// + /// The host builder, not null. + /// The action to configure the worker, not null. + /// The action to configure the task hub host options. + /// The original host builder with task hub worker configured. + public static IHostApplicationBuilder ConfigureTaskHubWorker( + this IHostApplicationBuilder builder, + Action configure, + Action configureOptions) + { + Check.NotNull(builder); + Check.NotNull(configure); + Check.NotNull(configureOptions); + + builder.Services.AddOptions(); + builder.Services.AddLogging(); + + builder.Services + .AddOptions() + .Bind(builder.Configuration.GetSection("TaskHub")) + .Configure(configureOptions); + + builder.Services.AddTaskHubWorker(taskHubBuilder => configure(builder, taskHubBuilder)); + builder.Services.AddHostedService(); + + return builder; + } } diff --git a/src/DurableTask.Hosting/test/Extensions/TaskHubHostBuilderExtensionsTests.cs b/src/DurableTask.Hosting/test/Extensions/TaskHubHostBuilderExtensionsTests.cs index cabe343..7dd798f 100644 --- a/src/DurableTask.Hosting/test/Extensions/TaskHubHostBuilderExtensionsTests.cs +++ b/src/DurableTask.Hosting/test/Extensions/TaskHubHostBuilderExtensionsTests.cs @@ -12,33 +12,55 @@ namespace DurableTask.Hosting.Extensions.Tests; public class TaskHubHostBuilderExtensionsTests { [Fact] - public void ConfigureTaskHubWorker_ArgumentNullBuilder() + public void Configure_HostBuilder_TaskHubWorker_ArgumentNullBuilder() => RunTestException( - builder => TaskHubHostBuilderExtensions - .ConfigureTaskHubWorker(null, b => { })); + (IHostBuilder builder) => TaskHubHostBuilderExtensions + .ConfigureTaskHubWorker((IHostBuilder)null, b => { })); [Fact] - public void ConfigureTaskHubWorker_ArgumentNullConfigure() + public void Configure_HostApplicationBuilder_TaskHubWorker_ArgumentNullBuilder() => RunTestException( - builder => TaskHubHostBuilderExtensions + (IHostApplicationBuilder builder) => TaskHubHostBuilderExtensions + .ConfigureTaskHubWorker((IHostApplicationBuilder)null, b => { })); + [Fact] + public void Configure_HostBuilder_TaskHubWorker_ArgumentNullConfigure() + => RunTestException( + (IHostBuilder builder) => TaskHubHostBuilderExtensions + .ConfigureTaskHubWorker(builder, (Action)null)); + + [Fact] + public void Configure_HostApplicationBuilder_TaskHubWorker_ArgumentNullConfigure() + => RunTestException( + (IHostApplicationBuilder builder) => TaskHubHostBuilderExtensions .ConfigureTaskHubWorker(builder, (Action)null)); [Fact] - public void ConfigureTaskHubWorker_ArgumentNullBuilder2() + public void Configure_HostBuilder_TaskHubWorker_ArgumentNullBuilder2() => RunTestException( - builder => TaskHubHostBuilderExtensions - .ConfigureTaskHubWorker(null, (c, b) => { })); + (IHostBuilder builder) => TaskHubHostBuilderExtensions + .ConfigureTaskHubWorker((IHostBuilder)null, (c, b) => { })); [Fact] - public void ConfigureTaskHubWorker_ArgumentNullConfigure2() + public void Configure_HostApplicationBuilder_TaskHubWorker_ArgumentNullBuilder2() => RunTestException( - builder => TaskHubHostBuilderExtensions + (IHostApplicationBuilder builder) => TaskHubHostBuilderExtensions + .ConfigureTaskHubWorker((IHostApplicationBuilder)null, (c, b) => { })); + + [Fact] + public void Configure_HostBuilder_TaskHubWorker_ArgumentNullConfigure2() + => RunTestException( + (IHostBuilder builder) => TaskHubHostBuilderExtensions .ConfigureTaskHubWorker(builder, (Action)null)); + [Fact] + public void Configure_HostApplicationBuilder_TaskHubWorker_ArgumentNullConfigure2() + => RunTestException( + (IHostApplicationBuilder builder) => TaskHubHostBuilderExtensions + .ConfigureTaskHubWorker(builder, (Action)null)); [Fact] - public void ConfigureTaskHubWorker_Callback() + public void Configure_HostBuilder_TaskHubWorker_Callback() { ITaskHubWorkerBuilder capturedBuilder = null; RunTest( - builder => builder.ConfigureTaskHubWorker(b => capturedBuilder = b), + (IHostBuilder builder) => builder.ConfigureTaskHubWorker(b => capturedBuilder = b), (builder, returned) => { returned.Should().BeSameAs(builder); @@ -47,12 +69,44 @@ public void ConfigureTaskHubWorker_Callback() } [Fact] - public void ConfigureTaskHubWorker_Callback2() + public void Configure_HostApplicationBuilder_TaskHubWorker_Callback() + { + ITaskHubWorkerBuilder capturedBuilder = null; + RunTest( + (IHostApplicationBuilder builder) => builder.ConfigureTaskHubWorker(b => capturedBuilder = b), + (builder, returned) => + { + returned.Should().BeSameAs(builder); + capturedBuilder.Should().NotBeNull(); + }); + } + + [Fact] + public void Configure_HostBuilder_TaskHubWorker_Callback2() { HostBuilderContext capturedContext = null; ITaskHubWorkerBuilder capturedBuilder = null; RunTest( - builder => builder.ConfigureTaskHubWorker((c, b) => + (IHostBuilder builder) => builder.ConfigureTaskHubWorker((c, b) => + { + capturedContext = c; + capturedBuilder = b; + }), + (builder, returned) => + { + returned.Should().BeSameAs(builder); + capturedContext.Should().NotBeNull(); + capturedBuilder.Should().NotBeNull(); + }); + } + + [Fact] + public void Configure_HostApplicationBuilder_TaskHubWorker_Callback2() + { + IHostApplicationBuilder capturedContext = null; + ITaskHubWorkerBuilder capturedBuilder = null; + RunTest( + (IHostApplicationBuilder builder) => builder.ConfigureTaskHubWorker((c, b) => { capturedContext = c; capturedBuilder = b; @@ -78,6 +132,19 @@ bool Act(IHostBuilder builder) exception.Should().NotBeNull(); } + private static void RunTestException(Action act) + where TException : Exception + { + bool Act(IHostApplicationBuilder builder) + { + act(builder); + return true; + } + + TException exception = Capture(() => RunTest(Act, null)); + exception.Should().NotBeNull(); + } + private static void RunTest( Func act, Action verify) @@ -88,4 +155,15 @@ private static void RunTest( builder.Build(); verify?.Invoke(builder, result); } + + private static void RunTest( + Func act, + Action verify) + { + HostApplicationBuilder builder = new(); + + TResult result = act(builder); + builder.Build(); + verify?.Invoke(builder, result); + } } From 7df12f4d7b914567bd14ad511c9540236b5fb6f9 Mon Sep 17 00:00:00 2001 From: Pedro Paulo Vezza Campos Date: Tue, 19 Mar 2024 12:54:03 -0700 Subject: [PATCH 2/2] Update documentation TaskHubHostBuilderExtensions.cs --- .../src/Extensions/TaskHubHostBuilderExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DurableTask.Hosting/src/Extensions/TaskHubHostBuilderExtensions.cs b/src/DurableTask.Hosting/src/Extensions/TaskHubHostBuilderExtensions.cs index cfe1a61..e8443c9 100644 --- a/src/DurableTask.Hosting/src/Extensions/TaskHubHostBuilderExtensions.cs +++ b/src/DurableTask.Hosting/src/Extensions/TaskHubHostBuilderExtensions.cs @@ -9,7 +9,7 @@ namespace DurableTask.Hosting; /// -/// Extensions for configuring a task hub worker service on . +/// Extensions for configuring a task hub worker service on or . /// public static class TaskHubHostBuilderExtensions {