-
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 bug/476_multiple_methods_per_interface_wit…
…h_json_serialisation_doesntwork
- Loading branch information
Showing
21 changed files
with
804 additions
and
78 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(); | ||
``` |
33 changes: 33 additions & 0 deletions
33
examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.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; | ||
|
||
namespace WorkflowExternalInteraction.Activities; | ||
|
||
internal sealed class ApproveActivity : WorkflowActivity<string, bool> | ||
{ | ||
/// <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<bool> RunAsync(WorkflowActivityContext context, string input) | ||
{ | ||
Console.WriteLine($"Workflow {input} is approved"); | ||
Console.WriteLine("Running Approval activity..."); | ||
await Task.Delay(TimeSpan.FromSeconds(5)); | ||
return true; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.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; | ||
|
||
namespace WorkflowExternalInteraction.Activities; | ||
|
||
internal sealed class RejectActivity : WorkflowActivity<string, bool> | ||
{ | ||
/// <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<bool> RunAsync(WorkflowActivityContext context, string input) | ||
{ | ||
Console.WriteLine($"Workflow {input} is rejected"); | ||
Console.WriteLine("Running Reject activity..."); | ||
await Task.Delay(TimeSpan.FromSeconds(5)); | ||
return true; | ||
} | ||
} |
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,76 @@ | ||
// ------------------------------------------------------------------------ | ||
// 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 WorkflowExternalInteraction.Activities; | ||
using WorkflowExternalInteraction.Workflows; | ||
|
||
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => | ||
{ | ||
services.AddDaprWorkflow(options => | ||
{ | ||
options.RegisterWorkflow<DemoWorkflow>(); | ||
options.RegisterActivity<ApproveActivity>(); | ||
options.RegisterActivity<RejectActivity>(); | ||
}); | ||
}); | ||
|
||
using var host = builder.Build(); | ||
await host.StartAsync(); | ||
|
||
await using var scope = host.Services.CreateAsyncScope(); | ||
var daprWorkflowClient = scope.ServiceProvider.GetRequiredService<DaprWorkflowClient>(); | ||
|
||
var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; | ||
|
||
await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, instanceId); | ||
|
||
|
||
bool enterPressed = false; | ||
Console.WriteLine("Press [ENTER] within the next 10 seconds to approve this workflow"); | ||
using (var cts = new CancellationTokenSource()) | ||
{ | ||
var inputTask = Task.Run(() => | ||
{ | ||
if (Console.ReadKey().Key == ConsoleKey.Enter) | ||
{ | ||
Console.WriteLine("Approved"); | ||
enterPressed = true; | ||
cts.Cancel(); //Cancel the delay task if Enter is pressed | ||
} | ||
}); | ||
|
||
try | ||
{ | ||
await Task.Delay(TimeSpan.FromSeconds(10), cts.Token); | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
// Task was cancelled because Enter was pressed | ||
} | ||
} | ||
|
||
if (enterPressed) | ||
{ | ||
await daprWorkflowClient.RaiseEventAsync(instanceId, "Approval", true); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Rejected"); | ||
} | ||
|
||
await daprWorkflowClient.WaitForWorkflowCompletionAsync(instanceId); | ||
var state = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); | ||
Console.WriteLine($"Workflow state: {state.RuntimeStatus}"); |
18 changes: 18 additions & 0 deletions
18
examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.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> | ||
<ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
</ItemGroup> | ||
|
||
</Project> |
46 changes: 46 additions & 0 deletions
46
examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.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,46 @@ | ||
// ------------------------------------------------------------------------ | ||
// 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 WorkflowExternalInteraction.Activities; | ||
|
||
namespace WorkflowExternalInteraction.Workflows; | ||
|
||
internal sealed class DemoWorkflow : Workflow<string, bool> | ||
{ | ||
/// <summary> | ||
/// Override to implement workflow logic. | ||
/// </summary> | ||
/// <param name="context">The workflow context.</param> | ||
/// <param name="input">The deserialized workflow input.</param> | ||
/// <returns>The output of the workflow as a task.</returns> | ||
public override async Task<bool> RunAsync(WorkflowContext context, string input) | ||
{ | ||
try | ||
{ | ||
await context.WaitForExternalEventAsync<bool>(eventName: "Approval", timeout: TimeSpan.FromSeconds(10)); | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
Console.WriteLine("Approval timeout"); | ||
await context.CallActivityAsync(nameof(RejectActivity), input); | ||
Console.WriteLine("Reject Activity finished"); | ||
return false; | ||
} | ||
|
||
await context.CallActivityAsync(nameof(ApproveActivity), input); | ||
Console.WriteLine("Approve Activity finished"); | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.