【愚公系列】2023年02月 .NET CORE工具案例-Workflow-Core轻量级工作流引擎(流程事件)
【摘要】 前言 1.什么是工作流工作流是OA系统比较重要的功能之一,主要在于企业流程协同审批,有效进行流程管理。流程管理起源于生产组织和办公自动化领域,是针对日常工作中具有固定程序的活动提出的一个概念。目的是通过将工作分解成定义良好的任务、角色,按照一定的规则和过程来执行这些任务并对它们进行监控,达到提高办事效率、降低生产成本、提高企业生产经营管理水平和企业竞争力的目标。现代计算机技术实现了工作流,...
前言
1.什么是工作流
工作流是OA系统比较重要的功能之一,主要在于企业流程协同审批,有效进行流程管理。
流程管理起源于生产组织和办公自动化领域,是针对日常工作中具有固定程序的活动提出的一个概念。目的是通过将工作分解成定义良好的任务、角色,按照一定的规则和过程来执行这些任务并对它们进行监控,达到提高办事效率、降低生产成本、提高企业生产经营管理水平和企业竞争力的目标。
现代计算机技术实现了工作流,是支持企业经营过程高效执行并监控其执行过程的计算机软件系统。工作流软件类似电脑桌面操作系统,操作系统驱动电脑各部件协同运转,工作流软件则驱动业务过程自动有序进行。
Workflow-Core地址:https://github.com/danielgerlag/workflow-core
一、Workflow-Core轻量级工作流引擎
1.安装包
Install-Package WorkflowCore
Microsoft.Extensions.Logging
Microsoft.Extensions.Logging.Debug
2.流程事件
模型类
public class MyDataClass
{
public string Value1 { get; set; }
}
2.1 定义步骤
1、步骤一
public class CustomMessage : StepBody
{
public string Message { get; set; }
public override ExecutionResult Run(IStepExecutionContext context)
{
Console.WriteLine(Message);
return ExecutionResult.Next();
}
}
2.2 定义工作流
public class EventSampleWorkflow : IWorkflow<MyDataClass>
{
public string Id => "EventSampleWorkflow";
public int Version => 1;
public void Build(IWorkflowBuilder<MyDataClass> builder)
{
builder
.StartWith(context => ExecutionResult.Next())
.WaitFor("MyEvent", (data, context) => context.Workflow.Id, data => DateTime.Now)
.Output(data => data.Value1, step => step.EventData)
.Then<CustomMessage>()
.Input(step => step.Message, data => "The data from the event is " + data.Value1)
.Then(context => Console.WriteLine("workflow complete"));
}
}
2.3 启动工作流
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.Interface;
using WorkflowCore.Sample04;
IServiceProvider serviceProvider = ConfigureServices();
//start the workflow host
var host = serviceProvider.GetService<IWorkflowHost>();
host.RegisterWorkflow<EventSampleWorkflow, MyDataClass>();
host.Start();
var initialData = new MyDataClass();
var workflowId = host.StartWorkflow("EventSampleWorkflow", 1, initialData).Result;
Console.WriteLine("Enter value to publish");
string value = Console.ReadLine();
host.PublishEvent("MyEvent", workflowId, value);
Console.ReadLine();
host.Stop();
static IServiceProvider ConfigureServices()
{
//setup dependency injection
IServiceCollection services = new ServiceCollection();
services.AddLogging();
services.AddWorkflow();
//services.AddWorkflow(x => x.UseMongoDB(@"mongodb://localhost:27017", "workflow"));
//services.AddWorkflow(x => x.UseSqlServer(@"Server=.;Database=WorkflowCore;Trusted_Connection=True;", true, true));
//services.AddWorkflow(x => x.UsePostgreSQL(@"Server=127.0.0.1;Port=5432;Database=workflow;User Id=postgres;", true, true));
//services.AddWorkflow(x => x.UseSqlite(@"Data Source=database.db;", true));
//services.AddWorkflow(x =>
//{
// x.UseAzureSynchronization(@"UseDevelopmentStorage=true");
// x.UseMongoDB(@"mongodb://localhost:27017", "workflow9999");
//});
//services.AddWorkflow(x =>
//{
// x.UseSqlServer(@"Server=.\SQLEXPRESS;Database=WorkflowCore;Trusted_Connection=True;", true, true);
// x.UseSqlServerLocking(@"Server=.\SQLEXPRESS;Database=WorkflowCore;Trusted_Connection=True;");
//});
//services.AddWorkflow(cfg =>
//{
// var ddbConfig = new AmazonDynamoDBConfig() { RegionEndpoint = RegionEndpoint.USWest2 };
// cfg.UseAwsDynamoPersistence(new EnvironmentVariablesAWSCredentials(), ddbConfig, "sample4");
// cfg.UseAwsDynamoLocking(new EnvironmentVariablesAWSCredentials(), ddbConfig, "workflow-core-locks");
// cfg.UseAwsSimpleQueueService(new EnvironmentVariablesAWSCredentials(), new AmazonSQSConfig() { RegionEndpoint = RegionEndpoint.USWest2 });
//});
//services.AddWorkflow(cfg =>
//{
// cfg.UseRedisPersistence("localhost:6379", "sample4");
// cfg.UseRedisLocking("localhost:6379");
// cfg.UseRedisQueues("localhost:6379", "sample4");
// cfg.UseRedisEventHub("localhost:6379", "channel1");
//});
//services.AddWorkflow(x =>
//{
// x.UseMongoDB(@"mongodb://192.168.0.12:27017", "workflow");
//x.UseRabbitMQ(new ConnectionFactory() { HostName = "localhost" });
//x.UseRedlock(redis);
//});
var serviceProvider = services.BuildServiceProvider();
return serviceProvider;
}
2.4 运行
二、Workflow-Core的流程配置
1.代码配置
public class MyWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder<MyData> builder)
{
builder
.StartWith<Task1>()
.Then<Task2>()
.Then<Task3>();
}
}
2.JSON配置
{
"Id": "HelloWorld",
"Version": 1,
"Steps": [
{
"Id": "Hello",
"StepType": "MyApp.HelloWorld, MyApp",
"NextStepId": "Bye"
},
{
"Id": "Bye",
"StepType": "MyApp.GoodbyeWorld, MyApp"
}
]
}
3.YAML配置
Id: HelloWorld
Version: 1
Steps:
- Id: Hello
StepType: MyApp.HelloWorld, MyApp
NextStepId: Bye
- Id: Bye
StepType: MyApp.GoodbyeWorld, MyApp
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)