前言
工作流思想在上世纪60年代就有人提出过;70年代就有人开始尝试,但是由于当时许多的限制,工作流一直没有成功的被实现;80年代才出现第一批成功的工作流系统;90年代工作流技术走向了第一个发展高峰期;90年代后至今工作流出现了很多版本,但是主旨还是不变的,为了使我们的工作变得更加高效。
通俗点,我们经常使用的OA系统上。关于一个材料的申报,又或者个人的请假。这些流程就属于工作流(工作审批流)。其中对于审批人和各个节点是可以动态操作的。
工作流可以通过数据库设计的形式实现,也可以使用第三方的框架Elsa,Workflow Core。本文使用第二种,并详细的介绍一下代码实现和json实现工作流程。
安装
使用Nuget包管理工具安装以下的包:
Workflow Core 3.10.0 核心包不解释
WorkflowCore.DSL 3.10.0 json或者yaml注入需要
WorkflowCore.Persistence.MySQL 持久化
文档
JSON / YAML Definitions - Workflow Core
https://workflow-core.readthedocs.io/en/latest/json-yaml/
WorkFlowCore 加载Json文件 - 编程代码 (cscoder.cn)
https://www.cscoder.cn/docs/dotnet-core-projects-workflow-core/workflow-core-json.html
例子
1、创建项目
控制台或者WebApi项目均可,这里拿WebApi项目举例。(版本用的是.net 6)
2、添加工步
工步文件需要继承StepBody。现在添加一个Hello.cs和Goodbye的工步,代码如下:
using WorkflowCore.Interface;
using WorkflowCore.Models;
namespace WorkFlowCore_Test.WorkFlows.HelloWord.Steps
{
public class Hello : StepBody
{
public override ExecutionResult Run(IStepExecutionContext context)
{
Console.WriteLine("Hello World!");
return ExecutionResult.Next();
}
}
}
using WorkflowCore.Interface;
using WorkflowCore.Models;
namespace WorkFlowCore_Test.WorkFlows.HelloWord.Steps
{
public class Goodbye : StepBody
{
public override ExecutionResult Run(IStepExecutionContext context)
{
Console.WriteLine("Goodbye World!");
return ExecutionResult.Next();
}
}
}
工作流文件需要继承IWorkflow。现在开始添加:
using WorkflowCore.Interface;
using WorkFlowCore_Test.WorkFlows.HelloWord.Steps;
namespace WorkFlowCore_Test.WorkFlows.HelloWord
{
public class HelloWorldWorkflow : IWorkflow
{
public string Id => "HelloWorld";
public int Version => 1;
public void Build(IWorkflowBuilder<object> builder)
{
builder
.StartWith<Hello>()
.Then<Goodbye>();
}
}
}
基本数据都准备好了之后,在项目启动文件进行一下配置。代码如下:
using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq;
using WorkflowCore.Interface;
using WorkflowCore.Services;
using WorkflowCore.Services.DefinitionStorage;
using WorkFlowCore_Test.Utils;
using WorkFlowCore_Test.WorkFlows.AskForLeave.Model;
using WorkFlowCore_Test.WorkFlows.AskForLeave;
using WorkFlowCore_Test.WorkFlows.HelloWord;
using WorkFlowCore_Test.WorkFlows.IfStatement;
using WorkFlowCore_Test.WorkFlows.IfStatement.Model;
using WorkFlowCore_Test.WorkFlows.Json.Steps;
using WorkFlowCore_Test.WorkFlows.SimpleDecision;
namespace WorkFlowCore_Test
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthorization();
builder.Services.AddControllers();
builder.Services.AddLogging(); // WorkflowCore需要用到logging service
//持久化
builder.Services.AddWorkflow(cfg =>
{
cfg.UseMySQL("server=127.0.0.1;Database=Workflow;Uid=root;Pwd=xunpai123.", true, true);
});
builder.Services.AddWorkflowDSL();//用来注入json和yaml
var app = builder.Build();
//核心注入方法
UseWorkflow(app);
app.UseAuthorization();
app.MapControllers();
app.Run();
}
public static void UseWorkflow(WebApplication app)
{
var host = app.Services.GetService<IWorkflowHost>();
#region 工步注册
//c#代码注册
host?.RegisterWorkflow<HelloWorldWorkflow>();
//json注册
#endregion
host?.Start();
// 通过DI获取IHostApplicationLifetime实例
var applicationLifetime = app.Services.GetService(typeof(IHostApplicationLifetime)) as IHostApplicationLifetime;
applicationLifetime?.ApplicationStopping.Register(() =>
{
host?.Stop();
});
}
}
}
这个时候我们添加api控制器进行调用这个helloworld的流程。代码如下:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System.Net.Http.Json;
using System.Text.Json.Nodes;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using WorkflowCore.Services;
using WorkflowCore.Services.DefinitionStorage;
using WorkFlowCore_Test.Utils;
using WorkFlowCore_Test.WorkFlows.AskForLeave.Model;
using WorkFlowCore_Test.WorkFlows.SimpleDecision;
namespace WorkFlowCore_Test.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TestApiController : ControllerBase
{
private IWorkflowController WorkflowService;
private IWorkflowHost WorkflowHost;
public TestApiController(IWorkflowController workflowService, IWorkflowHost workflowHost)
{
WorkflowService = workflowService;
WorkflowHost = workflowHost;
}
[Route("WorkFlowCoreTestDemo")]
[HttpGet]
public string WorkFlowCoreTestDemo()
{
//var initialData = new LeaveRequestData
//{
// RequestId = "11111",
// RequestMsg = "开始请假"
//};
//WorkflowHost.StartWorkflow("LeaveRequestWorkflow", 1, initialData);
WorkflowService.StartWorkflow("HelloWorld");
return "true";
}
}
}
启动后调用我们的控制器方法,然后观看console 的变化,如图: