diff --git a/src/Client/Core/DependencyInjection/ServiceCollectionExtensions.cs b/src/Client/Core/DependencyInjection/ServiceCollectionExtensions.cs index 89d4db19..a1d96c5f 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; @@ -47,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 7ddce4d5..2dd643ac 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] @@ -145,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