-
Notifications
You must be signed in to change notification settings - Fork 323
Add DI instructions for dotnet Nexus #4911
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,6 +285,49 @@ async Task RunHandlerWorkerAsync() | |
| } | ||
| ``` | ||
|
|
||
| ### Use dependency injection with a Nexus Service handler {/* #dependency-injection */} | ||
|
|
||
| Nexus Service handlers support dependency injection through the [Temporalio.Extensions.Hosting](https://github.com/temporalio/sdk-dotnet/tree/main/src/Temporalio.Extensions.Hosting) generic-host Worker. | ||
| Register the handler on the Worker with `AddScopedNexusService`, and the container injects the handler's constructor dependencies. | ||
| Use `AddSingletonNexusService` or `AddTransientNexusService` for singleton or transient lifetimes instead, mirroring `AddScopedActivities` / `AddSingletonActivities` / `AddTransientActivities`. | ||
|
|
||
| For a complete, runnable example, see the [NexusDependencyInjection sample](https://github.com/temporalio/samples-dotnet/tree/main/src/NexusDependencyInjection). | ||
|
|
||
| [NexusDependencyInjection/Program.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusDependencyInjection/Program.cs) | ||
| ```csharp | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of the code samples should be made snipsync in the features repo. |
||
| IHost host = Host.CreateDefaultBuilder(args) | ||
| .ConfigureServices(ctx => | ||
| ctx. | ||
| // Add the dependency that will be injected into the Nexus Service handler | ||
| AddScoped<IGreetingClient, GreetingClient>(). | ||
| // Add the worker | ||
| AddHostedTemporalWorker(handlerTaskQueue). | ||
| ConfigureOptions(options => options.ClientOptions = LoadConnectOptions()). | ||
| // Add the Nexus Service handler at the scoped level | ||
| AddScopedNexusService<GreetingServiceHandler>()) | ||
| .Build(); | ||
| await host.RunAsync(); | ||
| ``` | ||
|
|
||
| The handler receives its dependencies through its constructor, and a scoped instance is created for each Operation: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Maybe could be more precise here. I think it's better to say that it's created for each operation invocation. Because the question is wether an operation instance is cached or not (and it is not, since it is recreated for each invocation; and that is the desired behavior for scoped services). Because, hypothetically, we could cache the operation instances for single registrations and not have to pay for construction on each invocation. |
||
|
|
||
| [NexusDependencyInjection/Handler/GreetingServiceHandler.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusDependencyInjection/Handler/GreetingServiceHandler.cs) | ||
| ```csharp | ||
| [NexusServiceHandler(typeof(IGreetingService))] | ||
| public class GreetingServiceHandler | ||
| { | ||
| private readonly IGreetingClient greetingClient; | ||
|
|
||
| // The dependency is injected by the container | ||
| public GreetingServiceHandler(IGreetingClient greetingClient) => this.greetingClient = greetingClient; | ||
|
|
||
| [NexusOperationHandler] | ||
| public IOperationHandler<IGreetingService.SayHelloInput, string> SayHello() => | ||
| OperationHandler.Sync<IGreetingService.SayHelloInput, string>( | ||
| (ctx, input) => greetingClient.GetGreetingAsync(input.Name)); | ||
| } | ||
| ``` | ||
|
|
||
| ## Develop a caller Workflow that uses the Nexus Service {/* #develop-caller-workflow-nexus-service */} | ||
|
|
||
| Import the Service API package that has the necessary service and operation names and input/output types to execute a Nexus Operation from the caller Workflow: | ||
|
|
||
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.
This reads as if it works with the page's existing prerequisite of Temporal .NET SDK v1.9.0+, but
AddScopedNexusServiceis not present inTemporalio.Extensions.Hosting1.9.0 and the Nexus service DI helpers appear in 1.13.0+. Users on v1.9-v1.12, which the page still allows, will follow this section and hit a missing-method compile error, so please add the required SDK/Extensions.Hosting version here or update the prerequisite.Useful? React with 👍 / 👎.