Search code examples
asp.net-coreelsa-workflows

How to setup Elsa workflow using .Net 6.0 or above?


I'm new in Elsa Workflow. I try to set up a simple Hello World REST API following the steps in the URL below:

https://elsa-workflows.github.io/elsa-core/docs/next/quickstarts/quickstarts-aspnetcore-hello-world

However, when I set up the project using dotnet, no Startup.cs is found. I check my dotnet version using dotnet --version, it shows 7.0.100. I search the web, it said that Startup.cs is removed and no longer required in Web App, starting from .Net 6.0 onwards.

My question is:

  1. Do I need to install the old .Net Core 3.1 to make Elsa work?
  2. If not, how do I set up a simple Elsa workflow using .Net 7.0?

I've tried to move the code in Startup.cs into Program.cs as below:

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddElsa(options => options
    .AddHttpActivities()    
    .AddWorkflow<ElsaQuickstarts.WebApp.HelloWorld.HelloWorld>());

var app = builder.Build();

app.Run();

But when I build the program and run, it gives the following error:

Unhandled exception. System.InvalidOperationException: Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddAuthorization' in the application startup code.
   at Microsoft.AspNetCore.Builder.AuthorizationAppBuilderExtensions.VerifyServicesRegistered(IApplicationBuilder app)
   at Microsoft.AspNetCore.Builder.AuthorizationAppBuilderExtensions.UseAuthorization(IApplicationBuilder app)
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.ConfigureApplication(WebHostBuilderContext context, IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   at Program.<Main>$(String[] args) in C:\Git\ElsaQuickstarts.WebApp.HelloWorld\Program.cs:line 10

Solution

  • Add AddAuthorization to the services and call UseHttpActivities to add Elsa's middleware to handle HTTP requests to workflows:

    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services
        // ...
        .AddAuthorization();
    
    var app = builder.Build();
    
    app.UseHttpActivities();
    
    app.Run();