I am working on an Elsa .NET 8 proof of concept (POC). I’m attempting to connect Elsacontext in my controller, but I’m encountering the following issue.
Unable to resolve service for type ‘Elsa.Persistence.EntityFramework.Core.ElsaContext’ while attempting to activate ‘ElsaDemo2.Controllers.BinApprovalController
My program.cs code:-
using Elsa.Persistence.EntityFramework.Core.Extensions;
using Elsa.Persistence.EntityFramework.Sqlite;
using ElsaDemo2.WorkFlows;
using Elsa.Persistence.EntityFrameworkCore.DbContexts;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ElsaContext>(options =>
{
options.UseSqlite("Data Source=elsa.db;");
});
builder.Services.AddElsaCore(options =>
options
.UseEntityFrameworkPersistence(ef => ef.UseSqlite("Data Source=elsa.db;"), true)
.AddHttpActivities()
.AddWorkflow<HelloWorld>())
.AddElsaApiEndpoints();
builder.Services.AddControllersWithViews();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
//ELSA
app.UseHttpActivities();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
My controller code:-
using Elsa.Persistence.EntityFramework.Core;
using Elsa.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace ElsaDemo2.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class BinApprovalController : ControllerBase
{
private readonly IWorkflowRegistry _workflowRegistry;
private readonly IWorkflowLaunchpad _workflowLaunchpad;
private readonly ElsaContext _elsaContext;
public BinApprovalController(IWorkflowRegistry workflowRegistry, IWorkflowLaunchpad workflowLaunchpad, ElsaContext elsaContext)
{
_workflowRegistry = workflowRegistry;
_workflowLaunchpad = workflowLaunchpad;
_elsaContext = elsaContext;
}
[HttpPost("v1/bins")]
public async Task<IActionResult> Post([FromBody] BinRequestModel request, CancellationToken cancellationToken)
{
var allVersions = await _elsaContext.WorkflowDefinitions.ToListAsync(cancellationToken);
var latestVersion = allVersions.OrderByDescending(x => x.Version).FirstOrDefault();
return Ok("<h1>Request for Approval Sent</h1><p>Your bin has been received and will be reviewed shortly.</p>");
}
public class BinRequestModel
{
public string BinId { get; set; }
// Add additional properties as needed
}
}
}
What I’m trying to do is test the Elsa database data using a POST request through a Postman collection.
Here is the URL and body I used for the Postman request:
https://localhost:xxxxx/api/BinApproval/v1/bins/
{
“BinId”: “bin123”
}
Can I get your support on this? Since I DI the context I am not sure what still wrong.