elsa-core—2.Hello World: HTTP
2021-08-16 23:39
1566 查看
在本快速入门中,我们将了解一个执行工作流的最小 ASP.NET Core 应用程序。工作流将侦听传入的 HTTP 请求并写回一个简单的响应。
我们将:
- 创建 ASP.NET Core 应用程序。
- 使用 Elsa 的 Workflow Builder API 以编程方式定义工作流定义,当 HTTP 请求从指定的 URL 传入时,该 API 会执行。
项目
创建一个名为 ElsaQuickstarts.WebApp.HelloWorld 的新的空 ASP.NET Core 项目:
dotnet new web -n "ElsaQuickstarts.WebApp.HelloWorld"
CD 到创建的项目文件夹中:
cd ElsaQuickstarts.WebApp.HelloWorld
添加以下包:
dotnet add package Elsa dotnet add package Elsa.Activities.Http
工作流程
创建一个名为 HelloWorld.cs 的新文件并添加以下代码:
using System.Net; using Elsa.Activities.Http; using Elsa.Builders; namespace ElsaQuickstarts.WebApp.HelloWorld { /// <summary> /// A workflow that is triggered when HTTP requests are made to /hello-world and writes a response. /// </summary> public class HelloWorld : IWorkflow { public void Build(IWorkflowBuilder builder) { builder .HttpEndpoint("/hello-world") .WriteHttpResponse(HttpStatusCode.OK, "<h1>Hello World!</h1>", "text/html"); } } }
上述工作流有两个活动。第一个活动 HttpEndpoint 表示一个 HTTP 端点,可以使用 HTTP 客户端(包括 Web 浏览器)调用该端点。第一个活动连接到第二个活动 WriteHttpResponse,后者将响应写入 HTTP 客户端。 当活动如本例所示链接时,第二个活动连接到第一个活动的“完成”结果。您还可以明确连接到“完成”结果,如下所示: builder .HttpEndpoint("/hello-world") .When(OutcomeNames.Done) .WriteHttpResponse(HttpStatusCode.OK, "<h1>Hello World!</h1>", "text/html"); 任何时候您想连接到“Done”以外的结果时,都需要明确连接到活动的结果 |
Startup
接下来,打开 Startup.cs 并将其内容替换为以下内容:
using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; namespace ElsaQuickstarts.WebApp.HelloWorld { public class Startup { public void ConfigureServices(IServiceCollection services) { services .AddElsa(options => options .AddHttpActivities() .AddWorkflow<HelloWorld>()); } public void Configure(IApplicationBuilder app) { app.UseHttpActivities(); } } }
运行
运行程序并等待,直到看到以 56c 下输出:
Now listening on: http://localhost:5000 Now listening on: https://localhost:5001 Application started. Press Ctrl+C to shut down. |
打开 Web 浏览器并导航到 https://localhost:5001/hello-world。
结果应如下所示:
相关文章推荐
- Getting a JSTL error message "The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- nginx教程第七篇:ngx_http_core_module模块提供的变量
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in ei
- c# .net core HttpWebRequest 使用Headers[]方法代替UserAgent
- ASP.NET Core中间件计算Http请求时间示例详解
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 出错
- Can not find the tag library descriptor for “http://java.sun.com/jstl/core”
- linux下调试core的命令,察看堆栈状态命令 摘录(http://blog.csdn.net/yearn520/article/details/6663265)
- java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
- [.NET]Fiddler Core实现HTTP HTTPS抓包分析 数据篡改 全局监听
- ASP.NET Core HTTP 管道中的那些事儿
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- 在JSP中This absolute uri http://java.sun.com/jsp/jstl/core cannot be resolved ……的解
- 使用Maven配置Jstl出现异常:The absolute uri: http://java.sun.com/jsp/jstl/core cannot...
- Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core" 解决办法
- HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
- 畅游HttpCore