需要安装Nuget包Install-Package WorkflowCore.Users
使用方法
在构建工作流时,使用.userTask拓展方法,其中参数2是用于解析此步骤将分配给那些用户
public class HumanWorkflow : IWorkflow
{
...
public void Build(IWorkflowBuilder<object> builder)
{
builder
.StartWith(context => .WriteLine("start"))
.UserTask("Do you approve", data => "MYDOMAIN\\user")
.WithOption("yes", "I approve").Do(then => then
.StartWith(context => Console.WriteLine("You approved"))
)
.WithOption("no", "I do not approve").Do(then => then
.StartWith(context => Console.WriteLine("You did not approve"))
)
.Then(context => Console.WriteLine("end"));
}
}
使用WorkflowHost服务上的. getopenuseractions获取给定工作流的可用用户操作列表,
var openItems = host.GetOpenUserActions(workflowId);
然后使用. publishuseraction响应给定工作流的开放用户操作
host.PublishUserAction(openItems.First().Key, "MYDOMAIN\\someuser", chosenValue);
案例:
Workflow:
public class HumanWorkflow : IWorkflow
{
public string Id => "HumanWorkflow";
public int Version => 1;
public void Build(IWorkflowBuilder<object> builder)
{
builder
.StartWith(context => ExecutionResult.Next())
.UserTask("Do you approve", data => @"domain\bob")
.WithOption("yes", "I approve").Do(then => then
.StartWith(context => Console.WriteLine("You approved"))
)
.WithOption("no", "I do not approve").Do(then => then
.StartWith(context => Console.WriteLine("You did not approve"))
)
.WithEscalation(x => TimeSpan.FromSeconds(20), x => @"domain\frank", action => action
.StartWith(context => Console.WriteLine("Escalated task"))
.Then(context => Console.WriteLine("Sending notification..."))
)
.Then(context => Console.WriteLine("end"));
}
}
program
var host = serviceProvider.GetService<IWorkflowHost>();
host.RegisterWorkflow<HumanWorkflow>();
host.Start();
Console.WriteLine("Starting workflow...");
string workflowId = host.StartWorkflow("HumanWorkflow").Result;
var timer = new Timer(new TimerCallback((state) => { PrintOptions(host, workflowId); }), null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10));
Thread.Sleep(1000);
Console.WriteLine();
Console.WriteLine("Open user actions are");
var openItems = host.GetOpenUserActions(workflowId);
foreach (var item in openItems)
{
Console.WriteLine(item.Prompt + ", Assigned to " + item.AssignedPrincipal);
Console.WriteLine("Options are ");
foreach (var option in item.Options)
{
Console.WriteLine(" - " + option.Key + " : " + option.Value + ", ");
}
//Thread.Sleep(500);
var input = Console.ReadLine();
Console.WriteLine();
string key = item.Key;
string value = item.Options.Single(x => x.Value == input).Value;
Console.WriteLine("Choosing key:" + key + " value:" + value);
host.PublishUserAction(key, @"domain\john", value).Wait();
}
Thread.Sleep(1000);
Console.WriteLine("Open user actions left:" + host.GetOpenUserActions(workflowId).Count().ToString());
timer.Dispose();
timer = null;
Console.WriteLine("Workflow ended.");
Console.ReadLine();
host.Stop();
private static void PrintOptions(IWorkflowHost host, string workflowId)
{
var openItems = host.GetOpenUserActions(workflowId);
foreach (var item in openItems)
{
Console.WriteLine(item.Prompt + ", Assigned to " + item.AssignedPrincipal);
Console.WriteLine("Options are ");
foreach (var option in item.Options)
{
Console.WriteLine(" - " + option.Key + " : " + option.Value + ", ");
}
}
}