-
Notifications
You must be signed in to change notification settings - Fork 53
Add dependency injection support to DurableTaskTestHost #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nytian
wants to merge
6
commits into
main
Choose a base branch
from
nytian/test-host-di
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+969
−9
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac3ef45
initial commit
nytian 60be02f
Merge branch 'main' into nytian/test-host-di
nytian 8edf37f
Merge branch 'main' into nytian/test-host-di
nytian c806020
update
nytian 26a70ff
Merge branch 'nytian/test-host-di' of https://github.com/microsoft/du…
nytian 9dfec73
udpate version
nytian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using DurableTask.Core; | ||
| using Grpc.Net.Client; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Server.Kestrel.Core; | ||
| using Microsoft.DurableTask.Client; | ||
| using Microsoft.DurableTask.Client.Grpc; | ||
| using Microsoft.DurableTask.Testing.Sidecar; | ||
| using Microsoft.DurableTask.Testing.Sidecar.Grpc; | ||
| using Microsoft.DurableTask.Worker; | ||
| using Microsoft.DurableTask.Worker.Grpc; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.DurableTask.Testing; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for integrating in-memory durable task testing with your existing DI container, | ||
| /// such as WebApplicationFactory. | ||
| /// </summary> | ||
| public static class DurableTaskTestExtensions | ||
| { | ||
| /// <summary> | ||
| /// These extensions allow you to inject the <see cref="InMemoryOrchestrationService"/> into your | ||
| /// existing test host so that your orchestrations and activities can resolve services from your DI container. | ||
| /// </summary> | ||
| /// <param name="services">The service collection (from your WebApplicationFactory or host).</param> | ||
| /// <param name="configureTasks">Action to register orchestrators and activities.</param> | ||
| /// <param name="options">Optional configuration options.</param> | ||
| /// <returns>The service collection for chaining.</returns> | ||
| public static IServiceCollection AddInMemoryDurableTask( | ||
| this IServiceCollection services, | ||
| Action<DurableTaskRegistry> configureTasks, | ||
| InMemoryDurableTaskOptions? options = null) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(services); | ||
| ArgumentNullException.ThrowIfNull(configureTasks); | ||
|
|
||
| options ??= new InMemoryDurableTaskOptions(); | ||
|
|
||
| // Determine port for the internal gRPC server | ||
| int port = options.Port ?? Random.Shared.Next(30000, 40000); | ||
| string address = $"http://localhost:{port}"; | ||
|
|
||
| // Register the in-memory orchestration service as a singleton | ||
| services.AddSingleton<InMemoryOrchestrationService>(sp => | ||
| { | ||
| var loggerFactory = sp.GetService<ILoggerFactory>(); | ||
| return new InMemoryOrchestrationService(loggerFactory); | ||
| }); | ||
| services.AddSingleton<IOrchestrationService>(sp => sp.GetRequiredService<InMemoryOrchestrationService>()); | ||
| services.AddSingleton<IOrchestrationServiceClient>(sp => sp.GetRequiredService<InMemoryOrchestrationService>()); | ||
|
|
||
| // Register the gRPC sidecar server as a hosted service | ||
| services.AddSingleton<TaskHubGrpcServer>(); | ||
| services.AddHostedService<InMemoryGrpcSidecarHost>(sp => | ||
| { | ||
| return new InMemoryGrpcSidecarHost( | ||
| address, | ||
| sp.GetRequiredService<InMemoryOrchestrationService>(), | ||
| sp.GetService<ILoggerFactory>()); | ||
| }); | ||
|
|
||
| // Create a gRPC channel that will connect to our internal sidecar | ||
| services.AddSingleton<GrpcChannel>(sp => GrpcChannel.ForAddress(address)); | ||
|
|
||
| // Register the durable task worker (connects to our internal sidecar) | ||
| services.AddDurableTaskWorker(builder => | ||
| { | ||
| builder.UseGrpc(address); | ||
| builder.AddTasks(configureTasks); | ||
| }); | ||
|
|
||
| // Register the durable task client (connects to our internal sidecar) | ||
| services.AddDurableTaskClient(builder => | ||
| { | ||
| builder.UseGrpc(address); | ||
| builder.RegisterDirectly(); | ||
| }); | ||
|
|
||
| return services; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="InMemoryOrchestrationService"/> from the service provider. | ||
| /// Useful for advanced scenarios like inspecting orchestration state. | ||
| /// </summary> | ||
| /// <param name="services">The service provider.</param> | ||
| /// <returns>The in-memory orchestration service instance.</returns> | ||
| public static InMemoryOrchestrationService GetInMemoryOrchestrationService(this IServiceProvider services) | ||
| { | ||
| return services.GetRequiredService<InMemoryOrchestrationService>(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Options for configuring in-memory durable task support. | ||
| /// </summary> | ||
| public class InMemoryDurableTaskOptions | ||
|
Comment on lines
+100
to
+103
|
||
| { | ||
| /// <summary> | ||
| /// Gets or sets the port for the internal gRPC server. | ||
| /// If not set, a random port between 30000-40000 will be used. | ||
| /// </summary> | ||
| public int? Port { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Internal hosted service that runs the gRPC sidecar within the user's host. | ||
| /// </summary> | ||
| internal sealed class InMemoryGrpcSidecarHost : IHostedService, IAsyncDisposable | ||
| { | ||
| private readonly string address; | ||
| private readonly InMemoryOrchestrationService orchestrationService; | ||
| private readonly ILoggerFactory? loggerFactory; | ||
| private IHost? inMemorySidecarHost; | ||
|
|
||
| public InMemoryGrpcSidecarHost( | ||
| string address, | ||
| InMemoryOrchestrationService orchestrationService, | ||
| ILoggerFactory? loggerFactory) | ||
| { | ||
| this.address = address; | ||
| this.orchestrationService = orchestrationService; | ||
| this.loggerFactory = loggerFactory; | ||
| } | ||
|
|
||
| public async Task StartAsync(CancellationToken cancellationToken) | ||
| { | ||
| // Build and start the gRPC sidecar | ||
| this.inMemorySidecarHost = Host.CreateDefaultBuilder() | ||
| .ConfigureLogging(logging => | ||
| { | ||
| logging.ClearProviders(); | ||
| if (this.loggerFactory != null) | ||
| { | ||
| logging.Services.AddSingleton(this.loggerFactory); | ||
| } | ||
| }) | ||
| .ConfigureWebHostDefaults(webBuilder => | ||
| { | ||
| webBuilder.UseUrls(this.address); | ||
| webBuilder.ConfigureKestrel(kestrelOptions => | ||
| { | ||
| kestrelOptions.ConfigureEndpointDefaults(listenOptions => | ||
| listenOptions.Protocols = HttpProtocols.Http2); | ||
| }); | ||
|
|
||
| webBuilder.ConfigureServices(services => | ||
| { | ||
| services.AddGrpc(); | ||
| // Use the SAME orchestration service instance | ||
| services.AddSingleton<IOrchestrationService>(this.orchestrationService); | ||
| services.AddSingleton<IOrchestrationServiceClient>(this.orchestrationService); | ||
| services.AddSingleton<TaskHubGrpcServer>(); | ||
| }); | ||
|
|
||
| webBuilder.Configure(app => | ||
| { | ||
| app.UseRouting(); | ||
| app.UseEndpoints(endpoints => | ||
| { | ||
| endpoints.MapGrpcService<TaskHubGrpcServer>(); | ||
| }); | ||
| }); | ||
| }) | ||
| .Build(); | ||
|
|
||
| await this.inMemorySidecarHost.StartAsync(cancellationToken); | ||
| } | ||
|
|
||
| public async Task StopAsync(CancellationToken cancellationToken) | ||
| { | ||
| if (this.inMemorySidecarHost != null) | ||
| { | ||
| await this.inMemorySidecarHost.StopAsync(cancellationToken); | ||
| } | ||
| } | ||
|
|
||
| public async ValueTask DisposeAsync() | ||
| { | ||
| if (this.inMemorySidecarHost != null) | ||
| { | ||
| this.inMemorySidecarHost.Dispose(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The version bump from 4.12.0 to 4.14.0 for Microsoft.CodeAnalysis.Common appears unrelated to the dependency injection feature being added. This change should either be explained in the PR description or moved to a separate PR to maintain clear change boundaries.