githubEdit

Hosting Elsa in an Existing App

Step-by-step guide to integrating Elsa Workflows into an existing ASP.NET Core application, including persistence setup, common pain points, and troubleshooting.

This guide walks you through adding Elsa Workflows to an existing ASP.NET Core application. Whether you're building a new feature or modernizing an existing codebase, this guide will help you integrate Elsa smoothly while avoiding common pitfalls.

Overview

Integrating Elsa into an existing ASP.NET Core app involves:

  1. Installing required NuGet packages

  2. Configuring Elsa services in Program.cs

  3. Setting up persistence (database)

  4. Addressing common integration challenges

This guide focuses on practical integration patterns and addresses common issues reported by the community (issue #6).

Prerequisites

Before you begin, ensure you have:

  • An existing ASP.NET Core application (.NET 6.0+, .NET 8.0 recommended)

  • Basic understanding of ASP.NET Core dependency injection

  • A database server (PostgreSQL, SQL Server, SQLite, or MySQL)

  • Visual Studio 2022+, Visual Studio Code, or Rider

Step 1: Install Elsa Packages

Add the core Elsa packages to your project. The exact packages depend on your needs:

Basic Workflow Runtime

For workflow execution without a UI:

With Entity Framework Core Persistence

For PostgreSQL:

For SQL Server:

For SQLite (development only):

Optional: HTTP Activities

If your workflows need HTTP endpoints:

Optional: Elsa Studio (Web UI)

To include the workflow designer UI in your app:

Step 2: Configure Elsa in Program.cs

Add Elsa to your existing Program.cs configuration. Here's a complete example showing integration with an existing app:

Basic Configuration

With Persistence (PostgreSQL Example)

Connection String (appsettings.json):

With SQL Server

SQL Server Connection String:

Step 3: Initialize Database

After configuring persistence, you need to create the database schema.

Option A: Auto-Migration (Development)

For development environments, you can auto-migrate on startup:

This creates/updates the database schema automatically when the app starts.

Option B: Manual Migration (Production)

For production, run migrations separately using the EF Core CLI:

circle-info

Note: Elsa uses separate DbContexts for management (workflow definitions) and runtime (workflow instances). You'll need to manage migrations for both contexts.

Common Pain Points and Solutions

Based on community feedback (issue #6), here are the most common integration challenges and how to solve them:

1. DbContextOptions Registration Issue

Problem: When you have your own AppDbContext that requires DbContextOptions<AppDbContext>, you may encounter conflicts with Elsa's internal DbContext registration.

Symptoms:

Solution:

Explicitly register your AppDbContext with its own connection string and options:

Key Points:

  • Use separate databases or schemas for Elsa and your app

  • Elsa's DbContexts are registered with specific lifetimes - don't try to share them

  • If you must share a database, use different connection strings with schema prefixes

2. Version Pinning Conflicts

Problem: Elsa depends on specific versions of packages like Hangfire, Microsoft.EntityFrameworkCore.Design, or Microsoft.CodeAnalysis.*, which may conflict with your existing dependencies.

Symptoms:

Solutions:

Strategy 1: Version Alignment

Align your EF Core versions with Elsa's requirements:

Strategy 2: Binding Redirects (Framework Apps)

For .NET Framework apps, use binding redirects in web.config:

Strategy 3: Update Dependencies

Update your existing packages to match Elsa's requirements:

3. Swagger / Swashbuckle Schema Conflicts

Problem: When adding Swagger/Swashbuckle to document your API, you may encounter schema ID conflicts with Elsa's API endpoints.

Symptoms:

Solutions:

Solution 1: Custom Schema ID Generation

Configure Swashbuckle to generate unique schema IDs:

Solution 2: Exclude Elsa Endpoints from Swagger

If you don't need to document Elsa's API endpoints:

Solution 3: Multiple Swagger Documents

Create separate Swagger documents for your API and Elsa:

Authentication and Authorization

For production deployments, you'll need to secure Elsa's API endpoints. This section provides a high-level overview; see the Security & Authentication Guide for detailed configuration.

Quick Overview

Development (Disable Auth):

Production Options:

  • Elsa.Identity: Built-in identity system with user management

  • API Keys: Simple token-based authentication

  • OIDC/OAuth2: Integration with Azure AD, Auth0, Keycloak, etc.

Basic Identity Setup

Testing Your Integration

1. Verify Elsa Services are Registered

Create a simple controller to check if Elsa is loaded:

2. Check API Endpoints

With the app running, navigate to:

  • https://localhost:5001/elsa/api/workflow-definitions - List workflow definitions

  • https://localhost:5001/swagger - Swagger UI (if configured)

3. Verify Database

Check that Elsa tables were created:

PostgreSQL:

SQL Server:

You should see tables like:

  • Elsa_WorkflowDefinitions

  • Elsa_WorkflowInstances

  • Elsa_Bookmarks

  • And others

Next Steps

Now that Elsa is integrated into your app:

  1. Create your first workflow: Use Elsa Studio or programmatic workflow definitions

  2. Add workflow activities: Extend Elsa with Custom Activities

  3. Secure your deployment: Configure authentication and authorization

  4. Deploy to production: Follow the Kubernetes Deployment Guide or Clustering Guide

  5. Monitor workflows: Set up observability and logging

Troubleshooting

Services Not Resolving

Problem: IWorkflowRuntime or other Elsa services not found in DI.

Solution: Ensure you called AddElsa() before builder.Build():

Database Connection Fails

Problem: Npgsql.NpgsqlException: Connection refused

Solutions:

  • Verify database server is running

  • Check connection string format

  • Ensure firewall allows connections

  • Test connection with psql or sqlcmd

Migrations Not Applied

Problem: Tables not created after running migrations.

Solution: Ensure migrations were created for both contexts:

Then apply both:


Last Updated: 2025-12-02 Addresses Issues: #6

Last updated