Finish Hello World ASP.NET sample project
This commit is contained in:
parent
b3f7cb1f17
commit
088158ec54
|
|
@ -5,12 +5,25 @@ using Microsoft.AspNetCore.Http;
|
|||
|
||||
namespace Elsa.Http;
|
||||
|
||||
[Activity("Elsa", "HTTP", "Write an HTTP response.", DisplayName = "HTTP Response")]
|
||||
/// <summary>
|
||||
/// Write a response to the current HTTP response object.
|
||||
/// </summary>
|
||||
[Activity("Elsa", "HTTP", "Write a response to the current HTTP response object.", DisplayName = "HTTP Response")]
|
||||
public class WriteHttpResponse : Activity
|
||||
{
|
||||
/// <summary>
|
||||
/// The status code to return.
|
||||
/// </summary>
|
||||
[Input(DefaultValue = HttpStatusCode.OK, Description = "The status code to return.")]
|
||||
public Input<HttpStatusCode> StatusCode { get; set; } = new(HttpStatusCode.OK);
|
||||
|
||||
/// <summary>
|
||||
/// The content to write back.
|
||||
/// </summary>
|
||||
[Input(Description = "The content to write back.")]
|
||||
public Input<string?> Content { get; set; } = new("");
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
|
||||
{
|
||||
var httpContextAccessor = context.GetRequiredService<IHttpContextAccessor>();
|
||||
|
|
@ -18,13 +31,13 @@ public class WriteHttpResponse : Activity
|
|||
|
||||
if (httpContext == null)
|
||||
{
|
||||
// We're executing in a non-HTTP context (like in a virtual actor).
|
||||
// Create a bookmark to allow the invoker to get the export state and resume execution from there.
|
||||
// We're executing in a non-HTTP context (e.g. in a virtual actor).
|
||||
// Create a bookmark to allow the invoker to export the state and resume execution from there.
|
||||
|
||||
context.CreateBookmark(OnResumeAsync);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var response = httpContext.Response;
|
||||
|
||||
response.StatusCode = (int)context.Get(StatusCode);
|
||||
|
|
@ -45,7 +58,7 @@ public class WriteHttpResponse : Activity
|
|||
// We're not in an HTTP context, so let's fail.
|
||||
throw new Exception("Cannot execute in a non-HTTP context");
|
||||
}
|
||||
|
||||
|
||||
var response = httpContext.Response;
|
||||
|
||||
response.StatusCode = (int)context.Get(StatusCode);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
using System.Net;
|
||||
using Elsa.Http;
|
||||
using Elsa.Workflows.Core.Activities;
|
||||
using Elsa.Workflows.Core.Services;
|
||||
|
||||
namespace Elsa.Samples.HelloWorld;
|
||||
|
||||
public class HelloWorldHttpWorkflow : WorkflowBase
|
||||
{
|
||||
protected override void Build(IWorkflowBuilder builder)
|
||||
{
|
||||
builder.Root = new Sequence
|
||||
{
|
||||
Activities =
|
||||
{
|
||||
new HttpEndpoint
|
||||
{
|
||||
Path = new("/hello-world"),
|
||||
SupportedMethods = new(new[] { HttpMethods.Get }),
|
||||
CanStartWorkflow = true
|
||||
},
|
||||
new WriteHttpResponse
|
||||
{
|
||||
StatusCode = new(HttpStatusCode.OK),
|
||||
Content = new("Hello world!")
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +1,19 @@
|
|||
using Elsa.Extensions;
|
||||
using Elsa.Http.Extensions;
|
||||
using Elsa.Workflows.Management.Extensions;
|
||||
using Elsa.Samples.HelloWorld;
|
||||
using Elsa.Workflows.Runtime.Extensions;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
var services = builder.Services;
|
||||
|
||||
// Add Elsa services.
|
||||
services.AddElsa(elsa => elsa
|
||||
|
||||
.UseWorkflowManagement()
|
||||
// Configure the workflow runtime.
|
||||
.UseWorkflowRuntime(runtime =>
|
||||
{
|
||||
// Register our HTTP workflow with the runtime.
|
||||
runtime.AddWorkflow<HelloWorldHttpWorkflow>();
|
||||
})
|
||||
|
||||
// Enable Elsa HTTP module (for HTTP related activities).
|
||||
.UseHttp()
|
||||
|
|
|
|||
Loading…
Reference in a new issue