其他分享
首页 > 其他分享> > workflow创建分支工作流

workflow创建分支工作流

作者:互联网

1.创建 工作流

namespace 工作流.Models
{
 
   public class OutcomeWorkflow : IWorkflow<MyData>
    {
        public string Id => "参数";
        public int Version => 1;

        public void Build(IWorkflowBuilder<MyData> builder)
        {
            var branch1 = builder.CreateBranch()
                .StartWith<PrintMessage>()
                    .Input(step => step.Message, data => "调用 PrintMessage 来自分支1")
                .Then<PrintMessage>()
                    .Input(step => step.Message, data => "分支1执行完毕!");

            var branch2 = builder.CreateBranch()
                .StartWith<PrintMessage>()
                    .Input(step => step.Message, data => "调用 PrintMessage 来自分支2")
                .Then<PrintMessage>()
                    .Input(step => step.Message, data => "分支2执行完毕!");


            builder
                .StartWith<SayHello>()
                .Decide(data => data.Value)
                    .Branch(1, branch1)
                    .Branch(2, branch2);
        }
    }
   public class MyData
    {

        public int Value { get; set; }
    }
}

2.创建 步骤体(stepbody)   PrintMessage

        public class PrintMessage : StepBody
        {
            public string Message { get; set; }

            public override ExecutionResult Run(IStepExecutionContext context)
            {
                Console.WriteLine("执行了 printMessage::"+Message);
                return ExecutionResult.Next();
            }
        }

 

3.创建步骤体  sayhello

 public class SayHello : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            Console.WriteLine(" 执行sayHello");
            return ExecutionResult.Next();
        }
    }

 

4.创建控制器 执行工作流

  创建工作流之前 要在 切记  startUp.cs  ConfigureServices 方法中注册工作流

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddWorkflow();//必须添加此代码
            services.AddControllersWithViews();
        }

 

 

 

 public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IServiceProvider _serviceprovider = null;
        public HomeController(ILogger<HomeController> logger, IServiceProvider serviceprovider)
        {
            _logger = logger;
            _serviceprovider = serviceprovider;
        }

     public IActionResult outCome()
        {

            //start the workflow host
            var host = _serviceprovider.GetService<IWorkflowHost>();
            host.RegisterWorkflow<OutcomeWorkflow, MyData>();
            host.Start();

            Console.WriteLine("启动工作流...");
            host.StartWorkflow("参数", new MyData { Value = 2 });//传入参数 调用分支2


            Console.ReadLine();
            host.Stop();


            return Json(null) ;
        
        }

}

 5.执行效果如下

 

标签:workflow,创建,step,host,serviceprovider,Message,data,public,分支
来源: https://www.cnblogs.com/sunzzhankui/p/16641646.html