-
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.
Merge branch 'master' into actor-source-gen
- Loading branch information
Showing
47 changed files
with
1,660 additions
and
80 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
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(); | ||
``` |
33 changes: 33 additions & 0 deletions
33
examples/Workflow/WorkflowAsyncOperations/Activities/NotifyWarehouseActivity.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,33 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2024 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
|
||
using Dapr.Workflow; | ||
using WorkflowAsyncOperations.Models; | ||
|
||
namespace WorkflowAsyncOperations.Activities; | ||
|
||
internal sealed class NotifyWarehouseActivity : WorkflowActivity<Transaction, object?> | ||
{ | ||
/// <summary> | ||
/// Override to implement async (non-blocking) workflow activity logic. | ||
/// </summary> | ||
/// <param name="context">Provides access to additional context for the current activity execution.</param> | ||
/// <param name="input">The deserialized activity input.</param> | ||
/// <returns>The output of the activity as a task.</returns> | ||
public override async Task<object?> RunAsync(WorkflowActivityContext context, Transaction input) | ||
{ | ||
//Contact the warehouse to ship the product | ||
await Task.Delay(TimeSpan.FromSeconds(8)); | ||
return null; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
examples/Workflow/WorkflowAsyncOperations/Activities/ProcessPaymentActivity.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,33 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2024 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
|
||
using Dapr.Workflow; | ||
using WorkflowAsyncOperations.Models; | ||
|
||
namespace WorkflowAsyncOperations.Activities; | ||
|
||
internal sealed class ProcessPaymentActivity : WorkflowActivity<Transaction, object?> | ||
{ | ||
/// <summary> | ||
/// Override to implement async (non-blocking) workflow activity logic. | ||
/// </summary> | ||
/// <param name="context">Provides access to additional context for the current activity execution.</param> | ||
/// <param name="input">The deserialized activity input.</param> | ||
/// <returns>The output of the activity as a task.</returns> | ||
public override async Task<object?> RunAsync(WorkflowActivityContext context, Transaction input) | ||
{ | ||
//Confirm payment with processor | ||
await Task.Delay(TimeSpan.FromSeconds(10)); | ||
return null; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
examples/Workflow/WorkflowAsyncOperations/Models/Transaction.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,19 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2024 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
|
||
namespace WorkflowAsyncOperations.Models; | ||
|
||
internal sealed record Transaction(decimal Value) | ||
{ | ||
public Guid CustomerId { get; init; } = Guid.NewGuid(); | ||
} |
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,48 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2024 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
|
||
using Dapr.Workflow; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using WorkflowAsyncOperations.Activities; | ||
using WorkflowAsyncOperations.Models; | ||
using WorkflowAsyncOperations.Workflows; | ||
|
||
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => | ||
{ | ||
services.AddDaprWorkflow(options => | ||
{ | ||
options.RegisterWorkflow<DemoWorkflow>(); | ||
options.RegisterActivity<ProcessPaymentActivity>(); | ||
options.RegisterActivity<NotifyWarehouseActivity>(); | ||
}); | ||
}); | ||
|
||
var host = await builder.StartAsync(); | ||
|
||
await using var scope = host.Services.CreateAsyncScope(); | ||
var daprWorkflowClient = scope.ServiceProvider.GetRequiredService<DaprWorkflowClient>(); | ||
|
||
var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; | ||
var transaction = new Transaction(16.58m); | ||
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, transaction); | ||
|
||
//Poll for status updates every second | ||
var status = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); | ||
do | ||
{ | ||
Console.WriteLine($"Current status: {status.RuntimeStatus}, step: {status.ReadCustomStatusAs<string>()}"); | ||
status = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); | ||
} while (!status.IsWorkflowCompleted); | ||
|
||
Console.WriteLine($"Workflow completed - {status.ReadCustomStatusAs<string>()}"); |
18 changes: 18 additions & 0 deletions
18
examples/Workflow/WorkflowAsyncOperations/WorkflowAsyncOperations.csproj
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,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.