Elsa 2.0 为什么这个工作流执行一个分叉的两个分支,而我只期望一个分支?
Elsa 2.0 Why is this workflow executing both the branches of a fork, when I am expecting only one?
Elsa 正在执行工作流的两个分支,而我只期望一个分支,A 或 B。这是控制台应用程序的文件。
为了您的方便,我推送了 same here as well。
class Program
{
static async Task Main(string[] args)
{
var services = new ServiceCollection()
.AddElsa(options => options
.AddConsoleActivities()
.AddActivity<ForkBranchDecisionActivity>()
.AddWorkflow<SimpleForkWorkflow>())
.BuildServiceProvider();
var workflowStarter = services.GetRequiredService<IBuildsAndStartsWorkflow>();
await workflowStarter.BuildAndStartWorkflowAsync<SimpleForkWorkflow>();
Console.WriteLine("Done. Good bye");
}
}
当我输入 'A' 时,结果将是 'A',否则将是 'B'。
public class ForkBranchDecisionActivity : Activity
{
protected override IActivityExecutionResult OnExecute()
{
Console.WriteLine("Choose a branch. A or B");
Console.WriteLine("Typing a will resulting in going through branch A");
Console.WriteLine("Any other key will result in branch B");
var userChoosenBranch = Console.ReadLine();
if (userChoosenBranch!.ToUpper() == "A")
return Outcome("A");
return Outcome("B");
}
}
选择分支不是根据之前activity的结果吗?如果不是那么什么决定了要执行的分支?为什么在我的案例中执行了两个分支?
public class SimpleForkWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder builder)
{
builder
.WriteLine("This demonistrates a simple workflow with fork .")
.WriteLine("Using forks we can branch a workflow.")
.Then<ForkBranchDecisionActivity>()
.Then<Fork>(
fork => fork.WithBranches("A", "B"),
fork =>
{
var aBranch = fork
.When("A")
.WriteLine("You are in A branch. First line")
.WriteLine("You are in A branch. Second line.")
.ThenNamed("AfterJoin");
var bBranch = fork
.When("B")
.WriteLine("You are in B branch. First line")
.WriteLine("You are in B branch. Second line.")
.ThenNamed("AfterJoin");
})
.WithName("AB Fork")
.Add<Join>(x => x.WithMode(Join.JoinMode.WaitAny)).WithName("AfterJoin")
.WriteLine("Workflow finished.");
}
}
我的最终控制台输出如下所示。
This demonistrates a simple workflow with fork .
Using forks we can branch a workflow.
Choose a branch. A or B
Typing a will resulting in going through branch A
Any other key will result in branch B
a
You are in B branch. First line
You are in B branch. Second line.
Workflow finished.
You are in A branch. First line
You are in A branch. Second line.
Workflow finished.
Done. Good bye
更新
谢谢@Sipke Schoorstra。乍一看并不明显。所以在这里突出显示更改。
而不是
.Then<ForkBranchDecisionActivity>()
.Then<Fork>(
应该是
.Then<ForkBranchDecisionActivity>(fork =>
{
fork.When("A")
Note Fork activity 已完全删除,不再是工作流程的一部分。
这是设计使然:Fork
activity 将安排 所有 分支。这是一种将工作流执行拆分为多个分支的方法。
这允许您做一些事情,比如在一个分支中等待一些用户输入,但在某个超时事件后继续另一个分支。这种情况的一个很好的例子是 described here.
要实施您的工作流程,您只需要:
public class SimpleForkWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder builder)
{
builder
.WriteLine("This demonstrates a simple workflow with switch.")
.WriteLine("Using switch we can branch a workflow.")
.Then<ForkBranchDecisionActivity>(fork =>
{
fork.When("A")
.WriteLine("You are in A branch. First line")
.WriteLine("You are in A branch. Second line.")
.ThenNamed("Finish");
fork.When("B")
.WriteLine("You are in B branch. First line")
.WriteLine("You are in B branch. Second line.")
.ThenNamed("Finish");
})
.WriteLine("Workflow finished.").WithName("Finish");
}
}
请注意,Then<ForkBranchDecisionActivity>
调用接受一个参数,允许您将任意结果连接到您的 activity(在您的情况下为“A”和“B”)。
当您运行此工作流程时,结果将如预期:
This demonstrates a simple workflow with switch.
Using switch we can branch a workflow.
Choose a branch. A or B
Typing 'a' will result in going through branch A
Any other key will result in branch B
a
You are in A branch. First line
You are in A branch. Second line.
Workflow finished.
Elsa 正在执行工作流的两个分支,而我只期望一个分支,A 或 B。这是控制台应用程序的文件。
为了您的方便,我推送了 same here as well。
class Program
{
static async Task Main(string[] args)
{
var services = new ServiceCollection()
.AddElsa(options => options
.AddConsoleActivities()
.AddActivity<ForkBranchDecisionActivity>()
.AddWorkflow<SimpleForkWorkflow>())
.BuildServiceProvider();
var workflowStarter = services.GetRequiredService<IBuildsAndStartsWorkflow>();
await workflowStarter.BuildAndStartWorkflowAsync<SimpleForkWorkflow>();
Console.WriteLine("Done. Good bye");
}
}
当我输入 'A' 时,结果将是 'A',否则将是 'B'。
public class ForkBranchDecisionActivity : Activity
{
protected override IActivityExecutionResult OnExecute()
{
Console.WriteLine("Choose a branch. A or B");
Console.WriteLine("Typing a will resulting in going through branch A");
Console.WriteLine("Any other key will result in branch B");
var userChoosenBranch = Console.ReadLine();
if (userChoosenBranch!.ToUpper() == "A")
return Outcome("A");
return Outcome("B");
}
}
选择分支不是根据之前activity的结果吗?如果不是那么什么决定了要执行的分支?为什么在我的案例中执行了两个分支?
public class SimpleForkWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder builder)
{
builder
.WriteLine("This demonistrates a simple workflow with fork .")
.WriteLine("Using forks we can branch a workflow.")
.Then<ForkBranchDecisionActivity>()
.Then<Fork>(
fork => fork.WithBranches("A", "B"),
fork =>
{
var aBranch = fork
.When("A")
.WriteLine("You are in A branch. First line")
.WriteLine("You are in A branch. Second line.")
.ThenNamed("AfterJoin");
var bBranch = fork
.When("B")
.WriteLine("You are in B branch. First line")
.WriteLine("You are in B branch. Second line.")
.ThenNamed("AfterJoin");
})
.WithName("AB Fork")
.Add<Join>(x => x.WithMode(Join.JoinMode.WaitAny)).WithName("AfterJoin")
.WriteLine("Workflow finished.");
}
}
我的最终控制台输出如下所示。
This demonistrates a simple workflow with fork .
Using forks we can branch a workflow.
Choose a branch. A or B
Typing a will resulting in going through branch A
Any other key will result in branch B
a
You are in B branch. First line
You are in B branch. Second line.
Workflow finished.
You are in A branch. First line
You are in A branch. Second line.
Workflow finished.
Done. Good bye
更新
谢谢@Sipke Schoorstra。乍一看并不明显。所以在这里突出显示更改。
而不是
.Then<ForkBranchDecisionActivity>()
.Then<Fork>(
应该是
.Then<ForkBranchDecisionActivity>(fork =>
{
fork.When("A")
Note Fork activity 已完全删除,不再是工作流程的一部分。
这是设计使然:Fork
activity 将安排 所有 分支。这是一种将工作流执行拆分为多个分支的方法。
这允许您做一些事情,比如在一个分支中等待一些用户输入,但在某个超时事件后继续另一个分支。这种情况的一个很好的例子是 described here.
要实施您的工作流程,您只需要:
public class SimpleForkWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder builder)
{
builder
.WriteLine("This demonstrates a simple workflow with switch.")
.WriteLine("Using switch we can branch a workflow.")
.Then<ForkBranchDecisionActivity>(fork =>
{
fork.When("A")
.WriteLine("You are in A branch. First line")
.WriteLine("You are in A branch. Second line.")
.ThenNamed("Finish");
fork.When("B")
.WriteLine("You are in B branch. First line")
.WriteLine("You are in B branch. Second line.")
.ThenNamed("Finish");
})
.WriteLine("Workflow finished.").WithName("Finish");
}
}
请注意,Then<ForkBranchDecisionActivity>
调用接受一个参数,允许您将任意结果连接到您的 activity(在您的情况下为“A”和“B”)。
当您运行此工作流程时,结果将如预期:
This demonstrates a simple workflow with switch.
Using switch we can branch a workflow.
Choose a branch. A or B
Typing 'a' will result in going through branch A
Any other key will result in branch B
a
You are in A branch. First line
You are in A branch. Second line.
Workflow finished.