Fork me on GitHub

小试elsa

 

 

 

  最近工作需要,在调研BMP产品(开源和商用),重点了解了activiti和它的商业产品Alfresco Process Services,这是java的体系,成熟,完善(三方开源库是java多年开源积累下的最宝贵的财富),出身.neter的我自然就想看看.net世界里有没有类似的产品。于是就找到两篇关于elsa的文章,一篇是善友兄的《推荐一个很棒的开源工作流elsa-core》,另一篇国庆兄的《.NET 5 开源工作流框架elsa技术研究》,于是还专门致电国庆兄了解elsa这个开源框架的状况,国庆兄很细致的给我作了分享和演示(为开源精神,分享精神点赞),感谢。

  于是我也小试一把elsa,同时也把自己的小心得分享出来.

  demo的思路很简单,就是从一个api中请求信息,显示出来,这里用到两个activity:一个SendHttpRequest,一个Writeline。

 定义webapi服务:

namespace APIDemo.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class HomeController : ControllerBase
    {
        private readonly ILogger<HomeController> _logger;
        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }
        [HttpGet]
        public Person GetPerson()
        {
            _logger.LogInformation("请求person");
            return new Person() { ID = 100, Name = "张三丰收", Age = 22, Sex = true };
        }
    }
    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public bool Sex { get; set; }
    }
}

定义elsa工作流:

using Elsa.Activities.Console.Activities;
using Elsa.Activities.Http.Activities;
using Elsa.Activities.Http.Parsers;
using Elsa.Activities.Http.Services;
using Elsa.Expressions;
using Elsa.Scripting.JavaScript;
using Elsa.Services;
using Elsa.Services.Models;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;

namespace Elsa01
{
    class Program
    {
        async static Task Main(string[] args)
        {
            Console.WriteLine("回车开始");
            Console.ReadLine();
            var services = new ServiceCollection()
              .AddElsa()
              //注入Response返回格式
              .AddScoped<IHttpResponseBodyParser, JsonHttpResponseBodyParser>()
              //注入httpclient工厂
              .AddHttpClient()
              .AddActivity<SendHttpRequest>()
              .AddActivity<WriteLine>()
              .BuildServiceProvider();
            var invoker = services.GetService<IWorkflowInvoker>();
            await invoker.StartAsync<PersonHandleWorkflow>();
            Console.WriteLine("回车结束");
            Console.ReadLine();
        }
    }
    /// <summary>
    /// 定义工作流两个activity,一个是请求webapi,一个是显示请求结果
    /// </summary>
    public class PersonHandleWorkflow : IWorkflow
    {
        public void Build(IWorkflowBuilder builder)
        {
            builder
                .StartWith<SendHttpRequest>(RequestAPI, "http5001")
                .Then<WriteLine>(ShowMessage);
        }
        void ShowMessage(WriteLine write)
        {
            //用javascript脚本来处理返回信息
            write.TextExpression = new JavaScriptExpression<string>("'ID:'+lastResult('http5001').Content[\"id\"]+'  Name:'+lastResult('http5001').Content[\"name\"]+'  Age:'+lastResult('http5001').Content[\"age\"]");
        }
        void RequestAPI(SendHttpRequest http)
        {
            //定义http发送参数
            http.ContentType = "application/json";
            http.Method = "get";
            http.Url = new WorkflowExpression<Uri>(LiteralEvaluator.SyntaxName, "https://localhost:5001/home");
        }
    }
}

 

  想要更快更方便的了解相关知识,可以关注微信公众号 
 

 

 

posted @ 2022-02-02 20:28  桂素伟  阅读(242)  评论(0编辑  收藏  举报