-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 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 80fff43
udpate pkg ver
nytian 1e7eac6
address coopilot feedback
nytian b9aacbd
fix small typo
nytian 05ee02d
update
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,193 @@ | ||
| // 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> | ||
| /// 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 DI container. | ||
nytian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 DI container. | ||
nytian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </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 | ||
nytian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| /// <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 class InMemoryGrpcSidecarHost : IHostedService, IAsyncDisposable | ||
nytian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| private readonly string address; | ||
| private readonly InMemoryOrchestrationService orchestrationService; | ||
| private readonly ILoggerFactory? loggerFactory; | ||
| private IHost? sidecarHost; | ||
nytian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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.sidecarHost = 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.sidecarHost.StartAsync(cancellationToken); | ||
| } | ||
|
|
||
| public async Task StopAsync(CancellationToken cancellationToken) | ||
| { | ||
| if (this.sidecarHost != null) | ||
| { | ||
| await this.sidecarHost.StopAsync(cancellationToken); | ||
| } | ||
| } | ||
|
|
||
| public async ValueTask DisposeAsync() | ||
| { | ||
| if (this.sidecarHost != null) | ||
| { | ||
| this.sidecarHost.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
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.
Uh oh!
There was an error while loading. Please reload this page.