- The designer is not yet fully embeddable in other applications. This is planned for a future release
- C# and Python expressions are not yet fully tested
- Bulk Dispatch Workflows is a new activity and not yet fully tested
- Input / Output is not yet implemented in the Workflow Instance Viewer
- Starting workflows from the designer is only supported for workflows that do not require input and do not start with a trigger. This is planned for a future release.
- The designer currently only supports Flowchart activities. Support for Sequence and StateMachine activities is planned for a future release.
- Long-running workflows such as order fulfillment, product approval, etc.
- Short-running workflows such as sending emails, generating PDFs, etc.
- Scheduled workflows such as sending a daily report, etc.
- Event-driven workflows such as sending a welcome email when a user signs up, etc.
## Console Example
Let's take a look at a simple example that demonstrates how to create a workflow and run it. The following example is a console application that creates a workflow that writes "Hello World!" to the console.
To build workflows that execute more than one step, choose an activity that can do so. For example, the `Sequence` activity lets us add multiple activities to execute in sequence (plumbing code left out for brevity):
```csharp
// Create a workflow.
var workflow = new Sequence
{
Activities =
{
new WriteLine("Hello World!"),
new WriteLine("Goodbye cruel world...")
}
};
```
Outputs:
```shell
Hello World!
Goodbye cruel world...
```
### Conditions
The following demonstrates a workflow where it asks the user to enter their age, and based on this, offers a beer or a soda:
```csharp
// Declare a workflow variable for use in the workflow.
var ageVariable = new Variable<string>();
// Declare a workflow.
var workflow = new Sequence
{
// Register the variable.
Variables = { ageVariable },
// Setup the sequence of activities to run.
Activities =
{
new WriteLine("Please tell me your age:"),
new ReadLine(ageVariable), // Stores user input into the provided variable.,
new If
{
// If aged 18 or up, beer is provided, soda otherwise.
Condition = new Input<bool>(context => ageVariable.Get<int>(context) <18),
Then = new WriteLine("Enjoy your soda!"),
Else = new WriteLine("Enjoy your beer!")
},
new WriteLine("Come again!")
}
};
```
Notice that:
- To capture activity output, a workflow variable (ageVariable) is used.
- Depending on the result of the condition of the `If` activity, either the `Then` or the `Else` activity is executed.
- After the If activity completes, the final WriteLine activity is executed.
When working with workflows that involve timers, messages and other events, running a simple Console application is not enough.
In this case, we need a proper host that can run the workflows in the background and handle events.
ASP.NET Core is a great host for this purpose. The following example demonstrates how to create a simple ASP.NET Core application that acts as a workflow server.
```csharp
using Elsa.Extensions;
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
// Add Elsa services.
services.AddElsa(elsa => elsa
// Add workflows from this program.
.AddWorkflowsFrom<Program>()
// Enable Elsa HTTP module for HTTP related activities.
.UseHttp()
);
// Configure ASP.NET's middleware pipeline.
var app = builder.Build();
if (app.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();
// Add Elsa HTTP middleware to handle requests mapped to HTTP Endpoint activities.
app.UseWorkflows();
// Start accepting requests.
app.Run();
```
The above example demonstrates how to:
- Add workflows from the current program.
- Enable the HTTP module to handle HTTP related activities.
### HTTP Endpoint
The following example demonstrates how to create a workflow that handles HTTP requests:
```csharp
public class HelloWorldHttpWorkflow : WorkflowBase
new WriteLine(context => $"Heartbeat at {context.GetRequiredService<ISystemClock>().UtcNow}"),
}
};
}
}
```
The above example demonstrates how to:
- Create a timer that executes every 5 seconds.
- The `CanStartWorkflow` property is set to `true` to indicate that this timer can start a workflow.
- The `Timer` activity is followed by a `WriteLine` activity that writes the current time to the console.
- The `ISystemClock` service is used to get the current time.
- The `context` parameter is used to access the service.
## Elsa Server + Elsa Studio
So far, we have seen a simple Console and ASP.NET Core application that runs workflows. However, these applications do not provide a way to design workflows.
For this, we need the following:
- Elsa Server: The ASP.NET Core application needs to expose API endpoints that can be used to design workflows.
- Elsa Studio: A Blazor application that can be used to design workflows.
To setup a simple Elsa Server application, follow these steps:
1. Create a new ASP.NET Core application.
2. Add the necessary packages
3. Make the necessary changes in Program.cs
Let's go through the above steps in detail.
### Create Elsa Server
Create a new ASP.NET Core application using the following command:
// Setup a SignalR hub for real-time updates from the server.
els.UseRealTimeWorkflows();
// Enable C# workflow expressions
elsa.UseCSharp();
// Enable HTTP activities.
elsa.UseHttp();
// Use timer activities.
elsa.UseScheduling();
// Register custom activities from the application, if any.
elsa.AddActivitiesFrom<Program>();
// Register custom workflows from the application, if any.
elsa.AddWorkflowsFrom<Program>();
});
// Configure CORS to allow designer app hosted on a different origin to invoke the APIs.
builder.Services.AddCors(cors => cors
.AddDefaultPolicy(policy => policy
.AllowAnyOrigin() // For demo purposes only. Use a specific origin instead.
.AllowAnyHeader()
.AllowAnyMethod()
.WithExposedHeaders("x-elsa-workflow-instance-id"))); // Required for Elsa Studio in order to support running workflows from the designer. Alternatively, you can use the `*` wildcard to expose all headers.
// Add Health Checks.
builder.Services.AddHealthChecks();
// Configure ASP.NET's middleware pipeline.
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseWorkflowsApi(); // Use Elsa API endpoints.
app.UseWorkflows(); // Use Elsa middleware to handle HTTP requests mapped to HTTP Endpoint activities.
app.UseWorkflowsSignalRHubs(); // Optional SignalR integration. Elsa Studio uses SignalR to receive real-time updates from the server.
app.Run();
```
### Create Elsa Studio
Create a new Blazor WebAssembly application using the following command:
```shell
dotnet new blazorwasm-empty -n "ElsaStudio" -f net8.0
To see your application in action, execute the following command:
```shell
dotnet run
```
Your application should now be accessible at https://localhost:5001. The port number might vary based on your configuration. By default, you can log in using: