Coding4Fun to present Elsa workflow
If you want to known more about Elsa -> here
- .Net 5
- Visual Studio Code
dotnet new blazorserver -o Elsa.Blazor.Client --no-https
cd .\src\Elsa.Blazor.Client\
dotnet add package Elsa
dotnet add package Elsa.Dashboard
dotnet build
Go to StartupFile and configure dependency injection for ELSA
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
...
services.AddElsa();
}
Create db directory
mkdir C:\data
Add connection string in your app settings
"ConnectionStrings": {
"SqlLite": "Data Source=c:\\data\\elsa.db;Cache=Shared;"
}
Configure persistence dependencies
services
.AddElsa(elsa => elsa
.AddEntityFrameworkStores<SqliteContext>(options => options
.UseSqlite(Configuration.GetConnectionString("SqlLite"))))
Add ElsaContext as parameter in the Configure method
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ElsaContext elsaDbcontext)
And add db creation statement like this to allow EF Core to create tables
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
elsaDbcontext.Database.EnsureCreated();
}
Execute dotnet add package Elsa.Dashboard
Add dependecies configuration
services.AddElsaDashboard();
Configure endpoints for Elsa dashboard
Add endpoints.MapControllers();
goto http://localhost:{port-user-by-kestrel}/elsa/home
You can create worklflow but don't do that now. Because you not add any activities.
To use built in activites from designer add this packages:
dotnet add package Elsa.Activities.Console
dotnet add package Elsa.Activities.Timers
dotnet add package Elsa.Activities.Http
dotnet add package Elsa.Activities.Email
dotnet add package Elsa.Activities.ControlFlow
dotnet add package Elsa.Activities.Workflows
dotnet add package Elsa.Activities.UserTask
Declare Activites for dependency injection. Go to the Startup.cs in the ConfigureServices method and add
public void ConfigureServices(IServiceCollection services)
{
.....
//Configure dependencies injection for activities
services
.AddTimerActivities()
.AddConsoleActivities()
.AddWorkflowActivities()
.AddEmailActivities()
.AddControlFlowActivities()
.AddUserTaskActivities()
.AddHttpActivities();
....
App middleware for http activities in the configure method
public void Configure(IApplicationBuilder app)
{
...
app.UseHttpActivities();
app.UseTimersActivities();
...
}
Go to the dashboard of elsa /elsa/home Click on workflows and on the button Create workflow The designer will open
In this exercize we will a very basic workflow
Call "it hello world from the web".
Add an http request activity that handle message from the route /console Check the case read content
After that add a Variable. Name it Message and set the content with a javascript expression queryString("text").
Add Console write activity and set text of the context with the content of variable Body.
Connect all your activities that you added.
Publish your workflow. Go to the bottom buggy scroll page ;).
We will try to trigger two activities in parallel when we receive the http request.
Reopen the previous workflow and add http redirect activity. set the url to http://www.staggeringbeauty.com/
Add a fork activity. The fork activity allow you to make branch. You need to specify branch name separate by a ","
You can see we don't need to recompile anything. All the modification is made at runtime. So you can add or modify logic directly here.
Create a new workflow call it intervall, don't forget to made it as singleton. If you don't do that the workflow instance will ebe destroy after the last ativity ran to completion.
Add a Timer intervall
Add a Console write line in the text set the text to It's now ${new Date()}. Let's do this thing!
Link the two activity and publish the workflow. See your debug console. You will see a message with the date update each 5seconds.
In the next episode we will try to invoke by code.
Return to the root of you project in the src folder
cd src
dotnet new console --name Elsa.ConsoleClient
cd Elsa.ConsoleClient
dotnet add package Elsa
dotnet add package Microsoft.Extensions.DependencyInjection
dotnet add package Elsa
dotnet add package Elsa.Activities.Console
dotnet add package Elsa.Activities.ControlFlow
Add in the csproj
<ItemGroup>
<ProjectReference Include="..\Elsa.Shared\Elsa.Shared.csproj" />
</ItemGroup>
Add in the Program cs the Configure method
/// <summary>
/// Dependency injection configuration
/// </summary>
static void Configure()
{
// Setup a service collection.
var definitions = new ServiceCollection();
definitions
.AddElsa()
.AddConsoleActivities()
.AddControlFlowActivities();
services = definitions.BuildServiceProvider();
}
Call it from the Main method
static async Task Main(string[] args)
{
Configure();