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