From b7074e45d245ebe3b6d25e10c8aee7c19febdc4c Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Thu, 28 Mar 2024 16:05:28 +0100 Subject: [PATCH] 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. --- .../Features/WorkflowRuntimeFeature.cs | 2 +- .../Services/BackgroundTaskDispatcher.cs | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/modules/Elsa.Workflows.Runtime/Features/WorkflowRuntimeFeature.cs b/src/modules/Elsa.Workflows.Runtime/Features/WorkflowRuntimeFeature.cs index cf64528e2..bc08b314c 100644 --- a/src/modules/Elsa.Workflows.Runtime/Features/WorkflowRuntimeFeature.cs +++ b/src/modules/Elsa.Workflows.Runtime/Features/WorkflowRuntimeFeature.cs @@ -209,7 +209,7 @@ public class WorkflowRuntimeFeature : FeatureBase .AddScoped(ActivityExecutionLogStore) .AddScoped(WorkflowInboxStore) .AddScoped(WorkflowExecutionContextStore) - .AddSingleton(RunTaskDispatcher) + .AddScoped(RunTaskDispatcher) .AddSingleton(BackgroundActivityScheduler) .AddSingleton() .AddScoped() diff --git a/src/modules/Elsa.Workflows.Runtime/Services/BackgroundTaskDispatcher.cs b/src/modules/Elsa.Workflows.Runtime/Services/BackgroundTaskDispatcher.cs index a73edb92c..f2105bb3e 100644 --- a/src/modules/Elsa.Workflows.Runtime/Services/BackgroundTaskDispatcher.cs +++ b/src/modules/Elsa.Workflows.Runtime/Services/BackgroundTaskDispatcher.cs @@ -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; /// /// Relies on the to publish the received request as a domain event from a background worker. /// -public class BackgroundTaskDispatcher(IServiceScopeFactory scopeFactory) : ITaskDispatcher +public class BackgroundTaskDispatcher(INotificationSender notificationSender) : ITaskDispatcher { /// public async Task DispatchAsync(RunTaskRequest request, CancellationToken cancellationToken = default) { - using var scope = scopeFactory.CreateScope(); - var notificationSender = scope.ServiceProvider.GetRequiredService(); await notificationSender.SendAsync(request, NotificationStrategy.Background, cancellationToken); } } \ No newline at end of file