Skip to content
Merged
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
44 changes: 44 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,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.
Comment thread
Quinn-With-Two-Ns marked this conversation as resolved.
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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