-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optional DI lifecycle change (#1408)
* Added mechanism to allow the service lifetime to be overridden from a singleton (default) to another lifetime Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Added unit tests - updated dependencies accordingly Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Added service lifetime to DaprClient as well Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Added update to DaprClient to pass service lifetime through Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Added documentation indicating how to register DaprWorkflowClient with different lifecycle options. Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Removed unnecessary line from csproj Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Simplified registrations Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Called out an important point about registrations Signed-off-by: Whit Waldo <whit.waldo@innovian.net> --------- Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
- Loading branch information
Showing
8 changed files
with
238 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
77 changes: 77 additions & 0 deletions
77
daprdocs/content/en/dotnet-sdk-docs/dotnet-workflow/dotnet-workflowclient-usage.md
This file contains 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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--- | ||
type: docs | ||
title: "DaprWorkflowClient usage" | ||
linkTitle: "DaprWorkflowClient usage" | ||
weight: 100000 | ||
description: Essential tips and advice for using DaprWorkflowClient | ||
--- | ||
|
||
## Lifetime management | ||
|
||
A `DaprWorkflowClient` holds access to networking resources in the form of TCP sockets used to communicate with the Dapr sidecar as well | ||
as other types used in the management and operation of Workflows. `DaprWorkflowClient` implements `IAsyncDisposable` to support eager | ||
cleanup of resources. | ||
|
||
## Dependency Injection | ||
|
||
The `AddDaprWorkflow()` method will register the Dapr workflow services with ASP.NET Core dependency injection. This method | ||
requires an options delegate that defines each of the workflows and activities you wish to register and use in your application. | ||
|
||
{{% alert title="Note" color="primary" %}} | ||
|
||
This method will attempt to register a `DaprClient` instance, but this will only work if it hasn't already been registered with another | ||
lifetime. For example, an earlier call to `AddDaprClient()` with a singleton lifetime will always use a singleton regardless of the | ||
lifetime chose for the workflow client. The `DaprClient` instance will be used to communicate with the Dapr sidecar and if it's not | ||
yet registered, the lifetime provided during the `AddDaprWorkflow()` registration will be used to register the `DaprWorkflowClient` | ||
as well as its own dependencies. | ||
|
||
{{% /alert %}} | ||
|
||
### Singleton Registration | ||
By default, the `AddDaprWorkflow` method will register the `DaprWorkflowClient` and associated services using a singleton lifetime. This means | ||
that the services will be instantiated only a single time. | ||
|
||
The following is an example of how registration of the `DaprWorkflowClient` as it would appear in a typical `Program.cs` file: | ||
|
||
```csharp | ||
builder.Services.AddDaprWorkflow(options => { | ||
options.RegisterWorkflow<YourWorkflow>(); | ||
options.RegisterActivity<YourActivity>(); | ||
}); | ||
|
||
var app = builder.Build(); | ||
await app.RunAsync(); | ||
``` | ||
|
||
### Scoped Registration | ||
|
||
While this may generally be acceptable in your use case, you may instead wish to override the lifetime specified. This is done by passing a `ServiceLifetime` | ||
argument in `AddDaprWorkflow`. For example, you may wish to inject another scoped service into your ASP.NET Core processing pipeline | ||
that needs context used by the `DaprClient` that wouldn't be available if the former service were registered as a singleton. | ||
|
||
This is demonstrated in the following example: | ||
|
||
```csharp | ||
builder.Services.AddDaprWorkflow(options => { | ||
options.RegisterWorkflow<YourWorkflow>(); | ||
options.RegisterActivity<YourActivity>(); | ||
}, ServiceLifecycle.Scoped); | ||
|
||
var app = builder.Build(); | ||
await app.RunAsync(); | ||
``` | ||
|
||
### Transient Registration | ||
|
||
Finally, Dapr services can also be registered using a transient lifetime meaning that they will be initialized every time they're injected. This | ||
is demonstrated in the following example: | ||
|
||
```csharp | ||
builder.Services.AddDaprWorkflow(options => { | ||
options.RegisterWorkflow<YourWorkflow>(); | ||
options.RegisterActivity<YourActivity>(); | ||
}, ServiceLifecycle.Transient); | ||
|
||
var app = builder.Build(); | ||
await app.RunAsync(); | ||
``` |
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.extensibility.core" /> | ||
<PackageReference Include="xunit.runner.visualstudio" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
58 changes: 58 additions & 0 deletions
58
test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Dapr.Workflow.Test; | ||
|
||
public class WorkflowServiceCollectionExtensionsTests | ||
{ | ||
[Fact] | ||
public void RegisterWorkflowClient_ShouldRegisterSingleton_WhenLifetimeIsSingleton() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
services.AddDaprWorkflow(options => { }, ServiceLifetime.Singleton); | ||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var daprWorkflowClient1 = serviceProvider.GetService<DaprWorkflowClient>(); | ||
var daprWorkflowClient2 = serviceProvider.GetService<DaprWorkflowClient>(); | ||
|
||
Assert.NotNull(daprWorkflowClient1); | ||
Assert.NotNull(daprWorkflowClient2); | ||
|
||
Assert.Same(daprWorkflowClient1, daprWorkflowClient2); | ||
} | ||
|
||
[Fact] | ||
public async Task RegisterWorkflowClient_ShouldRegisterScoped_WhenLifetimeIsScoped() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
services.AddDaprWorkflow(options => { }, ServiceLifetime.Scoped); | ||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
await using var scope1 = serviceProvider.CreateAsyncScope(); | ||
var daprWorkflowClient1 = scope1.ServiceProvider.GetService<DaprWorkflowClient>(); | ||
|
||
await using var scope2 = serviceProvider.CreateAsyncScope(); | ||
var daprWorkflowClient2 = scope2.ServiceProvider.GetService<DaprWorkflowClient>(); | ||
|
||
Assert.NotNull(daprWorkflowClient1); | ||
Assert.NotNull(daprWorkflowClient2); | ||
Assert.NotSame(daprWorkflowClient1, daprWorkflowClient2); | ||
} | ||
|
||
[Fact] | ||
public void RegisterWorkflowClient_ShouldRegisterTransient_WhenLifetimeIsTransient() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
services.AddDaprWorkflow(options => { }, ServiceLifetime.Transient); | ||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var daprWorkflowClient1 = serviceProvider.GetService<DaprWorkflowClient>(); | ||
var daprWorkflowClient2 = serviceProvider.GetService<DaprWorkflowClient>(); | ||
|
||
Assert.NotNull(daprWorkflowClient1); | ||
Assert.NotNull(daprWorkflowClient2); | ||
Assert.NotSame(daprWorkflowClient1, daprWorkflowClient2); | ||
} | ||
} |