Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ namespace Microsoft.DurableTask.Client;
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds and configures Durable Task worker services to the service collection.
/// Configures and adds a <see cref="DurableTaskClient" /> to the service collection.
/// </summary>
/// <param name="services">The service collection to add to.</param>
/// <param name="name">The name of the builder to add.</param>
/// <returns>The builder used to configured the <see cref="DurableTaskClient"/>.</returns>
public static IDurableTaskClientBuilder AddDurableTaskClient(this IServiceCollection services, string? name = null)
{
Check.NotNull(services);
Check.NotNull(services);
services.TryAddSingleton<IDurableTaskClientProvider, DefaultDurableTaskClientProvider>();
IDurableTaskClientBuilder builder = GetBuilder(services, name ?? Options.DefaultName, out bool added);
ConditionalConfigureBuilder(services, builder, added);
return builder;
Expand All @@ -47,7 +48,8 @@ public static IServiceCollection AddDurableTaskClient(
/// <returns>The original service collection, for call chaining.</returns>
public static IServiceCollection AddDurableTaskClient(
this IServiceCollection services, string name, Action<IDurableTaskClientBuilder> configure)
{
{
Check.NotNull(services);
services.TryAddSingleton<IDurableTaskClientProvider, DefaultDurableTaskClientProvider>();
IDurableTaskClientBuilder builder = GetBuilder(services, name, out bool added);
configure.Invoke(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -145,6 +156,19 @@ public void AddDurableTaskClient_DoesNotConfiguresConverter()

DurableTaskClientOptions options = services.BuildServiceProvider().GetOptions<DurableTaskClientOptions>();
options.DataConverter.Should().BeSameAs(JsonDataConverter.Default);
}

[Fact]
public void AddDurableTaskClient_NullServices_ThrowsException()
{
Action act = () => ((ServiceCollection)null!).AddDurableTaskClient();
act.Should().Throw<ArgumentNullException>();

act = () => ((ServiceCollection)null!).AddDurableTaskClient(builder => { });
act.Should().Throw<ArgumentNullException>();

act = () => ((ServiceCollection)null!).AddDurableTaskClient("name", builder => { });
act.Should().Throw<ArgumentNullException>();
}

class CustomDataConverter : DataConverter
Expand Down
Loading