Skip to content

Commit e17afcc

Browse files
authored
Register IPlatformInformationService in AddDurableClientFactory() (#1727)
These changes incorporate the changes from #1706 and register IPlatformInformationService in AddDurableClientFactory(). Without registering IPlatformInformationService , creating a DurableClient with Azure Storage fails because it can't find an implementation of IPlatformInformationService in AzureStorageDurabilityProviderFactory. This PR also includes a test in DurableClientBaseTests to help us catch if any new services need to be registered in AddDurableClientFactory() in the future.
1 parent 752d78f commit e17afcc

File tree

5 files changed

+75
-14
lines changed

5 files changed

+75
-14
lines changed

src/WebJobs.Extensions.DurableTask/DurableTaskJobHostConfigurationExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public static IServiceCollection AddDurableClientFactory(this IServiceCollection
4545
serviceCollection.TryAddSingleton<IDurabilityProviderFactory, AzureStorageDurabilityProviderFactory>();
4646
serviceCollection.TryAddSingleton<IDurableClientFactory, DurableClientFactory>();
4747
serviceCollection.TryAddSingleton<IMessageSerializerSettingsFactory, MessageSerializerSettingsFactory>();
48+
#pragma warning disable CS0612 // Type or member is obsolete
49+
serviceCollection.TryAddSingleton<IPlatformInformationService, DefaultPlatformInformationProvider>();
50+
#pragma warning restore CS0612 // Type or member is obsolete
4851

4952
return serviceCollection;
5053
}

test/Common/DurableClientBaseTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Net.Http;
88
using System.Threading.Tasks;
99
using DurableTask.Core;
10-
using FluentAssertions;
1110
#if !FUNCTIONS_V1
1211
using Microsoft.AspNetCore.Http;
1312
using Microsoft.AspNetCore.Mvc;
@@ -20,6 +19,7 @@
2019
using Moq;
2120
using Newtonsoft.Json;
2221
using Xunit;
22+
using Xunit.Abstractions;
2323
using static Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests.HttpApiHandlerTests;
2424

2525
namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests

test/Common/DurableTaskEndToEndTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,30 @@ public async Task HelloWorld_OrchestrationClientTaskHub(string storageProviderTy
194194
}
195195
}
196196

197+
/// <summary>
198+
/// End to end test that ensures that DurableClientFactory is set up correctly
199+
/// (i.e. the correct services are injected through dependency injection
200+
/// and AzureStorageDurabilityProvider is created).
201+
/// </summary>
202+
[Fact]
203+
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
204+
public async Task DurableClient_AzureStorage_SuccessfulSetup()
205+
{
206+
string orchestratorName = nameof(TestOrchestrations.SayHelloInline);
207+
using (ITestHost host = TestHelpers.GetJobHost(
208+
loggerProvider: this.loggerProvider,
209+
testName: nameof(this.DurableClient_AzureStorage_SuccessfulSetup),
210+
enableExtendedSessions: false,
211+
storageProviderType: "azure_storage",
212+
addDurableClientFactory: true))
213+
{
214+
await host.StartAsync();
215+
var client = await host.StartOrchestratorAsync(orchestratorName, input: "World", this.output);
216+
var status = await client.WaitForCompletionAsync(this.output);
217+
await host.StopAsync();
218+
}
219+
}
220+
197221
/// <summary>
198222
/// End-to-end test which validates a simple orchestrator function does not have assigned value for <see cref="DurableOrchestrationContext.ParentInstanceId"/>.
199223
/// </summary>

test/Common/TestHelpers.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public static ITestHost GetJobHost(
6363
Action<ITelemetry> onSend = null,
6464
bool rollbackEntityOperationsOnExceptions = true,
6565
int entityMessageReorderWindowInMinutes = 30,
66-
string exactTaskHubName = null)
66+
string exactTaskHubName = null,
67+
bool addDurableClientFactory = false)
6768
{
6869
switch (storageProviderType)
6970
{
@@ -145,15 +146,18 @@ public static ITestHost GetJobHost(
145146
}
146147

147148
return GetJobHostWithOptions(
148-
loggerProvider,
149-
options,
150-
storageProviderType,
151-
nameResolver,
152-
durableHttpMessageHandler,
153-
lifeCycleNotificationHelper,
154-
serializerSettings,
155-
onSend,
156-
durabilityProviderFactoryType);
149+
loggerProvider: loggerProvider,
150+
durableTaskOptions: options,
151+
storageProviderType: storageProviderType,
152+
nameResolver: nameResolver,
153+
durableHttpMessageHandler: durableHttpMessageHandler,
154+
lifeCycleNotificationHelper: lifeCycleNotificationHelper,
155+
serializerSettings: serializerSettings,
156+
onSend: onSend,
157+
#if !FUNCTIONS_V1
158+
addDurableClientFactory: addDurableClientFactory,
159+
#endif
160+
durabilityProviderFactoryType: durabilityProviderFactoryType);
157161
}
158162

