In this topic, we'll create an ASP.NET Core application that acts as a workflow server.
An Elsa Server is an ASP.NET Core web application that lets you manage workflows using a REST API ad execute them. You can store your workflows in various places like databases, file systems, or even cloud storage.
Setup
The following is a step-by-step guide to setting up a new ASP.NET Core Web Application that serves as an Elsa Server.
Create a new ASP.NET project
Open your command line tool and run these commands:
dotnetnewweb-n"ElsaServer"
CD into the project's directory
Run the following command to go into the project's directory.
We need to add some code to make our server work. Open the Program.cs file in your project and replace its contents with the code provided below. This code does a lot of things like setting up database connections, enabling user authentication, and preparing the server to handle workflows.
Program.cs
usingElsa.EntityFrameworkCore.Extensions;usingElsa.EntityFrameworkCore.Modules.Management;usingElsa.EntityFrameworkCore.Modules.Runtime;usingElsa.Extensions;var builder =WebApplication.CreateBuilder(args);builder.Services.AddElsa(elsa =>{ // Configure Management layer to use EF Core.elsa.UseWorkflowManagement(management =>management.UseEntityFrameworkCore(ef =>ef.UseSqlite())); // Configure Runtime layer to use EF Core.elsa.UseWorkflowRuntime(runtime =>runtime.UseEntityFrameworkCore(ef =>ef.UseSqlite())); // Default Identity features for authentication/authorization.elsa.UseIdentity(identity => {identity.TokenOptions= options =>options.SigningKey="sufficiently-large-secret-signing-key"; // This key needs to be at least 256 bits long.identity.UseAdminUserProvider(); }); // Configure ASP.NET authentication/authorization.elsa.UseDefaultAuthentication(auth =>auth.UseAdminApiKey()); // Expose Elsa API endpoints.elsa.UseWorkflowsApi(); // Enable JavaScript workflow expressions.elsa.UseJavaScript(); // Enable C# workflow expressions.elsa.UseCSharp(); // Enable Liquid workflow expressions.elsa.UseLiquid(); // 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();// Build the web application.var app =builder.Build();// Configure web application'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.Run();
Launch the Application
To see the application in action, execute the following command:
dotnetrun--urls"https://localhost:5001"
Source Code
The source code for this chapter can be found here