|
11 | 11 | // limitations under the License.
|
12 | 12 | // ------------------------------------------------------------------------
|
13 | 13 |
|
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; |
19 | 21 |
|
| 22 | +/// <summary> |
| 23 | +/// Contains extension methods for using Dapr Workflow with dependency injection. |
| 24 | +/// </summary> |
| 25 | +public static class WorkflowServiceCollectionExtensions |
| 26 | +{ |
20 | 27 | /// <summary>
|
21 |
| - /// Contains extension methods for using Dapr Workflow with dependency injection. |
| 28 | + /// Adds Dapr Workflow support to the service collection. |
22 | 29 | /// </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) |
24 | 35 | {
|
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) |
33 | 37 | {
|
34 |
| - if (serviceCollection == null) |
35 |
| - { |
36 |
| - throw new ArgumentNullException(nameof(serviceCollection)); |
37 |
| - } |
| 38 | + throw new ArgumentNullException(nameof(serviceCollection)); |
| 39 | + } |
38 | 40 |
|
39 |
| - serviceCollection.TryAddSingleton<WorkflowRuntimeOptions>(); |
40 |
| - serviceCollection.AddHttpClient(); |
| 41 | + serviceCollection.TryAddSingleton<WorkflowRuntimeOptions>(); |
| 42 | + serviceCollection.AddHttpClient(); |
41 | 43 |
|
42 | 44 | #pragma warning disable CS0618 // Type or member is obsolete - keeping around temporarily - replaced by DaprWorkflowClient
|
43 |
| - serviceCollection.TryAddSingleton<WorkflowEngineClient>(); |
| 45 | + serviceCollection.TryAddSingleton<WorkflowEngineClient>(); |
44 | 46 | #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(); |
48 | 50 |
|
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); |
59 | 61 | }
|
| 62 | + |
| 63 | + return serviceCollection; |
60 | 64 | }
|
61 | 65 | }
|
0 commit comments