159163
public static ITestHost GetJobHostWithOptions(
@@ -165,7 +169,8 @@ public static ITestHost GetJobHostWithOptions(
165169
ILifeCycleNotificationHelper lifeCycleNotificationHelper = null,
166170
IMessageSerializerSettingsFactory serializerSettings = null,
167171
Action<ITelemetry> onSend = null,
168-
Type durabilityProviderFactoryType = null)
172+
Type durabilityProviderFactoryType = null,
173+
bool addDurableClientFactory = false)
169174
{
170175
if (serializerSettings == null)
171176
{
@@ -184,6 +189,7 @@ public static ITestHost GetJobHostWithOptions(
184189
storageProvider: storageProviderType,
185190
#if !FUNCTIONS_V1
186191
durabilityProviderFactoryType: durabilityProviderFactoryType,
192+
addDurableClientFactory: addDurableClientFactory,
187193
#endif
188194
loggerProvider: loggerProvider,
189195
nameResolver: testNameResolver,

test/FunctionsV2/PlatformSpecificHelpers.FunctionsV2.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public static ITestHost CreateJobHost(
3434
IDurableHttpMessageHandlerFactory durableHttpMessageHandler,
3535
ILifeCycleNotificationHelper lifeCycleNotificationHelper,
3636
IMessageSerializerSettingsFactory serializerSettingsFactory,
37-
Action<ITelemetry> onSend)
37+
Action<ITelemetry> onSend,
38+
bool addDurableClientFactory)
3839
{
3940
// Unless otherwise specified, use legacy partition management for tests as it makes the task hubs start up faster.
4041
// These tests run on a single task hub workers, so they don't test partition management anyways, and that is tested
@@ -53,7 +54,15 @@ public static ITestHost CreateJobHost(
5354
.ConfigureWebJobs(
5455
webJobsBuilder =>
5556
{
56-
webJobsBuilder.AddDurableTask(options, storageProvider, durabilityProviderFactoryType);
57+
if (addDurableClientFactory)
58+
{
59+
webJobsBuilder.AddDurableClientFactoryDurableTask(options);
60+
}
61+
else
62+
{
63+
webJobsBuilder.AddDurableTask(options, storageProvider, durabilityProviderFactoryType);
64+
}
65+
5766
webJobsBuilder.AddAzureStorage();
5867
})
5968
.ConfigureServices(
@@ -157,6 +166,25 @@ private static IWebJobsBuilder AddMultipleDurabilityProvidersDurableTask(this IW
157166
return builder;
158167
}
159168

169+
/// <summary>
170+
/// Registers the services needed for DurableClientFactory and calls AddDurableClientFactory()
171+
/// which adds the Durable Task extension that uses Azure Storage.
172+
/// </summary>
173+
private static IWebJobsBuilder AddDurableClientFactoryDurableTask(this IWebJobsBuilder builder, IOptions<DurableTaskOptions> options)
174+
{
175+
builder.Services.AddDurableClientFactory();
176+
177+
builder.Services.AddSingleton(options);
178+
179+
var serviceCollection = builder.AddExtension<DurableTaskExtension>()
180+
.BindOptions<DurableTaskOptions>()
181+
.Services.AddSingleton<IConnectionStringResolver, WebJobsConnectionStringProvider>();
182+
183+
serviceCollection.TryAddSingleton<IApplicationLifetimeWrapper, HostLifecycleService>();
184+
185+
return builder;
186+
}
187+
160188
private static IWebJobsBuilder AddRedisDurableTask(this IWebJobsBuilder builder)
161189
{
162190
builder.Services.AddSingleton<IDurabilityProviderFactory, RedisDurabilityProviderFactory>();

0 commit comments

Comments
 (0)