-
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
Merged
+44
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,6 +285,50 @@ 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.
Contributor
Author
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. I copied what other dotnet docs did here |
||
| 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. | ||
| The container creates a new scoped handler instance and its scoped dependencies for each Operation invocation; it does not cache them between invocations: | ||
|
|
||
| [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: | ||
|
|
||
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.