美文网首页
Dotnet Core Webapi项目学习使用Workflow

Dotnet Core Webapi项目学习使用Workflow

作者: 夏天的夏秋天的天 | 来源:发表于2019-04-18 00:09 被阅读0次
    Workflow-core是基于.net core 的一款轻量级工作流引擎。

    Workflow Core is a light weight workflow engine targeting .NET Standard. Think: long running processes with multiple tasks that need to track state. It supports pluggable persistence and concurrency providers to allow for multi-node clusters. --来自官网文档

    GITHUB:https://github.com/danielgerlag/workflow-core
    官方文档:https://github.com/danielgerlag/workflow-core/wiki

    目前刚接触与工作流相关的项目
    以此工作流引擎来进行学习


    在WebApi项目中导入相关Nuget包
    Install-Package WorkflowCore 或 dotnet add package WorkflowCore
    也可以通过vs进行导入


    Workflow Package

    此外要需要导入需要连接的DB的包


    Mysql Workflow

    在Startup.cs中进行注入:


    ConfigureServices

    在项目中建立了一个TestWorkflow.cs 继承 IWorkflow

    public class HelloWorkFlow : IWorkflow
        {
            public string Id => nameof(helloworld);
    
            public int Version => 1;
    
            public void Build(IWorkflowBuilder<object> builder)
            {
                builder
                    .StartWith<helloworld>(); //helloworld为手动创建的一个step
            }
        }
    

    在helloworld.cs中

    public class helloworld : StepBody
        {
            private IUnitOfWork _unitOfWork;     
            private IUserRepository _userRepository;
            public helloworld(IUnitOfWork unitOfWork, IUserRepository userRepository)
            {
                _unitOfWork = unitOfWork;
                _userRepository = userRepository;
            }
           //在这中实现需要执行的方法
            public override ExecutionResult Run(IStepExecutionContext context)
            {
                //为数据库中添加一个新的用户
                TbUser user = new TbUser { UserId = Method.GetGuid32(), UserName = "WorkFlowAdd" };
    
                _userRepository.Add(user);
                _unitOfWork.Save();
                return ExecutionResult.Next();
            }
        }
    

    然后需要会带Startup.cs对写好的流进行使用

     public void ConfigureServices(IServiceCollection services)
            {
                services.AddTransient<helloworld>();
                services.AddWorkflow(x => x.UseMySQL(Configuration.GetConnectionString("mysqlconnectionString"), true, true));
            }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
                var host = app.ApplicationServices.GetService<IWorkflowHost>();
                host.RegisterWorkflow<HelloWorkFlow>();
                host.Start();
            }
    

    进入Controller中

    private readonly IUserRepository _userRepository;
            private readonly IWorkflowHost _workflowHost;
            public UserController(IWorkflowHost workflowHost)
            {
                _workflowHost = workflowHost;
            }
    //编写一个测试接口来调用
            [HttpGet("test")]
            public Message Test()
            {
                _workflowHost.StartWorkflow(nameof(HelloWorkFlow));
                return Message.Ok();
            }
    

    测试结果:
    通过Postman进行接口访问测试


    结果

    成功,看看数据库中是否生成了对应的User信息


    table_tbuser

    在运行程序时因为连接了MYSQL数据库,workflowcore会生成相应的表


    db

    Workflow表中也会生成记录


    table_workflow

    其他各表有什么作用还在学习中~~~~~~

    相关文章

      网友评论

          本文标题:Dotnet Core Webapi项目学习使用Workflow

          本文链接:https://www.haomeiwen.com/subject/ptrawqtx.html