.Net工作流elsa-workflows官方文檔學習:一個簡單的Hello World工作流


網頁: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();
        }
    }
}
View Code

運行

運行該程序時,應該看到以下輸出:

Hello world!

總結

在本節中,我們了解了如何創建簡單的控制台應用程序以及如何使用IWorkflowBuilder實現一個工作流。 然后,我們使用IWorkflowInvoker執行工作流。

源碼

https://github.com/elsa-workflows/elsa-guides/tree/master/src/Elsa.Guides.HelloWorld.ConsoleApp

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM