In this topic, we will create an ASP.NET Core application that acts as both an Elsa Server and an Elsa Studio.
Instead of running Elsa Server and Elsa Studio as separate ASP.NET Core applications, you can also setup an ASP.NET Core application that hosts both the workflow server and the UI. The UI will still make HTTP calls to the backend as if they were hosted separately, but the difference is that they are now served from the same application and therefore deployable as a single unit.
For Elsa Studio, we will setup the Blazor parts using Blazor WebAssembly, which static files will be served from the ASP.NET Core host application.
Create Solution
In this chapter, we will scaffold a new solution and two projects:
The Host
The Client
The host will host both Elsa Server and the Blazor WebAssembly application representing Elsa Studio.
Run the following commands to create a solution with two projects:
# Create a new solutiondotnetnewsln-nElsaServerAndStudio# Create the host projectdotnetnewweb-n"ElsaServer"# Add the host project to the solutiondotnetslnaddElsaServer/ElsaServer.csproj# Create the client projectdotnetnewblazorwasm-n"ElsaStudio"# Add the client project to the solutiondotnetslnaddElsaStudio/ElsaStudio.csproj# Navigate to the directory where the host project is locatedcdElsaServer# Add a reference to the client projectdotnetaddreference../ElsaStudio/ElsaStudio.csproj
Setup Host
In this chapter, we will setup the host, which will host both the Elsa Server engine as well as the webassembly files for serving the Elsa Studio client assets to the browser.
Add Packages
Add the following packages:
Update Program.cs
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
Update appsettings.json
Add the following configuration section to appsettings.json or appsettings.Development.json with the following content:
Create _Host.cshtml
To conclude the setup, create new folder called Pages and add a new file called _Host.cshtml and copy in the code showcased below:
Pages/_Host.cshtml
Setup Client
Next, we will modify the client project.
Add Elsa Studio Packages
Navigate to the root directory of the client project and add the following Elsa Studio packages:
Modify Program.cs
Open Program.cs and replace its existing content with the code provided below:
Program.cs
Modify MainLayout.razor
Update Layout/MainLayout.razor with the following code listing:
MainLayout.razor
Launch the Application
To see your application in action, navigate back to the root directory containing the host project:
using System.Text.Json;
using Elsa.Studio.Contracts;
using Elsa.Studio.Core.BlazorWasm.Extensions;
using Elsa.Studio.Dashboard.Extensions;
using Elsa.Studio.Extensions;
using Elsa.Studio.Login.BlazorWasm.Extensions;
using Elsa.Studio.Login.Extensions;
using Elsa.Studio.Login.HttpMessageHandlers;
using Elsa.Studio.Options;
using Elsa.Studio.Shell;
using Elsa.Studio.Shell.Extensions;
using Elsa.Studio.Workflows.Designer.Extensions;
using Elsa.Studio.Workflows.Extensions;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.JSInterop;
// Build the host.
var builder = WebAssemblyHostBuilder.CreateDefault(args);
// Register root components.
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.RootComponents.RegisterCustomElsaStudioElements();
// Register shell services and modules.
builder.Services.AddCore();
builder.Services.AddShell();
builder.Services.AddRemoteBackend(new()
{
ConfigureHttpClientBuilder = options => options.AuthenticationHandler = typeof(AuthenticatingApiHttpMessageHandler)
});
builder.Services
.AddLoginModule()
.UseElsaIdentity();
builder.Services.AddDashboardModule();
builder.Services.AddWorkflowsModule();
builder.Services.UseElsaIdentity();
// Build the application.
var app = builder.Build();
// Apply client config.
var js = app.Services.GetRequiredService<IJSRuntime>();
var clientConfig = await js.InvokeAsync<JsonElement>("getClientConfig");
var apiUrl = clientConfig.GetProperty("apiUrl").GetString() ?? throw new InvalidOperationException("No API URL configured.");
app.Services.GetRequiredService<IOptions<BackendOptions>>().Value.Url = new(apiUrl);
// Run each startup task.
var startupTaskRunner = app.Services.GetRequiredService<IStartupTaskRunner>();
await startupTaskRunner.RunStartupTasksAsync();
// Run the application.
await app.RunAsync();