Skip to content

Commit

Permalink
Register IPlatformInformationService in AddDurableClientFactory() (#1727
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
bachuv authored Mar 13, 2021
1 parent 752d78f commit e17afcc
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public static IServiceCollection AddDurableClientFactory(this IServiceCollection
serviceCollection.TryAddSingleton<IDurabilityProviderFactory, AzureStorageDurabilityProviderFactory>();
serviceCollection.TryAddSingleton<IDurableClientFactory, DurableClientFactory>();
serviceCollection.TryAddSingleton<IMessageSerializerSettingsFactory, MessageSerializerSettingsFactory>();
#pragma warning disable CS0612 // Type or member is obsolete
serviceCollection.TryAddSingleton<IPlatformInformationService, DefaultPlatformInformationProvider>();
#pragma warning restore CS0612 // Type or member is obsolete

return serviceCollection;
}
Expand Down
2 changes: 1 addition & 1 deletion test/Common/DurableClientBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Net.Http;
using System.Threading.Tasks;
using DurableTask.Core;
using FluentAssertions;
#if !FUNCTIONS_V1
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -20,6 +19,7 @@
using Moq;
using Newtonsoft.Json;
using Xunit;
using Xunit.Abstractions;
using static Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests.HttpApiHandlerTests;

namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests
Expand Down
24 changes: 24 additions & 0 deletions test/Common/DurableTaskEndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ public async Task HelloWorld_OrchestrationClientTaskHub(string storageProviderTy
}
}

/// <summary>
/// End to end test that ensures that DurableClientFactory is set up correctly
/// (i.e. the correct services are injected through dependency injection
/// and AzureStorageDurabilityProvider is created).
/// </summary>
[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public async Task DurableClient_AzureStorage_SuccessfulSetup()
{
string orchestratorName = nameof(TestOrchestrations.SayHelloInline);
using (ITestHost host = TestHelpers.GetJobHost(
loggerProvider: this.loggerProvider,
testName: nameof(this.DurableClient_AzureStorage_SuccessfulSetup),
enableExtendedSessions: false,
storageProviderType: "azure_storage",
addDurableClientFactory: true))
{
await host.StartAsync();
var client = await host.StartOrchestratorAsync(orchestratorName, input: "World", this.output);
var status = await client.WaitForCompletionAsync(this.output);
await host.StopAsync();
}
}

/// <summary>
/// End-to-end test which validates a simple orchestrator function does not have assigned value for <see cref="DurableOrchestrationContext.ParentInstanceId"/>.
/// </summary>
Expand Down
28 changes: 17 additions & 11 deletions test/Common/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public static ITestHost GetJobHost(
Action<ITelemetry> onSend = null,
bool rollbackEntityOperationsOnExceptions = true,
int entityMessageReorderWindowInMinutes = 30,
string exactTaskHubName = null)
string exactTaskHubName = null,
bool addDurableClientFactory = false)
{
switch (storageProviderType)
{
Expand Down Expand Up @@ -145,15 +146,18 @@ public static ITestHost GetJobHost(
}

return GetJobHostWithOptions(
loggerProvider,
options,
storageProviderType,
nameResolver,
durableHttpMessageHandler,
lifeCycleNotificationHelper,
serializerSettings,
onSend,
durabilityProviderFactoryType);
loggerProvider: loggerProvider,
durableTaskOptions: options,
storageProviderType: storageProviderType,
nameResolver: nameResolver,
durableHttpMessageHandler: durableHttpMessageHandler,
lifeCycleNotificationHelper: lifeCycleNotificationHelper,
serializerSettings: serializerSettings,
onSend: onSend,
#if !FUNCTIONS_V1
addDurableClientFactory: addDurableClientFactory,
#endif
durabilityProviderFactoryType: durabilityProviderFactoryType);
}

public static ITestHost GetJobHostWithOptions(
Expand All @@ -165,7 +169,8 @@ public static ITestHost GetJobHostWithOptions(
ILifeCycleNotificationHelper lifeCycleNotificationHelper = null,
IMessageSerializerSettingsFactory serializerSettings = null,
Action<ITelemetry> onSend = null,
Type durabilityProviderFactoryType = null)
Type durabilityProviderFactoryType = null,
bool addDurableClientFactory = false)
{
if (serializerSettings == null)
{
Expand All @@ -184,6 +189,7 @@ public static ITestHost GetJobHostWithOptions(
storageProvider: storageProviderType,
#if !FUNCTIONS_V1
durabilityProviderFactoryType: durabilityProviderFactoryType,
addDurableClientFactory: addDurableClientFactory,
#endif
loggerProvider: loggerProvider,
nameResolver: testNameResolver,
Expand Down
32 changes: 30 additions & 2 deletions test/FunctionsV2/PlatformSpecificHelpers.FunctionsV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public static ITestHost CreateJobHost(
IDurableHttpMessageHandlerFactory durableHttpMessageHandler,
ILifeCycleNotificationHelper lifeCycleNotificationHelper,
IMessageSerializerSettingsFactory serializerSettingsFactory,
Action<ITelemetry> onSend)
Action<ITelemetry> onSend,
bool addDurableClientFactory)
{
// Unless otherwise specified, use legacy partition management for tests as it makes the task hubs start up faster.
// These tests run on a single task hub workers, so they don't test partition management anyways, and that is tested
Expand All @@ -53,7 +54,15 @@ public static ITestHost CreateJobHost(
.ConfigureWebJobs(
webJobsBuilder =>
{
webJobsBuilder.AddDurableTask(options, storageProvider, durabilityProviderFactoryType);
if (addDurableClientFactory)
{
webJobsBuilder.AddDurableClientFactoryDurableTask(options);
}
else
{
webJobsBuilder.AddDurableTask(options, storageProvider, durabilityProviderFactoryType);
}

webJobsBuilder.AddAzureStorage();
})
.ConfigureServices(
Expand Down Expand Up @@ -157,6 +166,25 @@ private static IWebJobsBuilder AddMultipleDurabilityProvidersDurableTask(this IW
return builder;
}

/// <summary>
/// Registers the services needed for DurableClientFactory and calls AddDurableClientFactory()
/// which adds the Durable Task extension that uses Azure Storage.
/// </summary>
private static IWebJobsBuilder AddDurableClientFactoryDurableTask(this IWebJobsBuilder builder, IOptions<DurableTaskOptions> options)
{
builder.Services.AddDurableClientFactory();

builder.Services.AddSingleton(options);

var serviceCollection = builder.AddExtension<DurableTaskExtension>()
.BindOptions<DurableTaskOptions>()
.Services.AddSingleton<IConnectionStringResolver, WebJobsConnectionStringProvider>();

serviceCollection.TryAddSingleton<IApplicationLifetimeWrapper, HostLifecycleService>();

return builder;
}

private static IWebJobsBuilder AddRedisDurableTask(this IWebJobsBuilder builder)
{
builder.Services.AddSingleton<IDurabilityProviderFactory, RedisDurabilityProviderFactory>();
Expand Down

0 comments on commit e17afcc

Please sign in to comment.