Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/develop/dotnet/nexus/feature-guide.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge State the SDK version that adds Nexus DI

This reads as if it works with the page's existing prerequisite of Temporal .NET SDK v1.9.0+, but AddScopedNexusService is not present in Temporalio.Extensions.Hosting 1.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 👍 / 👎.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and a scoped instance is created for each Operation

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:
Expand Down