Hello World
In this topic, we'll setup a simple Console and an ASP.NET Core application that can host and execute workflows.
Console
1
dotnet new console -n "ElsaConsole"2
cd ElsaConsole
dotnet add package Elsa3
using Elsa.Extensions;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Microsoft.Extensions.DependencyInjection;
// Setup service container.
var services = new ServiceCollection();
// Add Elsa services to the container.
services.AddElsa();
// Build the service container.
var serviceProvider = services.BuildServiceProvider();
// Define a simple workflow with multiple activities.
var workflow = new Sequence
{
Activities =
{
new WriteLine("Hello World!"),
new WriteLine("We can do more than a one-liner!")
}
};
// Resolve a workflow runner to execute the workflow.
var workflowRunner = serviceProvider.GetRequiredService<IWorkflowRunner>();
// Run the workflow.
await workflowRunner.RunAsync(workflow);ASP.NET Core
1
dotnet new web -n "ElsaWeb"2
cd ElsaWeb
dotnet add package Elsa
dotnet add package Elsa.Http3
using Elsa.Extensions;
using ElsaWeb.Workflows;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddElsa(elsa =>
{
elsa.AddWorkflow<HttpHelloWorld>();
elsa.UseHttp(http => http.ConfigureHttpOptions = options =>
{
options.BaseUrl = new Uri("https://localhost:5001");
options.BasePath = "/workflows";
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.UseWorkflows();
app.Run();4
using Elsa.Http;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;
namespace ElsaWeb.Workflows;
public class HttpHelloWorld : WorkflowBase
{
protected override void Build(IWorkflowBuilder builder)
{
var queryStringsVariable = builder.WithVariable<IDictionary<string, object>>();
var messageVariable = builder.WithVariable<string>();
builder.Root = new Sequence
{
Activities =
{
new HttpEndpoint
{
Path = new("/hello-world"),
CanStartWorkflow = true,
QueryStringData = new(queryStringsVariable)
},
new SetVariable
{
Variable = messageVariable,
Value = new(context =>
{
var queryStrings = queryStringsVariable.Get(context)!;
var message = queryStrings.TryGetValue("message", out var messageValue) ? messageValue.ToString() : "Hello world of HTTP workflows!";
return message;
})
},
new WriteHttpResponse
{
Content = new(messageVariable)
}
}
};
}
}Summary
Source Code
Last updated