.Net工作流elsa-workflows官方文档学习:创建HTTP工作流
网页:https://elsa-workflows.github.io/elsa-core/docs/guides-hello-world-http
在本节中,我们将执行以下操作:
- 创建一个ASP.NET Core应用程序。
- 以编程方式定义工作流,该工作流在HTTP请求到达指定URL时执行。
让我们开始吧!
创建ASP.NET Core项目
创建一个名为Elsa.Guides.HelloWorld.WebApp的新的空ASP.NET Core项目,并添加以下程序包:
- Elsa.Core
- Elsa.Activities.Http
创建工作流类
创建一个名为HelloWorldHttpWorkflow的新类,并添加以下代码:
using System; using System.Net; using System.Net.Http; using Elsa.Activities.Http.Activities; using Elsa.Expressions; using Elsa.Services; using Elsa.Services.Models; namespace Elsa.Guides.HelloWorld.WebApp { public class HelloWorldHttpWorkflow : IWorkflow { public void Build(IWorkflowBuilder builder) { builder .StartWith<ReceiveHttpRequest>( x => { x.Method = HttpMethod.Get.Method; x.Path = new Uri("/hello-world", UriKind.Relative); } ) .Then<WriteHttpResponse>( x => { x.Content = new LiteralExpression("<h1>Hello World!</h1>"); x.ContentType = "text/html"; x.StatusCode = HttpStatusCode.OK; x.ResponseHeaders = new LiteralExpression("X-Powered-By=Elsa Workflows"); } ); } } }
更新Startup.cs文件
接下来,打开Startup.cs并插入以下代码:
using Elsa.Activities.Http.Extensions; using Elsa.Extensions; using Elsa.Services; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; namespace Elsa.Guides.HelloWorld.WebApp { public class Startup { public void ConfigureServices(IServiceCollection services) { services .AddElsa() .AddHttpActivities() .AddWorkflow<HelloWorldHttpWorkflow>; } public void Configure(IApplicationBuilder app) { app.UseHttpActivities(); } } }
运行
运行程序并等待,直到看到以下输出:
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.
打开浏览器窗口或Postman之类的工具,然后导航至https://localhost:5001/hello-world。
结果应如下所示:
总结
由此示例可见,HTTP工作流是一种轻松实现逻辑以快速响应HTTP请求的好方法。
源码
https://github.com/elsa-workflows/elsa-guides/tree/master/src/Elsa.Guides.HelloWorld.WebApp