Skip to content

Commit c527066

Browse files
authored
Merge branch 'main' into extstore/initial-ts-docs
2 parents eebcb49 + 7e6e2e8 commit c527066

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,50 @@ 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.
313+
The container creates a new scoped handler instance and its scoped dependencies for each Operation invocation; it does not cache them between invocations:
314+
315+
[NexusDependencyInjection/Handler/GreetingServiceHandler.cs](https://github.com/temporalio/samples-dotnet/blob/main/src/NexusDependencyInjection/Handler/GreetingServiceHandler.cs)
316+
```csharp
317+
[NexusServiceHandler(typeof(IGreetingService))]
318+
public class GreetingServiceHandler
319+
{
320+
private readonly IGreetingClient greetingClient;
321+
322+
// The dependency is injected by the container
323+
public GreetingServiceHandler(IGreetingClient greetingClient) => this.greetingClient = greetingClient;
324+
325+
[NexusOperationHandler]
326+
public IOperationHandler<IGreetingService.SayHelloInput, string> SayHello() =>
327+
OperationHandler.Sync<IGreetingService.SayHelloInput, string>(
328+
(ctx, input) => greetingClient.GetGreetingAsync(input.Name));
329+
}
330+
```
331+
288332
## Develop a caller Workflow that uses the Nexus Service {/* #develop-caller-workflow-nexus-service */}
289333

290334
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)