Elsa 简介
Elsa 是一个基于MIT协议的.Net的开源工作流引擎,也就是你可以基于它修改源码及进行商业化。与其他工作流产品的区别是首先它可以很轻量级的嵌入到你的应用里,为你的应用提供流程化的能力; 其次它有设计器、API可以可视化的进行流程设计; 再次,它提供性能或功能的扩展能力,扩展性强。
配置Elsa工作流引擎
简单配置Elsa 的Hello World应用程序使用Http请求作为触发条件和Http Response作为输出。
- 创建一个项目
dotnet new web -n "ElsaQuickstarts.Server.DashboardAndServer"
进入该项目目录
cd ElsaQuickstarts.Server.DashboardAndServer
添加项目包引用
dotnet add package Elsa
dotnet add package Elsa.Activities.Http
dotnet add package Elsa.Activities.Temporal.Quartz
dotnet add package Elsa.Persistence.EntityFramework.Sqlite
dotnet add package Elsa.Server.Api
dotnet add package Elsa.Designer.Components.Web使用Visual Studio修改Setup文件,使用如下内容替换原有内容。
using Elsa;
using Elsa.Persistence.EntityFramework.Core.Extensions;
using Elsa.Persistence.EntityFramework.Sqlite;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace ElsaQuickstarts.Server.DashboardAndServer
{
public class Startup
{
public Startup(IWebHostEnvironment environment, IConfiguration configuration)
{
Environment = environment;
Configuration = configuration;
}
private IWebHostEnvironment Environment { get; }
private IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
var elsaSection = Configuration.GetSection("Elsa");
// Elsa services.
services
.AddElsa(elsa => elsa
.UseEntityFrameworkPersistence(ef => ef.UseSqlite())
.AddConsoleActivities()
.AddHttpActivities(elsaSection.GetSection("Server").Bind)
.AddQuartzTemporalActivities()
.AddWorkflowsFrom<Startup>()
);
// Elsa API endpoints.
services.AddElsaApiEndpoints();
// For Dashboard.
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app)
{
if (Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app
.UseStaticFiles() // For Dashboard.
.UseHttpActivities()
.UseRouting()
.UseEndpoints(endpoints =>
{
// Elsa API Endpoints are implemented as regular ASP.NET Core API controllers.
endpoints.MapControllers();
// For Dashboard.
endpoints.MapFallbackToPage("/_Host");
});
}
}
}创建Pages文件夹,并建立_Host.cshtml文件,使用如下内容替换原文件。
@page "/"
@{
var serverUrl = $"{Request.Scheme}://{Request.Host}";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Elsa Workflows</title>
<link rel="icon" type="image/png" sizes="32x32" href="/_content/Elsa.Designer.Components.Web/elsa-workflows-studio/assets/images/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/_content/Elsa.Designer.Components.Web/elsa-workflows-studio/assets/images/favicon-16x16.png">
<link rel="stylesheet" href="/_content/Elsa.Designer.Components.Web/elsa-workflows-studio/assets/fonts/inter/inter.css">
<link rel="stylesheet" href="/_content/Elsa.Designer.Components.Web/elsa-workflows-studio/elsa-workflows-studio.css">
<script src="/_content/Elsa.Designer.Components.Web/monaco-editor/min/vs/loader.js"></script>
<script type="module" src="/_content/Elsa.Designer.Components.Web/elsa-workflows-studio/elsa-workflows-studio.esm.js"></script>
</head>
<body>
<elsa-studio-root server-url="@serverUrl" monaco-lib-path="_content/Elsa.Designer.Components.Web/monaco-editor/min">
<elsa-studio-dashboard></elsa-studio-dashboard>
</elsa-studio-root>
</body>
</html>项目解决方案如下图
在Visual Studio中启动该服务
创建和测试Hello World流程
启动成功之后在流程器中输入https://localhost:5001, 如果是http访问,地址是http://localhost:5000 .
在流程定义(Workflow Definitions)中新建一个流程,这里借用Elsa官网的Gif图片来说明步骤。
创建好流程之后就可以在浏览器中输入地址进行测试。
初步性能测试
使用Apache并发100个请求1000次,在完全默认的情况下得到如下结果。
可能是采用SQLLite作为持久化性能受到影响。暂时先不做评论。
参考资料
ASP.NET Core Server with Elsa Dashboard + Elsa Server API Endpoints