From fbabbc34d8b2fb4c866dc7c5cf30c0e985693eab Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Thu, 21 Nov 2024 17:09:58 -0600 Subject: [PATCH 1/3] Added means to customize the Dapr client from the Dapr Workflow registration Signed-off-by: Whit Waldo --- .../WorkflowServiceCollectionExtensions.cs | 5 +++- ...orkflowServiceCollectionExtensionsTests.cs | 30 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs b/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs index 209e4edc0..f14c6aec2 100644 --- a/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs +++ b/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs @@ -13,6 +13,7 @@ using System; using System.Net.Http; +using Dapr.Client; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -29,10 +30,12 @@ public static class WorkflowServiceCollectionExtensions /// /// The . /// A delegate used to configure actor options and register workflow functions. + /// A delegate used to optionally configure the Dapr client connection. /// The lifetime of the registered services. public static IServiceCollection AddDaprWorkflow( this IServiceCollection serviceCollection, Action configure, + Action? configureDaprClient = null, ServiceLifetime lifetime = ServiceLifetime.Singleton) { if (serviceCollection == null) @@ -40,7 +43,7 @@ public static IServiceCollection AddDaprWorkflow( throw new ArgumentNullException(nameof(serviceCollection)); } - serviceCollection.AddDaprClient(lifetime: lifetime); + serviceCollection.AddDaprClient(configureDaprClient, lifetime: lifetime); serviceCollection.AddHttpClient(); serviceCollection.AddHostedService(); diff --git a/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs b/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs index 2206d939b..c718bad9c 100644 --- a/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs +++ b/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs @@ -1,15 +1,37 @@ -using Microsoft.Extensions.DependencyInjection; +using System.Text.Json; +using Dapr.Client; +using Microsoft.Extensions.DependencyInjection; namespace Dapr.Workflow.Test; public class WorkflowServiceCollectionExtensionsTests { + [Fact] + public void ConfigureDaprClient_FromWorkflowClientRegistration() + { + const int maxDepth = 4; + const bool writeIndented = true; + + var services = new ServiceCollection(); + + services.AddDaprWorkflow(options => { }, builder => + { + builder.UseJsonSerializationOptions(new JsonSerializerOptions { MaxDepth = maxDepth, WriteIndented = writeIndented }); + }); + + var serviceProvider = services.BuildServiceProvider(); + + var daprClient = serviceProvider.GetRequiredService(); + Assert.Equal(maxDepth, daprClient.JsonSerializerOptions.MaxDepth); + Assert.Equal(writeIndented, daprClient.JsonSerializerOptions.WriteIndented); + } + [Fact] public void RegisterWorkflowClient_ShouldRegisterSingleton_WhenLifetimeIsSingleton() { var services = new ServiceCollection(); - services.AddDaprWorkflow(options => { }, ServiceLifetime.Singleton); + services.AddDaprWorkflow(options => { }, lifetime: ServiceLifetime.Singleton); var serviceProvider = services.BuildServiceProvider(); var daprWorkflowClient1 = serviceProvider.GetService(); @@ -26,7 +48,7 @@ public async Task RegisterWorkflowClient_ShouldRegisterScoped_WhenLifetimeIsScop { var services = new ServiceCollection(); - services.AddDaprWorkflow(options => { }, ServiceLifetime.Scoped); + services.AddDaprWorkflow(options => { }, lifetime: ServiceLifetime.Scoped); var serviceProvider = services.BuildServiceProvider(); await using var scope1 = serviceProvider.CreateAsyncScope(); @@ -45,7 +67,7 @@ public void RegisterWorkflowClient_ShouldRegisterTransient_WhenLifetimeIsTransie { var services = new ServiceCollection(); - services.AddDaprWorkflow(options => { }, ServiceLifetime.Transient); + services.AddDaprWorkflow(options => { }, lifetime: ServiceLifetime.Transient); var serviceProvider = services.BuildServiceProvider(); var daprWorkflowClient1 = serviceProvider.GetService(); From c6eafe69e9f5733f7bd0601cbd2a8eca63f68987 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Thu, 21 Nov 2024 17:18:16 -0600 Subject: [PATCH 2/3] Updated to favor the overload that provides an IServiceProvider Signed-off-by: Whit Waldo --- .../WorkflowServiceCollectionExtensions.cs | 13 +++++++++++-- .../WorkflowServiceCollectionExtensionsTests.cs | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs b/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs index f14c6aec2..ad326f261 100644 --- a/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs +++ b/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs @@ -35,7 +35,7 @@ public static class WorkflowServiceCollectionExtensions public static IServiceCollection AddDaprWorkflow( this IServiceCollection serviceCollection, Action configure, - Action? configureDaprClient = null, + Action? configureDaprClient = null, ServiceLifetime lifetime = ServiceLifetime.Singleton) { if (serviceCollection == null) @@ -43,7 +43,16 @@ public static IServiceCollection AddDaprWorkflow( throw new ArgumentNullException(nameof(serviceCollection)); } - serviceCollection.AddDaprClient(configureDaprClient, lifetime: lifetime); + + if (configureDaprClient is not null) + { + serviceCollection.AddDaprClient(configureDaprClient, lifetime: lifetime); + } + else + { + serviceCollection.AddDaprClient(lifetime: lifetime); + } + serviceCollection.AddHttpClient(); serviceCollection.AddHostedService(); diff --git a/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs b/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs index c718bad9c..307da6052 100644 --- a/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs +++ b/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs @@ -9,14 +9,17 @@ public class WorkflowServiceCollectionExtensionsTests [Fact] public void ConfigureDaprClient_FromWorkflowClientRegistration() { - const int maxDepth = 4; + const int maxDepth = 6; const bool writeIndented = true; var services = new ServiceCollection(); + services.AddSingleton(_ => new MyAwesomeType { MaxDepth = maxDepth }); - services.AddDaprWorkflow(options => { }, builder => + services.AddDaprWorkflow(_ => { }, (serviceProvider, builder) => { - builder.UseJsonSerializationOptions(new JsonSerializerOptions { MaxDepth = maxDepth, WriteIndented = writeIndented }); + var myType = serviceProvider.GetRequiredService(); + + builder.UseJsonSerializationOptions(new JsonSerializerOptions { MaxDepth = myType.MaxDepth ?? 0, WriteIndented = writeIndented }); }); var serviceProvider = services.BuildServiceProvider(); @@ -77,4 +80,12 @@ public void RegisterWorkflowClient_ShouldRegisterTransient_WhenLifetimeIsTransie Assert.NotNull(daprWorkflowClient2); Assert.NotSame(daprWorkflowClient1, daprWorkflowClient2); } + + private sealed class MyAwesomeType + { + /// + /// The max depth value. + /// + public int? MaxDepth { get; init; } + } } From 606e326fc77f963470a1778b36a4606955f073b3 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Tue, 3 Dec 2024 03:40:11 -0600 Subject: [PATCH 3/3] Added constructor option to pull values from an IConfiguration Signed-off-by: Whit Waldo --- src/Dapr.Jobs/DaprJobsClientBuilder.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Dapr.Jobs/DaprJobsClientBuilder.cs b/src/Dapr.Jobs/DaprJobsClientBuilder.cs index 390d52236..e65e311e1 100644 --- a/src/Dapr.Jobs/DaprJobsClientBuilder.cs +++ b/src/Dapr.Jobs/DaprJobsClientBuilder.cs @@ -12,6 +12,7 @@ // ------------------------------------------------------------------------ using Dapr.Common; +using Microsoft.Extensions.Configuration; using Autogenerated = Dapr.Client.Autogen.Grpc.v1; namespace Dapr.Jobs; @@ -21,6 +22,14 @@ namespace Dapr.Jobs; /// public sealed class DaprJobsClientBuilder : DaprGenericClientBuilder { + /// + /// Used to initialize a new instance of the . + /// + /// An optional instance of . + public DaprJobsClientBuilder(IConfiguration? configuration = null) : base(configuration) + { + } + /// /// Builds the client instance from the properties of the builder. ///