Skip to content

Commit 8e8885f

Browse files
Add DI instructions for dotnet Nexus
1 parent e82e0b0 commit 8e8885f

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

docs/develop/dotnet/nexus/feature-guide.mdx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,49 @@ async Task RunHandlerWorkerAsync()
285285
}
286286
```
287287

288+
### Use dependency injection with a Nexus Service handler {/* #dependency-injection */}
289+
290+
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.
291+
Register the handler on the Worker with `AddScopedNexusService`, and the container injects the handler's constructor dependencies.
292+
Use `AddSingletonNexusService` or `AddTransientNexusService` for singleton or transient lifetimes instead, mirroring `AddScopedActivities` / `AddSingletonActivities` / `AddTransientActivities`.
293+
294+
For a complete, runnable example, see the [NexusDependencyInjection sample](https://github.com/temporalio/samples-dotnet/tree/main/src/NexusDependencyInjection).
295+
296+
[NexusDependencyInjection/Program.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusDependencyInjection/Program.cs)
297+
```csharp
298+
IHost host = Host.CreateDefaultBuilder(args)
299+
.ConfigureServices(ctx =>
300+
ctx.
301+
// Add the dependency that will be injected into the Nexus Service handler
302+
AddScoped<IGreetingClient, GreetingClient>().
303+
// Add the worker
304+
AddHostedTemporalWorker(handlerTaskQueue).
305+
ConfigureOptions(options => options.ClientOptions = LoadConnectOptions()).
306+
// Add the Nexus Service handler at the scoped level
307+
AddScopedNexusService<GreetingServiceHandler>())
308+
.Build();
309+
await host.RunAsync();
310+
```
311+
312+
The handler receives its dependencies through its constructor, and a scoped instance is created for each Operation:
313+
314+
[NexusDependencyInjection/Handler/GreetingServiceHandler.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusDependencyInjection/Handler/GreetingServiceHandler.cs)
315+
```csharp
316+
[NexusServiceHandler(typeof(IGreetingService))]
317+
public class GreetingServiceHandler
318+
{
319+
private readonly IGreetingClient greetingClient;
320+
321+
// The dependency is injected by the container
322+
public GreetingServiceHandler(IGreetingClient greetingClient) => this.greetingClient = greetingClient;
323+
324+
[NexusOperationHandler]
325+
public IOperationHandler<IGreetingService.SayHelloInput, string> SayHello() =>
326+
OperationHandler.Sync<IGreetingService.SayHelloInput, string>(
327+
(ctx, input) => greetingClient.GetGreetingAsync(input.Name));
328+
}
329+
```
330+
288331
## Develop a caller Workflow that uses the Nexus Service {/* #develop-caller-workflow-nexus-service */}
289332

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

0 commit comments

Comments
 (0)