Refactor to use scoped services in command handler

Updated `InvokeRecActionsCommandHandler` to use `IServiceScopeFactory` for creating scoped services. Replaced direct `IHttpClientFactory` usage with dynamic resolution of `ISender` within a service scope. Improved dependency injection adherence and ensured proper scoping of services. Added `Microsoft.Extensions.DependencyInjection` to `using` directives.
This commit is contained in:
tekh 2025-12-04 11:48:22 +01:00
parent f4390d992a
commit 5cefe1457f

View File

@ -1,4 +1,5 @@
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ReC.Application.RecActions.Queries;
@ -12,7 +13,7 @@ public static class InvokeBatchRecActionsCommandExtensions
=> sender.Send(new InvokeBatchRecActionsCommand { ProfileId = profileId }, cancel);
}
public class InvokeRecActionsCommandHandler(ISender sender, IHttpClientFactory clientFactory, ILogger<InvokeRecActionsCommandHandler>? logger = null) : IRequestHandler<InvokeBatchRecActionsCommand>
public class InvokeRecActionsCommandHandler(ISender sender, IServiceScopeFactory scopeFactory, IHttpClientFactory clientFactory, ILogger<InvokeRecActionsCommandHandler>? logger = null) : IRequestHandler<InvokeBatchRecActionsCommand>
{
public async Task Handle(InvokeBatchRecActionsCommand request, CancellationToken cancel)
{
@ -27,6 +28,8 @@ public class InvokeRecActionsCommandHandler(ISender sender, IHttpClientFactory c
await semaphore.WaitAsync(cancel);
try
{
using var scope = scopeFactory.CreateScope();
var sender = scope.ServiceProvider.GetRequiredService<ISender>();
await sender.Send(action.ToInvokeCommand(), cancel);
}
catch(Exception ex)