diff --git a/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs b/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs index 209e4edc..ad326f26 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,16 @@ public static IServiceCollection AddDaprWorkflow( throw new ArgumentNullException(nameof(serviceCollection)); } - serviceCollection.AddDaprClient(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 2206d939..307da605 100644 --- a/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs +++ b/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs @@ -1,15 +1,40 @@ -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 = 6; + const bool writeIndented = true; + + var services = new ServiceCollection(); + services.AddSingleton(_ => new MyAwesomeType { MaxDepth = maxDepth }); + + services.AddDaprWorkflow(_ => { }, (serviceProvider, builder) => + { + var myType = serviceProvider.GetRequiredService(); + + builder.UseJsonSerializationOptions(new JsonSerializerOptions { MaxDepth = myType.MaxDepth ?? 0, 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 +51,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 +70,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(); @@ -55,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; } + } }