網頁:https://elsa-workflows.github.io/elsa-core/docs/guides-hello-world-console
在本節中,我們將執行以下操作:
- 以編程方式定義一個工作流,該定義將文本“ Hello World”顯示到控制台。
- 運行工作流。
讓我們開始吧!
創建控制台項目
創建一個名為Elsa.Guides.HelloWorld.ConsoleApp的新.NET Core控制台項目,並添加以下軟件包:
- Elsa.Core
- Elsa.Activities.Console
定義工作流
打開Program.cs並插入以下代碼:

using System; using System.Threading.Tasks; using Elsa.Activities.Console.Activities; using Elsa.Activities.Console.Extensions; using Elsa.Expressions; using Elsa.Extensions; using Elsa.Services; using Microsoft.Extensions.DependencyInjection; namespace Elsa.Guides.HelloWorld.ConsoleApp { class Program { static async Task Main(string[] args) { // Setup a service collection. var services = new ServiceCollection() // Add essential workflow services. .AddElsa() // Add Console activities (ReadLine and WriteLine). .AddConsoleActivities() .BuildServiceProvider(); // Get a workflow builder. var workflowBuilder = services.GetRequiredService<IWorkflowBuilder>(); // Define a workflow and add a single activity. var workflowDefinition = workflowBuilder .StartWith<WriteLine>(x => x.TextExpression = new LiteralExpression("Hello world!")) .Build(); // Get a workflow invoker, var invoker = services.GetService<IWorkflowInvoker>(); // Start the workflow. await invoker.StartAsync(workflowDefinition); // Prevent the console from shutting down until user hits a key. System.Console.ReadLine(); } } }
運行
運行該程序時,應該看到以下輸出:
Hello world!
總結
在本節中,我們了解了如何創建簡單的控制台應用程序以及如何使用IWorkflowBuilder實現一個工作流。 然后,我們使用IWorkflowInvoker執行工作流。
源碼
https://github.com/elsa-workflows/elsa-guides/tree/master/src/Elsa.Guides.HelloWorld.ConsoleApp