LogoLogo
GitHub
  • Elsa Workflows 3
  • Getting Started
    • Concepts
      • Outcomes
      • Correlation ID
    • Hello World
    • Prerequisites
    • Packages
    • Containers
      • Docker
      • Docker Compose
        • Elsa Server + Studio
        • Elsa Server + Studio - Single Image
        • Persistent Database
        • Traefik
  • Application Types
    • Elsa Server
    • Elsa Studio
    • Elsa Server + Studio (WASM)
  • Guides
    • HTTP Workflows
      • Programmatic
      • Designer
    • External Application Interaction
    • Loading Workflows from JSON
    • Running Workflows
      • Using Elsa Studio
      • Using a Trigger
      • Dispatch Workflow Activity
  • Activities
    • Control Flow
      • Decision
    • MassTransit
      • Tutorial
  • Expressions
    • C#
    • JavaScript
    • Python
    • Liquid
  • Extensibility
    • Custom Activities
  • Reusable Triggers (3.5-preview)
  • Multitenancy
    • Introduction
    • Setup
  • Operate
    • Variables
    • Activation Strategies
    • Incidents
      • Strategies
      • Configuration
    • Alterations
      • Alteration Plans
        • REST API
      • Applying Alterations
        • REST API
        • Extensibility
  • Optimize
    • Log Persistence
    • Retention
  • Hosting
    • Distributed Hosting
Powered by GitBook
On this page
  • Console
  • ASP.NET Core
  • Summary
  • Source Code
Edit on GitHub
  1. Getting Started

Hello World

In this topic, we'll setup a simple Console and an ASP.NET Core application that can host and execute workflows.

PreviousCorrelation IDNextPrerequisites

Last updated 11 days ago

Console

1

Create Console App

Start by creating a new console application:

dotnet new console -n "ElsaConsole"
2

Add Packages

Navigate to your newly created project's root directory and add the following packages:

cd ElsaConsole
dotnet add package Elsa
3

Modify Program.cs

Open Program.cs and replace its contents with the following:

using Elsa.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Elsa.Extensions;
using Elsa.Workflows.Activities;
using Elsa.Workflows;
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();

// Instantiate an activity to run.
var activity = new Sequence
{
    Activities =
    {
        new WriteLine("Hello World!"),
        new WriteLine("We can do more than a one-liner!")
    }
};

// Resolve a workflow runner to execute the activity.
var workflowRunner = serviceProvider.GetRequiredService<IWorkflowRunner>();

// Execute the activity.
await workflowRunner.RunAsync(activity);

This code sets up a service container and adds Elsa services to it. The serviceProvider can be used to resolve Elsa services and run workflows.

ASP.NET Core

1

Create the Project

Create a new empty ASP.NET app using the following command:

dotnet new web -n "ElsaWeb"
2

Add Packages

Navigate to your project's root directory and install the Elsa package:

cd ElsaWeb
dotnet add package Elsa
dotnet add package Elsa.Http
3

Modify Program.cs

Open Program.cs in your project and replace its contents with the code provided below.

Program.cs

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

Add HttpHelloWorld Workflow

Create a new directory called Workflows and add a new file to it called HttpHelloWorld.cs with the following.

Workflows/HttpHelloWorld.cs

using Elsa.Http;
using Elsa.Workflows;
using Elsa.Workflows.Activities;

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

This document explains setting up Console and ASP.NET Core apps using Elsa workflows. For the Console app, we configured a service container, added Elsa, and ran a "Hello World" workflow. The ASP.NET Core app integrates Elsa with HTTP endpoints to process workflows. Follow the code samples for package additions and Program.cs configurations. Refer to source code links for further details.

Source Code

Console app
ASP.NET Core app