Skip to content

Commit c464294

Browse files
WhitWaldosvegiraju-microsoft
authored andcommitted
Fix for DI registration not completing as expected (dapr#1386)
* Tentative fix for DI registration not completing as expected Signed-off-by: Whit Waldo <[email protected]> * Making injected IConfiguration optional as it might not be populated if user isn't utilizing ASP.NET Core from caller Signed-off-by: Whit Waldo <[email protected]> * Fixed DI injection issue Signed-off-by: Whit Waldo <[email protected]> * Removed registration of DaprWorkflowClientBuilderFactory Signed-off-by: Whit Waldo <[email protected]> * Updated field names for consistency Signed-off-by: Whit Waldo <[email protected]> * Minor formatting changes Signed-off-by: Whit Waldo <[email protected]> * Fixed build error caused by bad merge resolution Signed-off-by: Whit Waldo <[email protected]> --------- Signed-off-by: Whit Waldo <[email protected]> Signed-off-by: Siri Varma Vegiraju <[email protected]>
1 parent a06605c commit c464294

File tree

2 files changed

+55
-55
lines changed

2 files changed

+55
-55
lines changed

src/Dapr.Workflow/DaprWorkflowClientBuilderFactory.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,40 @@
1919
using Microsoft.Extensions.Configuration;
2020
using Microsoft.Extensions.DependencyInjection;
2121

22-
#nullable enable
23-
2422
namespace Dapr.Workflow;
2523

2624
/// <summary>
2725
/// A factory for building a <see cref="DaprWorkflowClient"/>.
2826
/// </summary>
2927
internal sealed class DaprWorkflowClientBuilderFactory
3028
{
31-
private readonly IConfiguration _configuration;
32-
private readonly IHttpClientFactory _httpClientFactory;
33-
private readonly IServiceCollection _services;
29+
private readonly IConfiguration? configuration;
30+
private readonly IHttpClientFactory httpClientFactory;
3431

3532
/// <summary>
3633
/// Constructor used to inject the required types into the factory.
3734
/// </summary>
38-
public DaprWorkflowClientBuilderFactory(IConfiguration configuration, IHttpClientFactory httpClientFactory, IServiceCollection services)
35+
public DaprWorkflowClientBuilderFactory(IConfiguration? configuration, IHttpClientFactory httpClientFactory)
3936
{
40-
_configuration = configuration;
41-
_httpClientFactory = httpClientFactory;
42-
_services = services;
37+
this.configuration = configuration;
38+
this.httpClientFactory = httpClientFactory;
4339
}
4440

4541
/// <summary>
4642
/// Responsible for building the client itself.
4743
/// </summary>
4844
/// <returns></returns>
49-
public void CreateClientBuilder(Action<WorkflowRuntimeOptions> configure)
45+
public void CreateClientBuilder(IServiceCollection services, Action<WorkflowRuntimeOptions> configure)
5046
{
51-
_services.AddDurableTaskClient(builder =>
47+
services.AddDurableTaskClient(builder =>
5248
{
5349
WorkflowRuntimeOptions options = new();
54-
configure?.Invoke(options);
50+
configure.Invoke(options);
5551

56-
var apiToken = DaprDefaults.GetDefaultDaprApiToken(_configuration);
57-
var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(_configuration);
52+
var apiToken = DaprDefaults.GetDefaultDaprApiToken(configuration);
53+
var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(configuration);
5854

59-
var httpClient = _httpClientFactory.CreateClient();
55+
var httpClient = httpClientFactory.CreateClient();
6056

6157
if (!string.IsNullOrWhiteSpace(apiToken))
6258
{
@@ -72,17 +68,17 @@ public void CreateClientBuilder(Action<WorkflowRuntimeOptions> configure)
7268
builder.RegisterDirectly();
7369
});
7470

75-
_services.AddDurableTaskWorker(builder =>
71+
services.AddDurableTaskWorker(builder =>
7672
{
7773
WorkflowRuntimeOptions options = new();
78-
configure?.Invoke(options);
74+
configure.Invoke(options);
7975

80-
var apiToken = DaprDefaults.GetDefaultDaprApiToken(_configuration);
81-
var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(_configuration);
76+
var apiToken = DaprDefaults.GetDefaultDaprApiToken(configuration);
77+
var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(configuration);
8278

8379
if (!string.IsNullOrEmpty(grpcEndpoint))
8480
{
85-
var httpClient = _httpClientFactory.CreateClient();
81+
var httpClient = httpClientFactory.CreateClient();
8682

8783
if (!string.IsNullOrWhiteSpace(apiToken))
8884
{

src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,55 @@
1111
// limitations under the License.
1212
// ------------------------------------------------------------------------
1313

14-
namespace Dapr.Workflow
15-
{
16-
using System;
17-
using Microsoft.Extensions.DependencyInjection;
18-
using Microsoft.Extensions.DependencyInjection.Extensions;
14+
using System;
15+
using System.Net.Http;
16+
using Microsoft.Extensions.Configuration;
17+
using Microsoft.Extensions.DependencyInjection;
18+
using Microsoft.Extensions.DependencyInjection.Extensions;
19+
20+
namespace Dapr.Workflow;
1921

22+
/// <summary>
23+
/// Contains extension methods for using Dapr Workflow with dependency injection.
24+
/// </summary>
25+
public static class WorkflowServiceCollectionExtensions
26+
{
2027
/// <summary>
21-
/// Contains extension methods for using Dapr Workflow with dependency injection.
28+
/// Adds Dapr Workflow support to the service collection.
2229
/// </summary>
23-
public static class WorkflowServiceCollectionExtensions
30+
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
31+
/// <param name="configure">A delegate used to configure actor options and register workflow functions.</param>
32+
public static IServiceCollection AddDaprWorkflow(
33+
this IServiceCollection serviceCollection,
34+
Action<WorkflowRuntimeOptions> configure)
2435
{
25-
/// <summary>
26-
/// Adds Dapr Workflow support to the service collection.
27-
/// </summary>
28-
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
29-
/// <param name="configure">A delegate used to configure actor options and register workflow functions.</param>
30-
public static IServiceCollection AddDaprWorkflow(
31-
this IServiceCollection serviceCollection,
32-
Action<WorkflowRuntimeOptions> configure)
36+
if (serviceCollection == null)
3337
{
34-
if (serviceCollection == null)
35-
{
36-
throw new ArgumentNullException(nameof(serviceCollection));
37-
}
38+
throw new ArgumentNullException(nameof(serviceCollection));
39+
}
3840

39-
serviceCollection.TryAddSingleton<WorkflowRuntimeOptions>();
40-
serviceCollection.AddHttpClient();
41+
serviceCollection.TryAddSingleton<WorkflowRuntimeOptions>();
42+
serviceCollection.AddHttpClient();
4143

4244
#pragma warning disable CS0618 // Type or member is obsolete - keeping around temporarily - replaced by DaprWorkflowClient
43-
serviceCollection.TryAddSingleton<WorkflowEngineClient>();
45+
serviceCollection.TryAddSingleton<WorkflowEngineClient>();
4446
#pragma warning restore CS0618 // Type or member is obsolete
45-
serviceCollection.AddHostedService<WorkflowLoggingService>();
46-
serviceCollection.TryAddSingleton<DaprWorkflowClient>();
47-
serviceCollection.AddDaprClient();
47+
serviceCollection.AddHostedService<WorkflowLoggingService>();
48+
serviceCollection.TryAddSingleton<DaprWorkflowClient>();
49+
serviceCollection.AddDaprClient();
4850

49-
serviceCollection.AddOptions<WorkflowRuntimeOptions>().Configure(configure);
50-
51-
serviceCollection.AddSingleton(c =>
52-
{
53-
var factory = c.GetRequiredService<DaprWorkflowClientBuilderFactory>();
54-
factory.CreateClientBuilder(configure);
55-
return new object(); //Placeholder as actual registration is performed inside factory
56-
});
57-
58-
return serviceCollection;
51+
serviceCollection.AddOptions<WorkflowRuntimeOptions>().Configure(configure);
52+
53+
//Register the factory and force resolution so the Durable Task client and worker can be registered
54+
using (var scope = serviceCollection.BuildServiceProvider().CreateScope())
55+
{
56+
var httpClientFactory = scope.ServiceProvider.GetRequiredService<IHttpClientFactory>();
57+
var configuration = scope.ServiceProvider.GetService<IConfiguration>();
58+
59+
var factory = new DaprWorkflowClientBuilderFactory(configuration, httpClientFactory);
60+
factory.CreateClientBuilder(serviceCollection, configure);
5961
}
62+
63+
return serviceCollection;
6064
}
6165
}

0 commit comments

Comments
 (0)