From ecc097f4820ce71c3ddfff24166962be4e03dded Mon Sep 17 00:00:00 2001 From: Sophia Tevosyan Date: Wed, 29 Oct 2025 20:21:54 -0700 Subject: [PATCH 1/2] first commit --- .../ServiceCollectionExtensions.cs | 5 +++-- .../ServiceCollectionExtensionsTests.cs | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Client/Core/DependencyInjection/ServiceCollectionExtensions.cs b/src/Client/Core/DependencyInjection/ServiceCollectionExtensions.cs index 89d4db19..71e4459f 100644 --- a/src/Client/Core/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Client/Core/DependencyInjection/ServiceCollectionExtensions.cs @@ -13,14 +13,15 @@ namespace Microsoft.DurableTask.Client; public static class ServiceCollectionExtensions { /// - /// Adds and configures Durable Task worker services to the service collection. + /// Configures and adds a to the service collection. /// /// The service collection to add to. /// The name of the builder to add. /// The builder used to configured the . public static IDurableTaskClientBuilder AddDurableTaskClient(this IServiceCollection services, string? name = null) { - Check.NotNull(services); + Check.NotNull(services); + services.TryAddSingleton(); IDurableTaskClientBuilder builder = GetBuilder(services, name ?? Options.DefaultName, out bool added); ConditionalConfigureBuilder(services, builder, added); return builder; diff --git a/test/Client/Core.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs b/test/Client/Core.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs index 7ddce4d5..a8dd68a7 100644 --- a/test/Client/Core.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs +++ b/test/Client/Core.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs @@ -121,6 +121,17 @@ public void AddDurableTaskClient_Named_HostedServiceAdded() x => x.ServiceType == typeof(IDurableTaskClientProvider) && x.Lifetime == ServiceLifetime.Singleton); services.Should().NotContain( x => x.ServiceType == typeof(DurableTaskClient) && x.Lifetime == ServiceLifetime.Singleton); + } + + [Fact] + public void AddDurableTaskClient_Unnamed_HostedServiceAdded() + { + ServiceCollection services = new(); + services.AddDurableTaskClient(); + services.Should().ContainSingle( + x => x.ServiceType == typeof(IDurableTaskClientProvider) && x.Lifetime == ServiceLifetime.Singleton); + services.Should().ContainSingle( + x => x.ServiceType == typeof(DurableTaskClient) && x.Lifetime == ServiceLifetime.Singleton); } [Fact] From f2beca47f60ca278ed927491f568286ae0d98727 Mon Sep 17 00:00:00 2001 From: Sophia Tevosyan Date: Wed, 29 Oct 2025 20:33:29 -0700 Subject: [PATCH 2/2] added another null check and another unit test --- .../ServiceCollectionExtensions.cs | 3 ++- .../ServiceCollectionExtensionsTests.cs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Client/Core/DependencyInjection/ServiceCollectionExtensions.cs b/src/Client/Core/DependencyInjection/ServiceCollectionExtensions.cs index 71e4459f..a1d96c5f 100644 --- a/src/Client/Core/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Client/Core/DependencyInjection/ServiceCollectionExtensions.cs @@ -48,7 +48,8 @@ public static IServiceCollection AddDurableTaskClient( /// The original service collection, for call chaining. public static IServiceCollection AddDurableTaskClient( this IServiceCollection services, string name, Action configure) - { + { + Check.NotNull(services); services.TryAddSingleton(); IDurableTaskClientBuilder builder = GetBuilder(services, name, out bool added); configure.Invoke(builder); diff --git a/test/Client/Core.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs b/test/Client/Core.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs index a8dd68a7..2dd643ac 100644 --- a/test/Client/Core.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs +++ b/test/Client/Core.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs @@ -156,6 +156,19 @@ public void AddDurableTaskClient_DoesNotConfiguresConverter() DurableTaskClientOptions options = services.BuildServiceProvider().GetOptions(); options.DataConverter.Should().BeSameAs(JsonDataConverter.Default); + } + + [Fact] + public void AddDurableTaskClient_NullServices_ThrowsException() + { + Action act = () => ((ServiceCollection)null!).AddDurableTaskClient(); + act.Should().Throw(); + + act = () => ((ServiceCollection)null!).AddDurableTaskClient(builder => { }); + act.Should().Throw(); + + act = () => ((ServiceCollection)null!).AddDurableTaskClient("name", builder => { }); + act.Should().Throw(); } class CustomDataConverter : DataConverter