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
49 changes: 8 additions & 41 deletions src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ private static void AddDaprWorkflowCore(
Action<IServiceProvider, DaprWorkflowClientBuilder>? configureClient,
ServiceLifetime lifetime)
{
// Validate that we have a valid service lifetime
if (lifetime is not (ServiceLifetime.Scoped or ServiceLifetime.Singleton or ServiceLifetime.Transient))
throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, "Invalid service lifetime");

// Configure workflow runtime options
var options = new WorkflowRuntimeOptions();
configure(options);
Expand Down Expand Up @@ -222,15 +226,8 @@ private static void AddDaprWorkflowCore(
// Register the workflow worker as a hosted service
serviceCollection.AddHostedService<WorkflowWorker>();

// Register the workflow client - use builder pattern if custom configuration provided
if (configureClient != null)
{
RegisterWorkflowClientWithBuilder(serviceCollection, configureClient, lifetime);
}
else
{
RegisterWorkflowClient(serviceCollection, lifetime);
}
// Register the workflow client
RegisterWorkflowClientWithBuilder(serviceCollection, configureClient, lifetime);
}

private static void AddDaprWorkflowCore(IServiceCollection serviceCollection,
Expand All @@ -251,7 +248,7 @@ private static void EnsureWorkflowGrpcClientRegistered(IServiceCollection servic

private static void RegisterWorkflowClientWithBuilder(
IServiceCollection serviceCollection,
Action<IServiceProvider, DaprWorkflowClientBuilder> configureClient,
Action<IServiceProvider, DaprWorkflowClientBuilder>? configureClient,
ServiceLifetime lifetime)
{
var registration = new Func<IServiceProvider, DaprWorkflowClient>(provider =>
Expand All @@ -263,44 +260,14 @@ private static void RegisterWorkflowClientWithBuilder(
builder.UseServiceProvider(provider);

// Apply custom client configuration (endpoints, etc.)
configureClient(provider, builder);
configureClient?.Invoke(provider, builder);

return builder.Build();
});

serviceCollection.Add(new ServiceDescriptor(typeof(DaprWorkflowClient), registration, lifetime));
}

private static void RegisterWorkflowClient(IServiceCollection serviceCollection, ServiceLifetime lifetime)
{
switch (lifetime)
{
case ServiceLifetime.Singleton:
serviceCollection.TryAddSingleton<DaprWorkflowClient>(sp =>
{
var inner = sp.GetRequiredService<WorkflowClient>();
return new DaprWorkflowClient(inner);
});
break;
case ServiceLifetime.Scoped:
serviceCollection.TryAddScoped<DaprWorkflowClient>(sp =>
{
var inner = sp.GetRequiredService<WorkflowClient>();
return new DaprWorkflowClient(inner);
});
break;
case ServiceLifetime.Transient:
serviceCollection.TryAddTransient<DaprWorkflowClient>(sp =>
{
var inner = sp.GetRequiredService<WorkflowClient>();
return new DaprWorkflowClient(inner);
});
break;
default:
throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, "Invalid service lifetime");
}
}

private static void ConfigureWorkflowGrpcMessageSizeLimits(
IServiceCollection services,
int? maxReceiveMessageSize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Collections.Generic;
using Dapr.DurableTask.Protobuf;
using Dapr.Workflow.Abstractions;
using Dapr.Workflow.Client;
Expand All @@ -7,6 +8,7 @@
using Grpc.Core;
using Grpc.Net.Client;
using Grpc.Net.ClientFactory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -326,6 +328,62 @@ public void AddDaprWorkflowClient_ShouldResolve_GrpcTypedClient()

Assert.NotNull(grpcClient);
}

[Fact]
public void AddDaprWorkflowBuilder_ShouldApplyDaprApiToken_FromConfiguration()
{
var services = new ServiceCollection();
services.AddLogging(b => b.AddProvider(NullLoggerProvider.Instance));

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["DAPR_API_TOKEN"] = "workflow-test-token"
})
.Build();

services.AddSingleton<IConfiguration>(configuration);

string? observedToken = null;
services.AddDaprWorkflowBuilder(_ => { }, (_, builder) =>
{
observedToken = builder.DaprApiToken;
});

var sp = services.BuildServiceProvider();
_ = sp.GetRequiredService<DaprWorkflowClient>();

Assert.Equal("workflow-test-token", observedToken);
}

[Fact]
public void AddDaprWorkflowBuilder_ShouldApplyDaprApiToken_FromEnvironmentVariable()
{
var originalToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");

try
{
Environment.SetEnvironmentVariable("DAPR_API_TOKEN", "workflow-env-token");

var services = new ServiceCollection();
services.AddLogging(b => b.AddProvider(NullLoggerProvider.Instance));

string? observedToken = null;
services.AddDaprWorkflowBuilder(_ => { }, (_, builder) =>
{
observedToken = builder.DaprApiToken;
});

var sp = services.BuildServiceProvider();
_ = sp.GetRequiredService<DaprWorkflowClient>();

Assert.Equal("workflow-env-token", observedToken);
}
finally
{
Environment.SetEnvironmentVariable("DAPR_API_TOKEN", originalToken);
}
}

private sealed record SerializerDependency(string Value);

Expand Down
Loading