From 6e2841a14a6435593a6a394f3a4fa31e82e0659e Mon Sep 17 00:00:00 2001 From: Mike Nguyen Date: Tue, 12 Nov 2024 17:03:12 +0000 Subject: [PATCH 1/9] ci: set fail-fast to false (#1405) Signed-off-by: Mike Nguyen --- .github/workflows/itests.yml | 1 + .github/workflows/sdk_build.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/itests.yml b/.github/workflows/itests.yml index 36741ce7c..d06c12cd5 100644 --- a/.github/workflows/itests.yml +++ b/.github/workflows/itests.yml @@ -18,6 +18,7 @@ jobs: name: run integration tests runs-on: ubuntu-latest strategy: + fail-fast: false matrix: dotnet-version: ['6.0', '7.0', '8.0'] include: diff --git a/.github/workflows/sdk_build.yml b/.github/workflows/sdk_build.yml index fe935bfb8..5e6fd3532 100644 --- a/.github/workflows/sdk_build.yml +++ b/.github/workflows/sdk_build.yml @@ -41,6 +41,7 @@ jobs: name: Test .NET ${{ matrix.dotnet-version }} runs-on: ubuntu-latest strategy: + fail-fast: false matrix: dotnet-version: ['6.0', '7.0', '8.0'] include: From 74a98111dc6514251893d1cf4c23cbec0b292bf2 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Tue, 12 Nov 2024 14:08:21 -0700 Subject: [PATCH 2/9] Added async operations workflow sample (#1394) Signed-off-by: Whit Waldo --- Directory.Packages.props | 5 +- all.sln | 10 +++- .../Activities/NotifyWarehouseActivity.cs | 33 ++++++++++++ .../Activities/ProcessPaymentActivity.cs | 33 ++++++++++++ .../Models/Transaction.cs | 19 +++++++ .../WorkflowAsyncOperations/Program.cs | 48 +++++++++++++++++ .../WorkflowAsyncOperations.csproj | 18 +++++++ .../Workflows/DemoWorkflow.cs | 52 +++++++++++++++++++ 8 files changed, 215 insertions(+), 3 deletions(-) create mode 100644 examples/Workflow/WorkflowAsyncOperations/Activities/NotifyWarehouseActivity.cs create mode 100644 examples/Workflow/WorkflowAsyncOperations/Activities/ProcessPaymentActivity.cs create mode 100644 examples/Workflow/WorkflowAsyncOperations/Models/Transaction.cs create mode 100644 examples/Workflow/WorkflowAsyncOperations/Program.cs create mode 100644 examples/Workflow/WorkflowAsyncOperations/WorkflowAsyncOperations.csproj create mode 100644 examples/Workflow/WorkflowAsyncOperations/Workflows/DemoWorkflow.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index e5e60cd7b..772dd7c6e 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -29,10 +29,11 @@ - + + + - diff --git a/all.sln b/all.sln index 3b9959902..34f0f6fdb 100644 --- a/all.sln +++ b/all.sln @@ -119,11 +119,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Com EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowAsyncOperations", "examples\Workflow\WorkflowAsyncOperations\WorkflowAsyncOperations.csproj", "{00359961-0C50-4BB1-A794-8B06DE991639}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Messaging.Test", "test\Dapr.Messaging.Test\Dapr.Messaging.Test.csproj", "{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Messaging", "src\Dapr.Messaging\Dapr.Messaging.csproj", "{0EAE36A1-B578-4F13-A113-7A477ECA1BDA}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamingSubscriptionExample", "examples\Client\PublishSubscribe\StreamingSubscriptionExample\StreamingSubscriptionExample.csproj", "{290D1278-F613-4DF3-9DF5-F37E38CDC363}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Jobs", "src\Dapr.Jobs\Dapr.Jobs.csproj", "{C8BB6A85-A7EA-40C0-893D-F36F317829B3}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Jobs.Test", "test\Dapr.Jobs.Test\Dapr.Jobs.Test.csproj", "{BF9828E9-5597-4D42-AA6E-6E6C12214204}" @@ -316,6 +319,10 @@ Global {CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU + {00359961-0C50-4BB1-A794-8B06DE991639}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {00359961-0C50-4BB1-A794-8B06DE991639}.Debug|Any CPU.Build.0 = Debug|Any CPU + {00359961-0C50-4BB1-A794-8B06DE991639}.Release|Any CPU.ActiveCfg = Release|Any CPU + {00359961-0C50-4BB1-A794-8B06DE991639}.Release|Any CPU.Build.0 = Release|Any CPU {4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}.Debug|Any CPU.Build.0 = Debug|Any CPU {4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -327,7 +334,7 @@ Global {290D1278-F613-4DF3-9DF5-F37E38CDC363}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {290D1278-F613-4DF3-9DF5-F37E38CDC363}.Debug|Any CPU.Build.0 = Debug|Any CPU {290D1278-F613-4DF3-9DF5-F37E38CDC363}.Release|Any CPU.ActiveCfg = Release|Any CPU - {290D1278-F613-4DF3-9DF5-F37E38CDC363}.Release|Any CPU.Build.0 = Release|Any CPU + {290D1278-F613-4DF3-9DF5-F37E38CDC363}.Release|Any CPU.Build.0 = Release|Any CP {C8BB6A85-A7EA-40C0-893D-F36F317829B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {C8BB6A85-A7EA-40C0-893D-F36F317829B3}.Debug|Any CPU.Build.0 = Debug|Any CPU {C8BB6A85-A7EA-40C0-893D-F36F317829B3}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -396,6 +403,7 @@ Global {DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B} + {00359961-0C50-4BB1-A794-8B06DE991639} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {4E04EB35-7FD2-4FDB-B09A-F75CE24053B9} = {DD020B34-460F-455F-8D17-CF4A949F100B} {0EAE36A1-B578-4F13-A113-7A477ECA1BDA} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {290D1278-F613-4DF3-9DF5-F37E38CDC363} = {0EF6EA64-D7C3-420D-9890-EAE8D54A57E6} diff --git a/examples/Workflow/WorkflowAsyncOperations/Activities/NotifyWarehouseActivity.cs b/examples/Workflow/WorkflowAsyncOperations/Activities/NotifyWarehouseActivity.cs new file mode 100644 index 000000000..2088ff810 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/Activities/NotifyWarehouseActivity.cs @@ -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 +{ + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override async Task RunAsync(WorkflowActivityContext context, Transaction input) + { + //Contact the warehouse to ship the product + await Task.Delay(TimeSpan.FromSeconds(8)); + return null; + } +} diff --git a/examples/Workflow/WorkflowAsyncOperations/Activities/ProcessPaymentActivity.cs b/examples/Workflow/WorkflowAsyncOperations/Activities/ProcessPaymentActivity.cs new file mode 100644 index 000000000..1cd658899 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/Activities/ProcessPaymentActivity.cs @@ -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 +{ + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override async Task RunAsync(WorkflowActivityContext context, Transaction input) + { + //Confirm payment with processor + await Task.Delay(TimeSpan.FromSeconds(10)); + return null; + } +} diff --git a/examples/Workflow/WorkflowAsyncOperations/Models/Transaction.cs b/examples/Workflow/WorkflowAsyncOperations/Models/Transaction.cs new file mode 100644 index 000000000..082c9564e --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/Models/Transaction.cs @@ -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(); +} diff --git a/examples/Workflow/WorkflowAsyncOperations/Program.cs b/examples/Workflow/WorkflowAsyncOperations/Program.cs new file mode 100644 index 000000000..df46d1f9b --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/Program.cs @@ -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(); + options.RegisterActivity(); + options.RegisterActivity(); + }); +}); + +var host = await builder.StartAsync(); + +await using var scope = host.Services.CreateAsyncScope(); +var daprWorkflowClient = scope.ServiceProvider.GetRequiredService(); + +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()}"); + status = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); +} while (!status.IsWorkflowCompleted); + +Console.WriteLine($"Workflow completed - {status.ReadCustomStatusAs()}"); diff --git a/examples/Workflow/WorkflowAsyncOperations/WorkflowAsyncOperations.csproj b/examples/Workflow/WorkflowAsyncOperations/WorkflowAsyncOperations.csproj new file mode 100644 index 000000000..a1350fa79 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/WorkflowAsyncOperations.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/examples/Workflow/WorkflowAsyncOperations/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowAsyncOperations/Workflows/DemoWorkflow.cs new file mode 100644 index 000000000..7d70ca632 --- /dev/null +++ b/examples/Workflow/WorkflowAsyncOperations/Workflows/DemoWorkflow.cs @@ -0,0 +1,52 @@ +// ------------------------------------------------------------------------ +// 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.Activities; +using WorkflowAsyncOperations.Models; + +namespace WorkflowAsyncOperations.Workflows; + +internal sealed class DemoWorkflow : Workflow +{ + /// + /// Override to implement workflow logic. + /// + /// The workflow context. + /// The deserialized workflow input. + /// The output of the workflow as a task. + public override async Task RunAsync(WorkflowContext context, Transaction input) + { + try + { + //Submit the transaction to the payment processor + context.SetCustomStatus("Processing payment..."); + await context.CallActivityAsync(nameof(ProcessPaymentActivity), input); + + + //Send the transaction details to the warehouse + context.SetCustomStatus("Contacting warehouse..."); + await context.CallActivityAsync(nameof(NotifyWarehouseActivity), input); + + context.SetCustomStatus("Success!"); + return true; + } + catch + { + //If anything goes wrong, return false + context.SetCustomStatus("Something went wrong"); + return false; + } + } +} + From f3979ec08005af8138c8395018bd25a2a59675fc Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Wed, 13 Nov 2024 12:34:02 -0700 Subject: [PATCH 3/9] Added workflow example: Fan out/fan in (#1396) * Added workflow fan out/fan in example Signed-off-by: Whit Waldo * Added copyright headers Signed-off-by: Whit Waldo --------- Signed-off-by: Whit Waldo --- all.sln | 7 ++++ .../Activities/NotifyActivity.cs | 31 ++++++++++++++ .../Workflow/WorkflowFanOutFanIn/Program.cs | 40 +++++++++++++++++++ .../WorkflowFanOutFanIn.csproj | 18 +++++++++ .../Workflows/DemoWorkflow.cs | 40 +++++++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs create mode 100644 examples/Workflow/WorkflowFanOutFanIn/Program.cs create mode 100644 examples/Workflow/WorkflowFanOutFanIn/WorkflowFanOutFanIn.csproj create mode 100644 examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs diff --git a/all.sln b/all.sln index 34f0f6fdb..81f41d4d6 100644 --- a/all.sln +++ b/all.sln @@ -119,6 +119,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Com EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowFanOutFanIn", "examples\Workflow\WorkflowFanOutFanIn\WorkflowFanOutFanIn.csproj", "{D83B27F3-4401-42F5-843E-147566B4999A}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowAsyncOperations", "examples\Workflow\WorkflowAsyncOperations\WorkflowAsyncOperations.csproj", "{00359961-0C50-4BB1-A794-8B06DE991639}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Messaging.Test", "test\Dapr.Messaging.Test\Dapr.Messaging.Test.csproj", "{4E04EB35-7FD2-4FDB-B09A-F75CE24053B9}" @@ -319,6 +321,10 @@ Global {CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU + {D83B27F3-4401-42F5-843E-147566B4999A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D83B27F3-4401-42F5-843E-147566B4999A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D83B27F3-4401-42F5-843E-147566B4999A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D83B27F3-4401-42F5-843E-147566B4999A}.Release|Any CPU.Build.0 = Release|Any CPU {00359961-0C50-4BB1-A794-8B06DE991639}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {00359961-0C50-4BB1-A794-8B06DE991639}.Debug|Any CPU.Build.0 = Debug|Any CPU {00359961-0C50-4BB1-A794-8B06DE991639}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -403,6 +409,7 @@ Global {DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B} + {D83B27F3-4401-42F5-843E-147566B4999A} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {00359961-0C50-4BB1-A794-8B06DE991639} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {4E04EB35-7FD2-4FDB-B09A-F75CE24053B9} = {DD020B34-460F-455F-8D17-CF4A949F100B} {0EAE36A1-B578-4F13-A113-7A477ECA1BDA} = {27C5D71D-0721-4221-9286-B94AB07B58CF} diff --git a/examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs b/examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs new file mode 100644 index 000000000..88a5c7270 --- /dev/null +++ b/examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs @@ -0,0 +1,31 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowFanOutFanIn.Activities; + +internal sealed class NotifyActivity : WorkflowActivity +{ + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override Task RunAsync(WorkflowActivityContext context, string input) + { + Console.WriteLine(input); + return Task.FromResult(null); + } +} diff --git a/examples/Workflow/WorkflowFanOutFanIn/Program.cs b/examples/Workflow/WorkflowFanOutFanIn/Program.cs new file mode 100644 index 000000000..16d018ab0 --- /dev/null +++ b/examples/Workflow/WorkflowFanOutFanIn/Program.cs @@ -0,0 +1,40 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowFanOutFanIn.Activities; +using WorkflowFanOutFanIn.Workflows; + +var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => +{ + services.AddDaprWorkflow(options => + { + options.RegisterWorkflow(); + options.RegisterActivity(); + }); +}); + +var host = builder.Build(); +await host.StartAsync(); + +await using var scope = host.Services.CreateAsyncScope(); +var daprWorkflowClient = scope.ServiceProvider.GetRequiredService(); + +var instanceId = $"workflow-demo-{Guid.NewGuid().ToString()[..8]}"; +await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, "test input"); + +await daprWorkflowClient.WaitForWorkflowCompletionAsync(instanceId); +var state = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); +Console.WriteLine($"Workflow state: {state.RuntimeStatus}"); diff --git a/examples/Workflow/WorkflowFanOutFanIn/WorkflowFanOutFanIn.csproj b/examples/Workflow/WorkflowFanOutFanIn/WorkflowFanOutFanIn.csproj new file mode 100644 index 000000000..af3a1b2c8 --- /dev/null +++ b/examples/Workflow/WorkflowFanOutFanIn/WorkflowFanOutFanIn.csproj @@ -0,0 +1,18 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + + + diff --git a/examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs new file mode 100644 index 000000000..43a5b42b3 --- /dev/null +++ b/examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs @@ -0,0 +1,40 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowFanOutFanIn.Activities; + +namespace WorkflowFanOutFanIn.Workflows; + +public sealed class DemoWorkflow : Workflow +{ + /// + /// Override to implement workflow logic. + /// + /// The workflow context. + /// The deserialized workflow input. + /// The output of the workflow as a task. + public override async Task RunAsync(WorkflowContext context, string input) + { + var tasks = new List(); + for (var a = 1; a <= 3; a++) + { + var task = context.CallActivityAsync(nameof(NotifyActivity), $"calling task {a}"); + tasks.Add(task); + } + + await Task.WhenAll(tasks); + + return "Workflow completed"; + } +} From 651e5c74cb7fa710012916b574823dfdce8661fd Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Thu, 14 Nov 2024 10:04:49 -0700 Subject: [PATCH 4/9] Added workflow sample: Sub-workflows (#1395) * Added Workflow with sub-workflow Signed-off-by: Whit Waldo * Removed duplicate package version reference Signed-off-by: Whit Waldo --------- Signed-off-by: Whit Waldo --- all.sln | 7 ++++ .../Workflow/WorkflowSubworkflow/Program.cs | 42 +++++++++++++++++++ .../WorkflowSubworkflow.csproj | 18 ++++++++ .../Workflows/DemoSubWorkflow.cs | 33 +++++++++++++++ .../Workflows/DemoWorkflow.cs | 34 +++++++++++++++ 5 files changed, 134 insertions(+) create mode 100644 examples/Workflow/WorkflowSubworkflow/Program.cs create mode 100644 examples/Workflow/WorkflowSubworkflow/WorkflowSubworkflow.csproj create mode 100644 examples/Workflow/WorkflowSubworkflow/Workflows/DemoSubWorkflow.cs create mode 100644 examples/Workflow/WorkflowSubworkflow/Workflows/DemoWorkflow.cs diff --git a/all.sln b/all.sln index 81f41d4d6..26262176d 100644 --- a/all.sln +++ b/all.sln @@ -119,6 +119,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Com EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowSubworkflow", "examples\Workflow\WorkflowSubworkflow\WorkflowSubworkflow.csproj", "{FD3E9371-3134-4235-8E80-32226DFB4B1F}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowFanOutFanIn", "examples\Workflow\WorkflowFanOutFanIn\WorkflowFanOutFanIn.csproj", "{D83B27F3-4401-42F5-843E-147566B4999A}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowAsyncOperations", "examples\Workflow\WorkflowAsyncOperations\WorkflowAsyncOperations.csproj", "{00359961-0C50-4BB1-A794-8B06DE991639}" @@ -321,6 +323,10 @@ Global {CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU + {FD3E9371-3134-4235-8E80-32226DFB4B1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FD3E9371-3134-4235-8E80-32226DFB4B1F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FD3E9371-3134-4235-8E80-32226DFB4B1F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FD3E9371-3134-4235-8E80-32226DFB4B1F}.Release|Any CPU.Build.0 = Release|Any CPU {D83B27F3-4401-42F5-843E-147566B4999A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D83B27F3-4401-42F5-843E-147566B4999A}.Debug|Any CPU.Build.0 = Debug|Any CPU {D83B27F3-4401-42F5-843E-147566B4999A}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -409,6 +415,7 @@ Global {DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B} + {FD3E9371-3134-4235-8E80-32226DFB4B1F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {D83B27F3-4401-42F5-843E-147566B4999A} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {00359961-0C50-4BB1-A794-8B06DE991639} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {4E04EB35-7FD2-4FDB-B09A-F75CE24053B9} = {DD020B34-460F-455F-8D17-CF4A949F100B} diff --git a/examples/Workflow/WorkflowSubworkflow/Program.cs b/examples/Workflow/WorkflowSubworkflow/Program.cs new file mode 100644 index 000000000..b3e71537f --- /dev/null +++ b/examples/Workflow/WorkflowSubworkflow/Program.cs @@ -0,0 +1,42 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowSubWorkflow.Workflows; + +var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => +{ + services.AddDaprWorkflow(options => + { + options.RegisterWorkflow(); + options.RegisterWorkflow(); + }); +}); + +using var host = builder.Build(); +await host.StartAsync(); + +await using var scope = host.Services.CreateAsyncScope(); +var daprWorkflowClient = scope.ServiceProvider.GetRequiredService(); + +var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; +await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, instanceId ); + +await daprWorkflowClient.WaitForWorkflowCompletionAsync(instanceId); +var state = await daprWorkflowClient.GetWorkflowStateAsync(instanceId); +Console.WriteLine($"Workflow {instanceId}, state: {state.RuntimeStatus}"); + +state = await daprWorkflowClient.GetWorkflowStateAsync($"{instanceId}-sub"); +Console.WriteLine($"Workflow {instanceId}-sub, state: {state.RuntimeStatus}"); diff --git a/examples/Workflow/WorkflowSubworkflow/WorkflowSubworkflow.csproj b/examples/Workflow/WorkflowSubworkflow/WorkflowSubworkflow.csproj new file mode 100644 index 000000000..af3a1b2c8 --- /dev/null +++ b/examples/Workflow/WorkflowSubworkflow/WorkflowSubworkflow.csproj @@ -0,0 +1,18 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + + + diff --git a/examples/Workflow/WorkflowSubworkflow/Workflows/DemoSubWorkflow.cs b/examples/Workflow/WorkflowSubworkflow/Workflows/DemoSubWorkflow.cs new file mode 100644 index 000000000..9d9c0266c --- /dev/null +++ b/examples/Workflow/WorkflowSubworkflow/Workflows/DemoSubWorkflow.cs @@ -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 WorkflowSubWorkflow.Workflows; + +internal sealed class DemoSubWorkflow : Workflow +{ + /// + /// Override to implement workflow logic. + /// + /// The workflow context. + /// The deserialized workflow input. + /// The output of the workflow as a task. + public override async Task RunAsync(WorkflowContext context, string instanceId) + { + Console.WriteLine($"Workflow {context.InstanceId} started"); + Console.WriteLine($"Received input: {instanceId}"); + await context.CreateTimer(TimeSpan.FromSeconds(5)); + return true; + } +} diff --git a/examples/Workflow/WorkflowSubworkflow/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowSubworkflow/Workflows/DemoWorkflow.cs new file mode 100644 index 000000000..9fb0d6357 --- /dev/null +++ b/examples/Workflow/WorkflowSubworkflow/Workflows/DemoWorkflow.cs @@ -0,0 +1,34 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowSubWorkflow.Workflows; + +internal sealed class DemoWorkflow : Workflow +{ + /// + /// Override to implement workflow logic. + /// + /// The workflow context. + /// The deserialized workflow input. + /// The output of the workflow as a task. + public override async Task RunAsync(WorkflowContext context, string instanceId) + { + Console.WriteLine($"Workflow {instanceId} started"); + var subInstanceId = instanceId + "-sub"; + var options = new ChildWorkflowTaskOptions(subInstanceId); + await context.CallChildWorkflowAsync(nameof(DemoSubWorkflow), "Hello, sub-workflow", options); + return true; + } +} From 9d838fca9c3b0c802012c3787698ccf4f436aafd Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Thu, 14 Nov 2024 10:26:25 -0700 Subject: [PATCH 5/9] Added workflow sample: Task chaining (#1387) * Added Workflow Task Chaining example to replace https://github.com/dapr/dotnet-sdk/pull/1206 Signed-off-by: Whit Waldo * Targeting .NET 6, fixed transposition error Signed-off-by: Whit Waldo * Added missing copyright headers Signed-off-by: Whit Waldo --------- Signed-off-by: Whit Waldo --- all.sln | 7 ++ .../WorkflowTaskChaining/Activities/Step1.cs | 31 +++++++++ .../WorkflowTaskChaining/Activities/Step2.cs | 31 +++++++++ .../WorkflowTaskChaining/Activities/Step3.cs | 31 +++++++++ .../Workflow/WorkflowTaskChaining/Program.cs | 64 +++++++++++++++++++ .../WorkflowTaskChaining.csproj | 18 ++++++ .../Workflows/DemoWorkflow.cs | 36 +++++++++++ test/Dapr.E2E.Test/DaprCommand.cs | 2 +- 8 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 examples/Workflow/WorkflowTaskChaining/Activities/Step1.cs create mode 100644 examples/Workflow/WorkflowTaskChaining/Activities/Step2.cs create mode 100644 examples/Workflow/WorkflowTaskChaining/Activities/Step3.cs create mode 100644 examples/Workflow/WorkflowTaskChaining/Program.cs create mode 100644 examples/Workflow/WorkflowTaskChaining/WorkflowTaskChaining.csproj create mode 100644 examples/Workflow/WorkflowTaskChaining/Workflows/DemoWorkflow.cs diff --git a/all.sln b/all.sln index 26262176d..fe0140528 100644 --- a/all.sln +++ b/all.sln @@ -119,6 +119,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Com EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowTaskChaining", "examples\Workflow\WorkflowTaskChaining\WorkflowTaskChaining.csproj", "{945DD3B7-94E5-435E-B3CB-796C20A652C7}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowSubworkflow", "examples\Workflow\WorkflowSubworkflow\WorkflowSubworkflow.csproj", "{FD3E9371-3134-4235-8E80-32226DFB4B1F}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowFanOutFanIn", "examples\Workflow\WorkflowFanOutFanIn\WorkflowFanOutFanIn.csproj", "{D83B27F3-4401-42F5-843E-147566B4999A}" @@ -323,6 +325,10 @@ Global {CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU + {945DD3B7-94E5-435E-B3CB-796C20A652C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {945DD3B7-94E5-435E-B3CB-796C20A652C7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {945DD3B7-94E5-435E-B3CB-796C20A652C7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {945DD3B7-94E5-435E-B3CB-796C20A652C7}.Release|Any CPU.Build.0 = Release|Any CPU {FD3E9371-3134-4235-8E80-32226DFB4B1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {FD3E9371-3134-4235-8E80-32226DFB4B1F}.Debug|Any CPU.Build.0 = Debug|Any CPU {FD3E9371-3134-4235-8E80-32226DFB4B1F}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -415,6 +421,7 @@ Global {DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B} + {945DD3B7-94E5-435E-B3CB-796C20A652C7} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {FD3E9371-3134-4235-8E80-32226DFB4B1F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {D83B27F3-4401-42F5-843E-147566B4999A} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {00359961-0C50-4BB1-A794-8B06DE991639} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} diff --git a/examples/Workflow/WorkflowTaskChaining/Activities/Step1.cs b/examples/Workflow/WorkflowTaskChaining/Activities/Step1.cs new file mode 100644 index 000000000..a0e160b40 --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/Activities/Step1.cs @@ -0,0 +1,31 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowTaskChaining.Activities; + +internal sealed class Step1 : WorkflowActivity +{ + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override Task RunAsync(WorkflowActivityContext context, int input) + { + Console.WriteLine($@"Step 1: Received input: {input}."); + return Task.FromResult(input + 1); + } +} diff --git a/examples/Workflow/WorkflowTaskChaining/Activities/Step2.cs b/examples/Workflow/WorkflowTaskChaining/Activities/Step2.cs new file mode 100644 index 000000000..598239931 --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/Activities/Step2.cs @@ -0,0 +1,31 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowTaskChaining.Activities; + +internal sealed class Step2 : WorkflowActivity +{ + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override Task RunAsync(WorkflowActivityContext context, int input) + { + Console.WriteLine($@"Step 2: Received input: {input}."); + return Task.FromResult(input + 2); + } +} diff --git a/examples/Workflow/WorkflowTaskChaining/Activities/Step3.cs b/examples/Workflow/WorkflowTaskChaining/Activities/Step3.cs new file mode 100644 index 000000000..67a390018 --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/Activities/Step3.cs @@ -0,0 +1,31 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowTaskChaining.Activities; + +internal sealed class Step3 : WorkflowActivity +{ + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override Task RunAsync(WorkflowActivityContext context, int input) + { + Console.WriteLine($@"Step 3: Received input: {input}."); + return Task.FromResult(input ^ 2); + } +} diff --git a/examples/Workflow/WorkflowTaskChaining/Program.cs b/examples/Workflow/WorkflowTaskChaining/Program.cs new file mode 100644 index 000000000..126eff605 --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/Program.cs @@ -0,0 +1,64 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowTaskChaining.Activities; +using WorkflowTaskChaining.Workflows; + +var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => +{ + services.AddDaprWorkflow(options => + { + options.RegisterWorkflow(); + options.RegisterActivity(); + options.RegisterActivity(); + options.RegisterActivity(); + }); +}); + +// Start the app - this is the point where we connect to the Dapr sidecar to listen +// for workflow work-items to execute +using var host = builder.Build(); +await host.StartAsync(); + +await using var scope = host.Services.CreateAsyncScope(); +var daprWorkflowClient = scope.ServiceProvider.GetRequiredService(); + +//Check health +const int wfInput = 42; +Console.WriteLine(@"Workflow Started"); + +var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; + +//Start the workflow immediately +await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, wfInput); + +//Get the status of the workflow +WorkflowState workflowState; +while (true) +{ + workflowState = await daprWorkflowClient.GetWorkflowStateAsync(instanceId, true); + Console.WriteLine($@"Workflow status: {workflowState.RuntimeStatus}"); + if (workflowState.IsWorkflowCompleted) + break; + + await Task.Delay(TimeSpan.FromSeconds(1)); +} + +//Display the result from the workflow +var result = string.Join(" ", workflowState.ReadOutputAs() ?? Array.Empty()); +Console.WriteLine($@"Workflow result: {result}"); + + diff --git a/examples/Workflow/WorkflowTaskChaining/WorkflowTaskChaining.csproj b/examples/Workflow/WorkflowTaskChaining/WorkflowTaskChaining.csproj new file mode 100644 index 000000000..91ded8afb --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/WorkflowTaskChaining.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/examples/Workflow/WorkflowTaskChaining/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowTaskChaining/Workflows/DemoWorkflow.cs new file mode 100644 index 000000000..722114349 --- /dev/null +++ b/examples/Workflow/WorkflowTaskChaining/Workflows/DemoWorkflow.cs @@ -0,0 +1,36 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowTaskChaining.Activities; + +namespace WorkflowTaskChaining.Workflows; + +internal sealed class DemoWorkflow : Workflow +{ + /// + /// Override to implement workflow logic. + /// + /// The workflow context. + /// The deserialized workflow input. + /// The output of the workflow as a task. + public override async Task RunAsync(WorkflowContext context, int input) + { + var result1 = await context.CallActivityAsync(nameof(Step1), input); + var result2 = await context.CallActivityAsync(nameof(Step2), result1); + var result3 = await context.CallActivityAsync(nameof(Step3), result2); + var ret = new int[] { result1, result2, result3 }; + + return ret; + } +} diff --git a/test/Dapr.E2E.Test/DaprCommand.cs b/test/Dapr.E2E.Test/DaprCommand.cs index 768e81960..a692ec638 100644 --- a/test/Dapr.E2E.Test/DaprCommand.cs +++ b/test/Dapr.E2E.Test/DaprCommand.cs @@ -38,7 +38,7 @@ public DaprCommand(ITestOutputHelper output) public void Run() { - Console.WriteLine($"Running command: {this.Command}"); + Console.WriteLine($@"Running command: {this.Command}"); var escapedArgs = Command.Replace("\"", "\\\""); var process = new Process() { From 57a656bd2b6f6a12d9049115bca778bfdf394d80 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Thu, 14 Nov 2024 11:48:38 -0700 Subject: [PATCH 6/9] Added workflow sample: Monitor (#1388) * Added workflow monitor Signed-off-by: Whit Waldo * Restore to original argument names Signed-off-by: Whit Waldo * Update to target .NET 6 Signed-off-by: Whit Waldo * Added missing copyright headers Signed-off-by: Whit Waldo --------- Signed-off-by: Whit Waldo --- all.sln | 7 +++ .../WorkflowMonitor/Activities/CheckStatus.cs | 31 ++++++++++ examples/Workflow/WorkflowMonitor/Program.cs | 39 +++++++++++++ .../WorkflowMonitor/WorkflowMonitor.csproj | 18 ++++++ .../WorkflowMonitor/Workflows/DemoWorkflow.cs | 57 +++++++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs create mode 100644 examples/Workflow/WorkflowMonitor/Program.cs create mode 100644 examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj create mode 100644 examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs diff --git a/all.sln b/all.sln index fe0140528..f76687235 100644 --- a/all.sln +++ b/all.sln @@ -119,6 +119,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Com EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowMonitor", "examples\Workflow\WorkflowMonitor\WorkflowMonitor.csproj", "{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowTaskChaining", "examples\Workflow\WorkflowTaskChaining\WorkflowTaskChaining.csproj", "{945DD3B7-94E5-435E-B3CB-796C20A652C7}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowSubworkflow", "examples\Workflow\WorkflowSubworkflow\WorkflowSubworkflow.csproj", "{FD3E9371-3134-4235-8E80-32226DFB4B1F}" @@ -325,6 +327,10 @@ Global {CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU + {7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Release|Any CPU.Build.0 = Release|Any CPU {945DD3B7-94E5-435E-B3CB-796C20A652C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {945DD3B7-94E5-435E-B3CB-796C20A652C7}.Debug|Any CPU.Build.0 = Debug|Any CPU {945DD3B7-94E5-435E-B3CB-796C20A652C7}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -421,6 +427,7 @@ Global {DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B} + {7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {945DD3B7-94E5-435E-B3CB-796C20A652C7} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {FD3E9371-3134-4235-8E80-32226DFB4B1F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {D83B27F3-4401-42F5-843E-147566B4999A} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} diff --git a/examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs b/examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs new file mode 100644 index 000000000..99e5ba2b1 --- /dev/null +++ b/examples/Workflow/WorkflowMonitor/Activities/CheckStatus.cs @@ -0,0 +1,31 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowMonitor.Activities; + +internal sealed class CheckStatus : WorkflowActivity +{ + private static List status = new List { "healthy", "unhealthy" }; + private Random random = new(); + + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override Task RunAsync(WorkflowActivityContext context, bool input) => + Task.FromResult(status[random.Next(status.Count)]); +} diff --git a/examples/Workflow/WorkflowMonitor/Program.cs b/examples/Workflow/WorkflowMonitor/Program.cs new file mode 100644 index 000000000..05697d480 --- /dev/null +++ b/examples/Workflow/WorkflowMonitor/Program.cs @@ -0,0 +1,39 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowMonitor.Activities; +using WorkflowMonitor.Workflows; + +var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => +{ + services.AddDaprWorkflow(options => + { + options.RegisterWorkflow(); + options.RegisterActivity(); + }); +}); + +using var host = builder.Build(); +await host.StartAsync(); + +await using var scope = host.Services.CreateAsyncScope(); +var daprWorkflowClient = scope.ServiceProvider.GetRequiredService(); + +var instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}"; +var isHealthy = true; +await daprWorkflowClient.ScheduleNewWorkflowAsync(nameof(DemoWorkflow), instanceId, isHealthy); + +//We don't want to block on workflow completion as this workflow will never complete diff --git a/examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj b/examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj new file mode 100644 index 000000000..91ded8afb --- /dev/null +++ b/examples/Workflow/WorkflowMonitor/WorkflowMonitor.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs new file mode 100644 index 000000000..e456eea15 --- /dev/null +++ b/examples/Workflow/WorkflowMonitor/Workflows/DemoWorkflow.cs @@ -0,0 +1,57 @@ +// ------------------------------------------------------------------------ +// 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 WorkflowMonitor.Activities; + +namespace WorkflowMonitor.Workflows; + +internal sealed class DemoWorkflow : Workflow +{ + /// + /// Override to implement workflow logic. + /// + /// The workflow context. + /// The deserialized workflow input. + /// The output of the workflow as a task. + public override async Task RunAsync(WorkflowContext context, bool isHealthy) + { + string status = await context.CallActivityAsync(nameof(CheckStatus), true); + int next_sleep_interval; + if (!context.IsReplaying) + { + Console.WriteLine($"This job is {status}"); + } + + if (status == "healthy") + { + isHealthy = true; + next_sleep_interval = 30; + } + else + { + if (isHealthy) + { + isHealthy = false; + } + Console.WriteLine("Status is unhealthy. Set check interval to 5s"); + next_sleep_interval = 5; + } + + await context.CreateTimer(TimeSpan.FromSeconds(next_sleep_interval)); + context.ContinueAsNew(isHealthy); + + //This workflow will never complete + return true; + } +} From f769eb1205193883f053b40b85e12da8394d7af9 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Mon, 18 Nov 2024 11:48:53 -0700 Subject: [PATCH 7/9] Added workflow example: External interaction (#1389) * Added workflow example demonstrating external interaction Signed-off-by: Whit Waldo * Added copyright headers Signed-off-by: Whit Waldo * Fixed .sln file Signed-off-by: Whit Waldo --------- Signed-off-by: Whit Waldo --- all.sln | 7 ++ .../Activities/ApproveActivity.cs | 33 ++++++++ .../Activities/RejectActivity.cs | 33 ++++++++ .../WorkflowExternalInteraction/Program.cs | 76 +++++++++++++++++++ .../WorkflowExternalInteraction.csproj | 18 +++++ .../Workflows/DemoWorkflow.cs | 46 +++++++++++ 6 files changed, 213 insertions(+) create mode 100644 examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs create mode 100644 examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs create mode 100644 examples/Workflow/WorkflowExternalInteraction/Program.cs create mode 100644 examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj create mode 100644 examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs diff --git a/all.sln b/all.sln index f76687235..8a6eb2fff 100644 --- a/all.sln +++ b/all.sln @@ -119,6 +119,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Com EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowExternalInteraction", "examples\Workflow\WorkflowExternalInteraction\WorkflowExternalInteraction.csproj", "{43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowMonitor", "examples\Workflow\WorkflowMonitor\WorkflowMonitor.csproj", "{7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowTaskChaining", "examples\Workflow\WorkflowTaskChaining\WorkflowTaskChaining.csproj", "{945DD3B7-94E5-435E-B3CB-796C20A652C7}" @@ -327,6 +329,10 @@ Global {CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU {CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU + {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F}.Release|Any CPU.Build.0 = Release|Any CPU {7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -427,6 +433,7 @@ Global {DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF} {CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B} + {43CB06A9-7E88-4C5F-BFB8-947E072CBC9F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {7F73A3D8-FFC2-4E31-AA3D-A4840316A8C6} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {945DD3B7-94E5-435E-B3CB-796C20A652C7} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} {FD3E9371-3134-4235-8E80-32226DFB4B1F} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9} diff --git a/examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs b/examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs new file mode 100644 index 000000000..48048e19f --- /dev/null +++ b/examples/Workflow/WorkflowExternalInteraction/Activities/ApproveActivity.cs @@ -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 +{ + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override async Task RunAsync(WorkflowActivityContext context, string input) + { + Console.WriteLine($"Workflow {input} is approved"); + Console.WriteLine("Running Approval activity..."); + await Task.Delay(TimeSpan.FromSeconds(5)); + return true; + } +} diff --git a/examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs b/examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs new file mode 100644 index 000000000..765d31a27 --- /dev/null +++ b/examples/Workflow/WorkflowExternalInteraction/Activities/RejectActivity.cs @@ -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 +{ + /// + /// Override to implement async (non-blocking) workflow activity logic. + /// + /// Provides access to additional context for the current activity execution. + /// The deserialized activity input. + /// The output of the activity as a task. + public override async Task RunAsync(WorkflowActivityContext context, string input) + { + Console.WriteLine($"Workflow {input} is rejected"); + Console.WriteLine("Running Reject activity..."); + await Task.Delay(TimeSpan.FromSeconds(5)); + return true; + } +} diff --git a/examples/Workflow/WorkflowExternalInteraction/Program.cs b/examples/Workflow/WorkflowExternalInteraction/Program.cs new file mode 100644 index 000000000..b83527d25 --- /dev/null +++ b/examples/Workflow/WorkflowExternalInteraction/Program.cs @@ -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(); + options.RegisterActivity(); + options.RegisterActivity(); + }); +}); + +using var host = builder.Build(); +await host.StartAsync(); + +await using var scope = host.Services.CreateAsyncScope(); +var daprWorkflowClient = scope.ServiceProvider.GetRequiredService(); + +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}"); diff --git a/examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj b/examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj new file mode 100644 index 000000000..4aae25c46 --- /dev/null +++ b/examples/Workflow/WorkflowExternalInteraction/WorkflowExternalInteraction.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs b/examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs new file mode 100644 index 000000000..ba815a800 --- /dev/null +++ b/examples/Workflow/WorkflowExternalInteraction/Workflows/DemoWorkflow.cs @@ -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 +{ + /// + /// Override to implement workflow logic. + /// + /// The workflow context. + /// The deserialized workflow input. + /// The output of the workflow as a task. + public override async Task RunAsync(WorkflowContext context, string input) + { + try + { + await context.WaitForExternalEventAsync(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; + } +} From ef04cad901685959b90727c49ae2f159cc8e7137 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Thu, 21 Nov 2024 14:40:20 -0700 Subject: [PATCH 8/9] 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 * Added unit tests - updated dependencies accordingly Signed-off-by: Whit Waldo * Added service lifetime to DaprClient as well Signed-off-by: Whit Waldo * Added update to DaprClient to pass service lifetime through Signed-off-by: Whit Waldo * Added documentation indicating how to register DaprWorkflowClient with different lifecycle options. Signed-off-by: Whit Waldo * Removed unnecessary line from csproj Signed-off-by: Whit Waldo * Simplified registrations Signed-off-by: Whit Waldo * Called out an important point about registrations Signed-off-by: Whit Waldo --------- Signed-off-by: Whit Waldo --- Directory.Packages.props | 4 +- all.sln | 7 ++ .../dotnet-workflowclient-usage.md | 77 +++++++++++++++++++ .../DaprServiceCollectionExtensions.cs | 41 ++++++++-- src/Dapr.Workflow/Dapr.Workflow.csproj | 1 - .../WorkflowServiceCollectionExtensions.cs | 39 ++++++++-- .../Dapr.Workflow.Test.csproj | 27 +++++++ ...orkflowServiceCollectionExtensionsTests.cs | 58 ++++++++++++++ 8 files changed, 238 insertions(+), 16 deletions(-) create mode 100644 daprdocs/content/en/dotnet-sdk-docs/dotnet-workflow/dotnet-workflowclient-usage.md create mode 100644 test/Dapr.Workflow.Test/Dapr.Workflow.Test.csproj create mode 100644 test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 772dd7c6e..a98e9db58 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -28,8 +28,8 @@ - - + + diff --git a/all.sln b/all.sln index 8a6eb2fff..bb44a3bdc 100644 --- a/all.sln +++ b/all.sln @@ -145,6 +145,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Jobs", "Jobs", "{D9697361-2 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JobsSample", "examples\Jobs\JobsSample\JobsSample.csproj", "{9CAF360E-5AD3-4C4F-89A0-327EEB70D673}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Workflow.Test", "test\Dapr.Workflow.Test\Dapr.Workflow.Test.csproj", "{E90114C6-86FC-43B8-AE5C-D9273CF21FE4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -377,6 +379,10 @@ Global {9CAF360E-5AD3-4C4F-89A0-327EEB70D673}.Debug|Any CPU.Build.0 = Debug|Any CPU {9CAF360E-5AD3-4C4F-89A0-327EEB70D673}.Release|Any CPU.ActiveCfg = Release|Any CPU {9CAF360E-5AD3-4C4F-89A0-327EEB70D673}.Release|Any CPU.Build.0 = Release|Any CPU + {E90114C6-86FC-43B8-AE5C-D9273CF21FE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E90114C6-86FC-43B8-AE5C-D9273CF21FE4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E90114C6-86FC-43B8-AE5C-D9273CF21FE4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E90114C6-86FC-43B8-AE5C-D9273CF21FE4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -446,6 +452,7 @@ Global {BF9828E9-5597-4D42-AA6E-6E6C12214204} = {DD020B34-460F-455F-8D17-CF4A949F100B} {D9697361-232F-465D-A136-4561E0E88488} = {D687DDC4-66C5-4667-9E3A-FD8B78ECAA78} {9CAF360E-5AD3-4C4F-89A0-327EEB70D673} = {D9697361-232F-465D-A136-4561E0E88488} + {E90114C6-86FC-43B8-AE5C-D9273CF21FE4} = {DD020B34-460F-455F-8D17-CF4A949F100B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40} diff --git a/daprdocs/content/en/dotnet-sdk-docs/dotnet-workflow/dotnet-workflowclient-usage.md b/daprdocs/content/en/dotnet-sdk-docs/dotnet-workflow/dotnet-workflowclient-usage.md new file mode 100644 index 000000000..ac6a0f189 --- /dev/null +++ b/daprdocs/content/en/dotnet-sdk-docs/dotnet-workflow/dotnet-workflowclient-usage.md @@ -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(); + options.RegisterActivity(); +}); + +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(); + options.RegisterActivity(); +}, 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(); + options.RegisterActivity(); +}, ServiceLifecycle.Transient); + +var app = builder.Build(); +await app.RunAsync(); +``` \ No newline at end of file diff --git a/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs b/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs index 52e9110be..ea6fb520e 100644 --- a/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs +++ b/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs @@ -32,16 +32,32 @@ public static class DaprServiceCollectionExtensions /// /// The . /// - public static void AddDaprClient(this IServiceCollection services, Action? configure = null) + /// The lifetime of the registered services. + public static void AddDaprClient(this IServiceCollection services, Action? configure = null, + ServiceLifetime lifetime = ServiceLifetime.Singleton) { ArgumentNullException.ThrowIfNull(services, nameof(services)); - services.TryAddSingleton(serviceProvider => + var registration = new Func((serviceProvider) => { var builder = CreateDaprClientBuilder(serviceProvider); configure?.Invoke(builder); return builder.Build(); }); + + switch (lifetime) + { + case ServiceLifetime.Scoped: + services.TryAddScoped(registration); + break; + case ServiceLifetime.Transient: + services.TryAddTransient(registration); + break; + case ServiceLifetime.Singleton: + default: + services.TryAddSingleton(registration); + break; + } } /// @@ -50,17 +66,32 @@ public static void AddDaprClient(this IServiceCollection services, Action /// The . /// + /// The lifetime of the registered services. public static void AddDaprClient(this IServiceCollection services, - Action configure) + Action configure, ServiceLifetime lifetime = ServiceLifetime.Singleton) { ArgumentNullException.ThrowIfNull(services, nameof(services)); - - services.TryAddSingleton(serviceProvider => + + var registration = new Func((serviceProvider) => { var builder = CreateDaprClientBuilder(serviceProvider); configure?.Invoke(serviceProvider, builder); return builder.Build(); }); + + switch (lifetime) + { + case ServiceLifetime.Singleton: + services.TryAddSingleton(registration); + break; + case ServiceLifetime.Scoped: + services.TryAddScoped(registration); + break; + case ServiceLifetime.Transient: + default: + services.TryAddTransient(registration); + break; + } } private static DaprClientBuilder CreateDaprClientBuilder(IServiceProvider serviceProvider) diff --git a/src/Dapr.Workflow/Dapr.Workflow.csproj b/src/Dapr.Workflow/Dapr.Workflow.csproj index 992baee73..360d121ef 100644 --- a/src/Dapr.Workflow/Dapr.Workflow.csproj +++ b/src/Dapr.Workflow/Dapr.Workflow.csproj @@ -3,7 +3,6 @@ - net6;net7;net8 enable Dapr.Workflow Dapr Workflow Authoring SDK diff --git a/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs b/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs index 5c10a776e..209e4edc0 100644 --- a/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs +++ b/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs @@ -29,25 +29,48 @@ public static class WorkflowServiceCollectionExtensions /// /// The . /// A delegate used to configure actor options and register workflow functions. + /// The lifetime of the registered services. public static IServiceCollection AddDaprWorkflow( this IServiceCollection serviceCollection, - Action configure) + Action configure, + ServiceLifetime lifetime = ServiceLifetime.Singleton) { if (serviceCollection == null) { throw new ArgumentNullException(nameof(serviceCollection)); } - serviceCollection.TryAddSingleton(); + serviceCollection.AddDaprClient(lifetime: lifetime); serviceCollection.AddHttpClient(); - + serviceCollection.AddHostedService(); + + switch (lifetime) + { + case ServiceLifetime.Singleton: #pragma warning disable CS0618 // Type or member is obsolete - keeping around temporarily - replaced by DaprWorkflowClient - serviceCollection.TryAddSingleton(); + serviceCollection.TryAddSingleton(); #pragma warning restore CS0618 // Type or member is obsolete - serviceCollection.AddHostedService(); - serviceCollection.TryAddSingleton(); - serviceCollection.AddDaprClient(); - + serviceCollection.TryAddSingleton(); + serviceCollection.TryAddSingleton(); + break; + case ServiceLifetime.Scoped: +#pragma warning disable CS0618 // Type or member is obsolete - keeping around temporarily - replaced by DaprWorkflowClient + serviceCollection.TryAddScoped(); +#pragma warning restore CS0618 // Type or member is obsolete + serviceCollection.TryAddScoped(); + serviceCollection.TryAddScoped(); + break; + case ServiceLifetime.Transient: +#pragma warning disable CS0618 // Type or member is obsolete - keeping around temporarily - replaced by DaprWorkflowClient + serviceCollection.TryAddTransient(); +#pragma warning restore CS0618 // Type or member is obsolete + serviceCollection.TryAddTransient(); + serviceCollection.TryAddTransient(); + break; + default: + throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null); + } + serviceCollection.AddOptions().Configure(configure); //Register the factory and force resolution so the Durable Task client and worker can be registered diff --git a/test/Dapr.Workflow.Test/Dapr.Workflow.Test.csproj b/test/Dapr.Workflow.Test/Dapr.Workflow.Test.csproj new file mode 100644 index 000000000..531a0b1f1 --- /dev/null +++ b/test/Dapr.Workflow.Test/Dapr.Workflow.Test.csproj @@ -0,0 +1,27 @@ + + + + enable + enable + false + true + + + + + + + + + + + + + + + + + + + + diff --git a/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs b/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs new file mode 100644 index 000000000..2206d939b --- /dev/null +++ b/test/Dapr.Workflow.Test/WorkflowServiceCollectionExtensionsTests.cs @@ -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(); + var daprWorkflowClient2 = serviceProvider.GetService(); + + 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(); + + await using var scope2 = serviceProvider.CreateAsyncScope(); + var daprWorkflowClient2 = scope2.ServiceProvider.GetService(); + + 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(); + var daprWorkflowClient2 = serviceProvider.GetService(); + + Assert.NotNull(daprWorkflowClient1); + Assert.NotNull(daprWorkflowClient2); + Assert.NotSame(daprWorkflowClient1, daprWorkflowClient2); + } +} From 0b80c853b640686085b931eb92f20be7ce6dbc3b Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Sun, 24 Nov 2024 01:14:10 -0700 Subject: [PATCH 9/9] Additional lifecycle registration changes (#1410) * Added service lifetime to Jobs client Signed-off-by: Whit Waldo * Added service lifetime to messaging client Signed-off-by: Whit Waldo * Added service lifetime to actors registration Signed-off-by: Whit Waldo * Added unit tests for DaprClient Signed-off-by: Whit Waldo * Minor naming tweaks Signed-off-by: Whit Waldo * Removed invalid using Signed-off-by: Whit Waldo * Added service lifetime tests for actors Signed-off-by: Whit Waldo * Added unit tests for jobs client lifecycle registrations Signed-off-by: Whit Waldo * Added unit tests for PubSub and lifecycle registration Signed-off-by: Whit Waldo * Fixed missing registration dependency Signed-off-by: Whit Waldo --------- Signed-off-by: Whit Waldo --- .../ActorsServiceCollectionExtensions.cs | 38 +++-- .../DaprJobsServiceCollectionExtensions.cs | 24 ++- ...ishSubscribeServiceCollectionExtensions.cs | 19 ++- ...aprActorServiceCollectionExtensionsTest.cs | 60 ++++++++ .../DaprServiceCollectionExtensionsTest.cs | 142 ++++++++++++------ ...aprJobsServiceCollectionExtensionsTests.cs | 54 ++++++- ...bscribeServiceCollectionExtensionsTests.cs | 76 ++++++++++ 7 files changed, 352 insertions(+), 61 deletions(-) create mode 100644 test/Dapr.Actors.AspNetCore.Test/DaprActorServiceCollectionExtensionsTest.cs create mode 100644 test/Dapr.Messaging.Test/Extensions/PublishSubscribeServiceCollectionExtensionsTests.cs diff --git a/src/Dapr.Actors.AspNetCore/ActorsServiceCollectionExtensions.cs b/src/Dapr.Actors.AspNetCore/ActorsServiceCollectionExtensions.cs index 11f05f4c1..9b80975db 100644 --- a/src/Dapr.Actors.AspNetCore/ActorsServiceCollectionExtensions.cs +++ b/src/Dapr.Actors.AspNetCore/ActorsServiceCollectionExtensions.cs @@ -34,17 +34,18 @@ public static class ActorsServiceCollectionExtensions /// /// The . /// A delegate used to configure actor options and register actor types. - public static void AddActors(this IServiceCollection? services, Action? configure) + /// The lifetime of the registered services. + public static void AddActors(this IServiceCollection? services, Action? configure, ServiceLifetime lifetime = ServiceLifetime.Singleton) { ArgumentNullException.ThrowIfNull(services, nameof(services)); - // Routing and health checks are required dependencies. + // Routing, health checks and logging are required dependencies. services.AddRouting(); services.AddHealthChecks(); + services.AddLogging(); - services.TryAddSingleton(); - services.TryAddSingleton(s => - { + var actorRuntimeRegistration = new Func(s => + { var options = s.GetRequiredService>().Value; ConfigureActorOptions(s, options); @@ -53,11 +54,10 @@ public static void AddActors(this IServiceCollection? services, Action(); return new ActorRuntime(options, loggerFactory, activatorFactory, proxyFactory); }); - - services.TryAddSingleton(s => + var proxyFactoryRegistration = new Func(serviceProvider => { - var options = s.GetRequiredService>().Value; - ConfigureActorOptions(s, options); + var options = serviceProvider.GetRequiredService>().Value; + ConfigureActorOptions(serviceProvider, options); var factory = new ActorProxyFactory() { @@ -72,6 +72,26 @@ public static void AddActors(this IServiceCollection? services, Action(); + services.TryAddScoped(actorRuntimeRegistration); + services.TryAddScoped(proxyFactoryRegistration); + break; + case ServiceLifetime.Transient: + services.TryAddTransient(); + services.TryAddTransient(actorRuntimeRegistration); + services.TryAddTransient(proxyFactoryRegistration); + break; + default: + case ServiceLifetime.Singleton: + services.TryAddSingleton(); + services.TryAddSingleton(actorRuntimeRegistration); + services.TryAddSingleton(proxyFactoryRegistration); + break; + } + if (configure != null) { services.Configure(configure); diff --git a/src/Dapr.Jobs/Extensions/DaprJobsServiceCollectionExtensions.cs b/src/Dapr.Jobs/Extensions/DaprJobsServiceCollectionExtensions.cs index 67e718985..93265837b 100644 --- a/src/Dapr.Jobs/Extensions/DaprJobsServiceCollectionExtensions.cs +++ b/src/Dapr.Jobs/Extensions/DaprJobsServiceCollectionExtensions.cs @@ -26,25 +26,40 @@ public static class DaprJobsServiceCollectionExtensions /// /// The . /// Optionally allows greater configuration of the . - public static IServiceCollection AddDaprJobsClient(this IServiceCollection serviceCollection, Action? configure = null) + /// The lifetime of the registered services. + public static IServiceCollection AddDaprJobsClient(this IServiceCollection serviceCollection, Action? configure = null, ServiceLifetime lifetime = ServiceLifetime.Singleton) { ArgumentNullException.ThrowIfNull(serviceCollection, nameof(serviceCollection)); //Register the IHttpClientFactory implementation serviceCollection.AddHttpClient(); - serviceCollection.TryAddSingleton(serviceProvider => + var registration = new Func(serviceProvider => { var httpClientFactory = serviceProvider.GetRequiredService(); var builder = new DaprJobsClientBuilder(); builder.UseHttpClientFactory(httpClientFactory); - + configure?.Invoke(builder); return builder.Build(); }); + switch (lifetime) + { + case ServiceLifetime.Scoped: + serviceCollection.TryAddScoped(registration); + break; + case ServiceLifetime.Transient: + serviceCollection.TryAddTransient(registration); + break; + case ServiceLifetime.Singleton: + default: + serviceCollection.TryAddSingleton(registration); + break; + } + return serviceCollection; } @@ -53,8 +68,9 @@ public static IServiceCollection AddDaprJobsClient(this IServiceCollection servi /// /// The . /// Optionally allows greater configuration of the using injected services. + /// The lifetime of the registered services. /// - public static IServiceCollection AddDaprJobsClient(this IServiceCollection serviceCollection, Action? configure) + public static IServiceCollection AddDaprJobsClient(this IServiceCollection serviceCollection, Action? configure, ServiceLifetime lifetime = ServiceLifetime.Singleton) { ArgumentNullException.ThrowIfNull(serviceCollection, nameof(serviceCollection)); diff --git a/src/Dapr.Messaging/PublishSubscribe/Extensions/PublishSubscribeServiceCollectionExtensions.cs b/src/Dapr.Messaging/PublishSubscribe/Extensions/PublishSubscribeServiceCollectionExtensions.cs index bc60c5880..fe9b7c417 100644 --- a/src/Dapr.Messaging/PublishSubscribe/Extensions/PublishSubscribeServiceCollectionExtensions.cs +++ b/src/Dapr.Messaging/PublishSubscribe/Extensions/PublishSubscribeServiceCollectionExtensions.cs @@ -13,15 +13,16 @@ public static class PublishSubscribeServiceCollectionExtensions /// /// The . /// Optionally allows greater configuration of the using injected services. + /// The lifetime of the registered services. /// - public static IServiceCollection AddDaprPubSubClient(this IServiceCollection services, Action? configure = null) + public static IServiceCollection AddDaprPubSubClient(this IServiceCollection services, Action? configure = null, ServiceLifetime lifetime = ServiceLifetime.Singleton) { ArgumentNullException.ThrowIfNull(services, nameof(services)); //Register the IHttpClientFactory implementation services.AddHttpClient(); - services.TryAddSingleton(serviceProvider => + var registration = new Func(serviceProvider => { var httpClientFactory = serviceProvider.GetRequiredService(); @@ -33,6 +34,20 @@ public static IServiceCollection AddDaprPubSubClient(this IServiceCollection ser return builder.Build(); }); + switch (lifetime) + { + case ServiceLifetime.Scoped: + services.TryAddScoped(registration); + break; + case ServiceLifetime.Transient: + services.TryAddTransient(registration); + break; + default: + case ServiceLifetime.Singleton: + services.TryAddSingleton(registration); + break; + } + return services; } } diff --git a/test/Dapr.Actors.AspNetCore.Test/DaprActorServiceCollectionExtensionsTest.cs b/test/Dapr.Actors.AspNetCore.Test/DaprActorServiceCollectionExtensionsTest.cs new file mode 100644 index 000000000..3255fb785 --- /dev/null +++ b/test/Dapr.Actors.AspNetCore.Test/DaprActorServiceCollectionExtensionsTest.cs @@ -0,0 +1,60 @@ +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Dapr.Actors.AspNetCore.Test; + +public sealed class DaprActorServiceCollectionExtensionsTest +{ + [Fact] + public void RegisterActorsClient_ShouldRegisterSingleton_WhenLifetimeIsSingleton() + { + var services = new ServiceCollection(); + + services.AddActors(options => { }, ServiceLifetime.Singleton); + var serviceProvider = services.BuildServiceProvider(); + + var daprClient1 = serviceProvider.GetService(); + var daprClient2 = serviceProvider.GetService(); + + Assert.NotNull(daprClient1); + Assert.NotNull(daprClient2); + + Assert.Same(daprClient1, daprClient2); + } + + [Fact] + public async Task RegisterActorsClient_ShouldRegisterScoped_WhenLifetimeIsScoped() + { + var services = new ServiceCollection(); + + services.AddActors(options => { }, ServiceLifetime.Scoped); + var serviceProvider = services.BuildServiceProvider(); + + await using var scope1 = serviceProvider.CreateAsyncScope(); + var daprClient1 = scope1.ServiceProvider.GetService(); + + await using var scope2 = serviceProvider.CreateAsyncScope(); + var daprClient2 = scope2.ServiceProvider.GetService(); + + Assert.NotNull(daprClient1); + Assert.NotNull(daprClient2); + Assert.NotSame(daprClient1, daprClient2); + } + + [Fact] + public void RegisterActorsClient_ShouldRegisterTransient_WhenLifetimeIsTransient() + { + var services = new ServiceCollection(); + + services.AddActors(options => { }, ServiceLifetime.Transient); + var serviceProvider = services.BuildServiceProvider(); + + var daprClient1 = serviceProvider.GetService(); + var daprClient2 = serviceProvider.GetService(); + + Assert.NotNull(daprClient1); + Assert.NotNull(daprClient2); + Assert.NotSame(daprClient1, daprClient2); + } +} diff --git a/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs b/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs index 4a340e22a..2028a9fbb 100644 --- a/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs +++ b/test/Dapr.AspNetCore.Test/DaprServiceCollectionExtensionsTest.cs @@ -15,67 +15,120 @@ using System; using System.Text.Json; +using System.Threading.Tasks; using Dapr.Client; using Microsoft.Extensions.DependencyInjection; using Xunit; -namespace Dapr.AspNetCore.Test +namespace Dapr.AspNetCore.Test; + +public class DaprServiceCollectionExtensionsTest { - public class DaprServiceCollectionExtensionsTest + [Fact] + public void AddDaprClient_RegistersDaprClientOnlyOnce() { - [Fact] - public void AddDaprClient_RegistersDaprClientOnlyOnce() - { - var services = new ServiceCollection(); + var services = new ServiceCollection(); - var clientBuilder = new Action( - builder => builder.UseJsonSerializationOptions( - new JsonSerializerOptions() - { - PropertyNameCaseInsensitive = false - } - ) - ); + var clientBuilder = new Action( + builder => builder.UseJsonSerializationOptions( + new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = false + } + ) + ); - // register with JsonSerializerOptions.PropertyNameCaseInsensitive = true (default) - services.AddDaprClient(); + // register with JsonSerializerOptions.PropertyNameCaseInsensitive = true (default) + services.AddDaprClient(); - // register with PropertyNameCaseInsensitive = false - services.AddDaprClient(clientBuilder); + // register with PropertyNameCaseInsensitive = false + services.AddDaprClient(clientBuilder); - var serviceProvider = services.BuildServiceProvider(); + var serviceProvider = services.BuildServiceProvider(); - DaprClientGrpc? daprClient = serviceProvider.GetService() as DaprClientGrpc; + DaprClientGrpc? daprClient = serviceProvider.GetService() as DaprClientGrpc; - Assert.NotNull(daprClient); - Assert.True(daprClient?.JsonSerializerOptions.PropertyNameCaseInsensitive); - } + Assert.NotNull(daprClient); + Assert.True(daprClient?.JsonSerializerOptions.PropertyNameCaseInsensitive); + } - [Fact] - public void AddDaprClient_RegistersUsingDependencyFromIServiceProvider() + [Fact] + public void AddDaprClient_RegistersUsingDependencyFromIServiceProvider() + { + + var services = new ServiceCollection(); + services.AddSingleton(); + services.AddDaprClient((provider, builder) => { + var configProvider = provider.GetRequiredService(); + var caseSensitivity = configProvider.GetCaseSensitivity(); - var services = new ServiceCollection(); - services.AddSingleton(); - services.AddDaprClient((provider, builder) => + builder.UseJsonSerializationOptions(new JsonSerializerOptions { - var configProvider = provider.GetRequiredService(); - var caseSensitivity = configProvider.GetCaseSensitivity(); - - builder.UseJsonSerializationOptions(new JsonSerializerOptions - { - PropertyNameCaseInsensitive = caseSensitivity - }); + PropertyNameCaseInsensitive = caseSensitivity }); + }); - var serviceProvider = services.BuildServiceProvider(); + var serviceProvider = services.BuildServiceProvider(); - DaprClientGrpc? client = serviceProvider.GetRequiredService() as DaprClientGrpc; + DaprClientGrpc? client = serviceProvider.GetRequiredService() as DaprClientGrpc; - //Registers with case-insensitive as true by default, but we set as false above - Assert.NotNull(client); - Assert.False(client?.JsonSerializerOptions.PropertyNameCaseInsensitive); - } + //Registers with case-insensitive as true by default, but we set as false above + Assert.NotNull(client); + Assert.False(client?.JsonSerializerOptions.PropertyNameCaseInsensitive); + } + + [Fact] + public void RegisterClient_ShouldRegisterSingleton_WhenLifetimeIsSingleton() + { + var services = new ServiceCollection(); + + services.AddDaprClient(options => { }, ServiceLifetime.Singleton); + var serviceProvider = services.BuildServiceProvider(); + + var daprClient1 = serviceProvider.GetService(); + var daprClient2 = serviceProvider.GetService(); + + Assert.NotNull(daprClient1); + Assert.NotNull(daprClient2); + + Assert.Same(daprClient1, daprClient2); + } + + [Fact] + public async Task RegisterDaprClient_ShouldRegisterScoped_WhenLifetimeIsScoped() + { + var services = new ServiceCollection(); + + services.AddDaprClient(options => { }, ServiceLifetime.Scoped); + var serviceProvider = services.BuildServiceProvider(); + + await using var scope1 = serviceProvider.CreateAsyncScope(); + var daprClient1 = scope1.ServiceProvider.GetService(); + + await using var scope2 = serviceProvider.CreateAsyncScope(); + var daprClient2 = scope2.ServiceProvider.GetService(); + + Assert.NotNull(daprClient1); + Assert.NotNull(daprClient2); + Assert.NotSame(daprClient1, daprClient2); + } + + [Fact] + public void RegisterDaprClient_ShouldRegisterTransient_WhenLifetimeIsTransient() + { + var services = new ServiceCollection(); + + services.AddDaprClient(options => { }, ServiceLifetime.Transient); + var serviceProvider = services.BuildServiceProvider(); + + var daprClient1 = serviceProvider.GetService(); + var daprClient2 = serviceProvider.GetService(); + + Assert.NotNull(daprClient1); + Assert.NotNull(daprClient2); + Assert.NotSame(daprClient1, daprClient2); + } #if NET8_0_OR_GREATER @@ -96,9 +149,8 @@ public void AddDaprClient_WithKeyedServices() } #endif - private class TestConfigurationProvider - { - public bool GetCaseSensitivity() => false; - } + private class TestConfigurationProvider + { + public bool GetCaseSensitivity() => false; } } diff --git a/test/Dapr.Jobs.Test/Extensions/DaprJobsServiceCollectionExtensionsTests.cs b/test/Dapr.Jobs.Test/Extensions/DaprJobsServiceCollectionExtensionsTests.cs index 34d900aeb..281477d4e 100644 --- a/test/Dapr.Jobs.Test/Extensions/DaprJobsServiceCollectionExtensionsTests.cs +++ b/test/Dapr.Jobs.Test/Extensions/DaprJobsServiceCollectionExtensionsTests.cs @@ -13,9 +13,9 @@ using System; using System.Net.Http; +using System.Threading.Tasks; using Dapr.Jobs.Extensions; using Microsoft.Extensions.DependencyInjection; -using Xunit; namespace Dapr.Jobs.Test.Extensions; @@ -77,6 +77,58 @@ public void AddDaprJobsClient_RegistersUsingDependencyFromIServiceProvider() Assert.Equal("dapr-api-token", client.apiTokenHeader.Value.Key); Assert.Equal("abcdef", client.apiTokenHeader.Value.Value); } + + [Fact] + public void RegisterJobsClient_ShouldRegisterSingleton_WhenLifetimeIsSingleton() + { + var services = new ServiceCollection(); + + services.AddDaprJobsClient(options => { }, ServiceLifetime.Singleton); + var serviceProvider = services.BuildServiceProvider(); + + var daprJobsClient1 = serviceProvider.GetService(); + var daprJobsClient2 = serviceProvider.GetService(); + + Assert.NotNull(daprJobsClient1); + Assert.NotNull(daprJobsClient2); + + Assert.Same(daprJobsClient1, daprJobsClient2); + } + + [Fact] + public async Task RegisterJobsClient_ShouldRegisterScoped_WhenLifetimeIsScoped() + { + var services = new ServiceCollection(); + + services.AddDaprJobsClient(options => { }, ServiceLifetime.Scoped); + var serviceProvider = services.BuildServiceProvider(); + + await using var scope1 = serviceProvider.CreateAsyncScope(); + var daprJobsClient1 = scope1.ServiceProvider.GetService(); + + await using var scope2 = serviceProvider.CreateAsyncScope(); + var daprJobsClient2 = scope2.ServiceProvider.GetService(); + + Assert.NotNull(daprJobsClient1); + Assert.NotNull(daprJobsClient2); + Assert.NotSame(daprJobsClient1, daprJobsClient2); + } + + [Fact] + public void RegisterJobsClient_ShouldRegisterTransient_WhenLifetimeIsTransient() + { + var services = new ServiceCollection(); + + services.AddDaprJobsClient(options => { }, ServiceLifetime.Transient); + var serviceProvider = services.BuildServiceProvider(); + + var daprJobsClient1 = serviceProvider.GetService(); + var daprJobsClient2 = serviceProvider.GetService(); + + Assert.NotNull(daprJobsClient1); + Assert.NotNull(daprJobsClient2); + Assert.NotSame(daprJobsClient1, daprJobsClient2); + } private class TestSecretRetriever { diff --git a/test/Dapr.Messaging.Test/Extensions/PublishSubscribeServiceCollectionExtensionsTests.cs b/test/Dapr.Messaging.Test/Extensions/PublishSubscribeServiceCollectionExtensionsTests.cs new file mode 100644 index 000000000..d239fb86d --- /dev/null +++ b/test/Dapr.Messaging.Test/Extensions/PublishSubscribeServiceCollectionExtensionsTests.cs @@ -0,0 +1,76 @@ +using Dapr.Messaging.PublishSubscribe; +using Dapr.Messaging.PublishSubscribe.Extensions; +using Microsoft.Extensions.DependencyInjection; + +namespace Dapr.Messaging.Test.Extensions; + +public sealed class PublishSubscribeServiceCollectionExtensionsTests +{ + [Fact] + public void AddDaprPubSubClient_RegistersIHttpClientFactory() + { + var services = new ServiceCollection(); + + services.AddDaprPubSubClient(); + + var serviceProvider = services.BuildServiceProvider(); + + var httpClientFactory = serviceProvider.GetService(); + Assert.NotNull(httpClientFactory); + + var daprPubSubClient = serviceProvider.GetService(); + Assert.NotNull(daprPubSubClient); + } + + [Fact] + public void RegisterPubsubClient_ShouldRegisterSingleton_WhenLifetimeIsSingleton() + { + var services = new ServiceCollection(); + + services.AddDaprPubSubClient(lifetime: ServiceLifetime.Singleton); + var serviceProvider = services.BuildServiceProvider(); + + var daprPubSubClient1 = serviceProvider.GetService(); + var daprPubSubClient2 = serviceProvider.GetService(); + + Assert.NotNull(daprPubSubClient1); + Assert.NotNull(daprPubSubClient2); + + Assert.Same(daprPubSubClient1, daprPubSubClient2); + } + + [Fact] + public async Task RegisterPubsubClient_ShouldRegisterScoped_WhenLifetimeIsScoped() + { + var services = new ServiceCollection(); + + services.AddDaprPubSubClient(lifetime: ServiceLifetime.Scoped); + var serviceProvider = services.BuildServiceProvider(); + + await using var scope1 = serviceProvider.CreateAsyncScope(); + var daprPubSubClient1 = scope1.ServiceProvider.GetService(); + + await using var scope2 = serviceProvider.CreateAsyncScope(); + var daprPubSubClient2 = scope2.ServiceProvider.GetService(); + + Assert.NotNull(daprPubSubClient1); + Assert.NotNull(daprPubSubClient2); + Assert.NotSame(daprPubSubClient1, daprPubSubClient2); + } + + [Fact] + public void RegisterPubsubClient_ShouldRegisterTransient_WhenLifetimeIsTransient() + { + var services = new ServiceCollection(); + + services.AddDaprPubSubClient(lifetime: ServiceLifetime.Transient); + var serviceProvider = services.BuildServiceProvider(); + + var daprPubSubClient1 = serviceProvider.GetService(); + var daprPubSubClient2 = serviceProvider.GetService(); + + Assert.NotNull(daprPubSubClient1); + Assert.NotNull(daprPubSubClient2); + Assert.NotSame(daprPubSubClient1, daprPubSubClient2); + } +}