Refactor BackgroundTaskDispatcher and update lifecycle
Refactored the BackgroundTaskDispatcher class to accept the INotificationSender service directly rather than the IServiceScopeFactory. This change simplifies the dispatch process. Also, adjusted the service lifecycle of RunTaskDispatcher, changing it from singleton to scoped.
This commit is contained in:
parent
673f5dfdbc
commit
b7074e45d2
|
|
@ -209,7 +209,7 @@ public class WorkflowRuntimeFeature : FeatureBase
|
|||
.AddScoped(ActivityExecutionLogStore)
|
||||
.AddScoped(WorkflowInboxStore)
|
||||
.AddScoped(WorkflowExecutionContextStore)
|
||||
.AddSingleton(RunTaskDispatcher)
|
||||
.AddScoped(RunTaskDispatcher)
|
||||
.AddSingleton(BackgroundActivityScheduler)
|
||||
.AddSingleton<RandomLongIdentityGenerator>()
|
||||
.AddScoped<IBookmarkManager, DefaultBookmarkManager>()
|
||||
|
|
|
|||
|
|
@ -2,20 +2,17 @@ using Elsa.Mediator;
|
|||
using Elsa.Mediator.Contracts;
|
||||
using Elsa.Workflows.Runtime.Contracts;
|
||||
using Elsa.Workflows.Runtime.Notifications;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Elsa.Workflows.Runtime.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Relies on the <see cref="INotificationSender"/> to publish the received request as a domain event from a background worker.
|
||||
/// </summary>
|
||||
public class BackgroundTaskDispatcher(IServiceScopeFactory scopeFactory) : ITaskDispatcher
|
||||
public class BackgroundTaskDispatcher(INotificationSender notificationSender) : ITaskDispatcher
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task DispatchAsync(RunTaskRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var scope = scopeFactory.CreateScope();
|
||||
var notificationSender = scope.ServiceProvider.GetRequiredService<INotificationSender>();
|
||||
await notificationSender.SendAsync(request, NotificationStrategy.Background, cancellationToken);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue