|
5 | 5 | using DurableTask.Core.Entities; |
6 | 6 | using Microsoft.DurableTask.Client.OrchestrationServiceClientShim; |
7 | 7 | using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
8 | 9 | using Microsoft.Extensions.Options; |
9 | 10 |
|
10 | 11 | namespace Microsoft.DurableTask.Client; |
@@ -56,55 +57,74 @@ public static IDurableTaskClientBuilder UseOrchestrationService( |
56 | 57 | { |
57 | 58 | Check.NotNull(builder); |
58 | 59 | Check.NotNull(configure); |
59 | | - builder.Services.Configure(builder.Name, configure); |
60 | | - builder.Services.AddOptions<ShimDurableTaskClientOptions>(builder.Name) |
61 | | - .PostConfigure<IServiceProvider>((opt, sp) => |
62 | | - { |
63 | | - ConfigureClient(sp, opt); |
64 | | - ConfigureEntities(builder.Name, sp, opt); |
65 | | - }) |
66 | | - .Validate(x => x.Client is not null, "ShimDurableTaskClientOptions.Client must not be null.") |
67 | | - .Validate( |
68 | | - x => !x.EnableEntitySupport || x.Entities.Queries is not null, |
69 | | - "ShimDurableTaskClientOptions.Entities.Queries must not be null when entity support is enabled."); |
70 | | - |
| 60 | + builder.Services.AddOptions<ShimDurableTaskClientOptions>(builder.Name).Configure(configure); |
| 61 | + builder.Services.TryAddSingleton<IPostConfigureOptions<ShimDurableTaskClientOptions>, OptionsConfigure>(); |
| 62 | + builder.Services.TryAddSingleton<IValidateOptions<ShimDurableTaskClientOptions>, OptionsValidator>(); |
71 | 63 | return builder.UseBuildTarget<ShimDurableTaskClient, ShimDurableTaskClientOptions>(); |
72 | 64 | } |
73 | 65 |
|
74 | | - static void ConfigureClient(IServiceProvider services, ShimDurableTaskClientOptions options) |
| 66 | + static IEntityOrchestrationService? GetEntityService( |
| 67 | + IServiceProvider services, ShimDurableTaskClientOptions options) |
75 | 68 | { |
76 | | - if (options.Client is not null) |
77 | | - { |
78 | | - return; |
79 | | - } |
80 | | - |
81 | | - // Try to resolve client from service container. |
82 | | - options.Client = services.GetService<IOrchestrationServiceClient>() |
83 | | - ?? services.GetService<IOrchestrationService>() as IOrchestrationServiceClient; |
| 69 | + return options.Client as IEntityOrchestrationService |
| 70 | + ?? services.GetService<IEntityOrchestrationService>() |
| 71 | + ?? services.GetService<IOrchestrationServiceClient>() as IEntityOrchestrationService |
| 72 | + ?? services.GetService<IOrchestrationService>() as IEntityOrchestrationService; |
84 | 73 | } |
85 | 74 |
|
86 | | - static void ConfigureEntities(string name, IServiceProvider services, ShimDurableTaskClientOptions options) |
| 75 | + class OptionsConfigure(IServiceProvider services) : IPostConfigureOptions<ShimDurableTaskClientOptions> |
87 | 76 | { |
88 | | - if (options.Entities.Queries is null) |
| 77 | + public void PostConfigure(string name, ShimDurableTaskClientOptions options) |
89 | 78 | { |
90 | | - options.Entities.Queries = services.GetService<EntityBackendQueries>() |
91 | | - ?? GetEntityService(services, options)?.EntityBackendQueries; |
| 79 | + ConfigureClient(services, options); |
| 80 | + ConfigureEntities(name, services, options); // Must be called after ConfigureClient. |
92 | 81 | } |
93 | 82 |
|
94 | | - if (options.Entities.MaxSignalDelayTime is null) |
| 83 | + static void ConfigureClient(IServiceProvider services, ShimDurableTaskClientOptions options) |
95 | 84 | { |
96 | | - EntityBackendProperties? properties = services.GetService<IOptionsMonitor<EntityBackendProperties>>()?.Get(name) |
97 | | - ?? GetEntityService(services, options)?.EntityBackendProperties; |
98 | | - options.Entities.MaxSignalDelayTime = properties?.MaximumSignalDelayTime; |
| 85 | + if (options.Client is not null) |
| 86 | + { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Try to resolve client from service container. |
| 91 | + options.Client = services.GetService<IOrchestrationServiceClient>() |
| 92 | + ?? services.GetService<IOrchestrationService>() as IOrchestrationServiceClient; |
| 93 | + } |
| 94 | + |
| 95 | + static void ConfigureEntities(string name, IServiceProvider services, ShimDurableTaskClientOptions options) |
| 96 | + { |
| 97 | + if (options.Entities.Queries is null) |
| 98 | + { |
| 99 | + options.Entities.Queries = services.GetService<EntityBackendQueries>() |
| 100 | + ?? GetEntityService(services, options)?.EntityBackendQueries; |
| 101 | + } |
| 102 | + |
| 103 | + if (options.Entities.MaxSignalDelayTime is null) |
| 104 | + { |
| 105 | + EntityBackendProperties? properties = services.GetService<IOptionsMonitor<EntityBackendProperties>>()?.Get(name) |
| 106 | + ?? GetEntityService(services, options)?.EntityBackendProperties; |
| 107 | + options.Entities.MaxSignalDelayTime = properties?.MaximumSignalDelayTime; |
| 108 | + } |
99 | 109 | } |
100 | 110 | } |
101 | 111 |
|
102 | | - static IEntityOrchestrationService? GetEntityService( |
103 | | - IServiceProvider services, ShimDurableTaskClientOptions options) |
| 112 | + class OptionsValidator : IValidateOptions<ShimDurableTaskClientOptions> |
104 | 113 | { |
105 | | - return services.GetService<IEntityOrchestrationService>() |
106 | | - ?? services.GetService<IOrchestrationService>() as IEntityOrchestrationService |
107 | | - ?? services.GetService<IOrchestrationServiceClient>() as IEntityOrchestrationService |
108 | | - ?? options.Client as IEntityOrchestrationService; |
| 114 | + public ValidateOptionsResult Validate(string name, ShimDurableTaskClientOptions options) |
| 115 | + { |
| 116 | + if (options.Client is null) |
| 117 | + { |
| 118 | + return ValidateOptionsResult.Fail("ShimDurableTaskClientOptions.Client must not be null."); |
| 119 | + } |
| 120 | + |
| 121 | + if (options.EnableEntitySupport && options.Entities.Queries is null) |
| 122 | + { |
| 123 | + return ValidateOptionsResult.Fail( |
| 124 | + "ShimDurableTaskClientOptions.Entities.Queries must not be null when entity support is enabled."); |
| 125 | + } |
| 126 | + |
| 127 | + return ValidateOptionsResult.Success; |
| 128 | + } |
109 | 129 | } |
110 | 130 | } |
0 commit comments