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.
52 lines
1.8 KiB
C#
52 lines
1.8 KiB
C#
using MediatR;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using ReC.Application.RecActions.Queries;
|
|
|
|
namespace ReC.Application.RecActions.Commands;
|
|
|
|
public record InvokeBatchRecActionsCommand : ReadRecActionQueryBase, IRequest;
|
|
|
|
public static class InvokeBatchRecActionsCommandExtensions
|
|
{
|
|
public static Task InvokeBatchRecAction(this ISender sender, long profileId, CancellationToken cancel = default)
|
|
=> sender.Send(new InvokeBatchRecActionsCommand { ProfileId = profileId }, cancel);
|
|
}
|
|
|
|
public class InvokeRecActionsCommandHandler(ISender sender, IServiceScopeFactory scopeFactory, IHttpClientFactory clientFactory, ILogger<InvokeRecActionsCommandHandler>? logger = null) : IRequestHandler<InvokeBatchRecActionsCommand>
|
|
{
|
|
public async Task Handle(InvokeBatchRecActionsCommand request, CancellationToken cancel)
|
|
{
|
|
var actions = await sender.Send(request.ToReadQuery(), cancel);
|
|
|
|
var http = clientFactory.CreateClient();
|
|
|
|
using var semaphore = new SemaphoreSlim(5);
|
|
|
|
var tasks = actions.Select(async action =>
|
|
{
|
|
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)
|
|
{
|
|
logger?.LogError(
|
|
ex,
|
|
"Error invoking Rec action. ProfileId: {ProfileId}, Id: {Id}",
|
|
action.ProfileId,
|
|
action.Id
|
|
);
|
|
}
|
|
finally
|
|
{
|
|
semaphore.Release();
|
|
}
|
|
});
|
|
|
|
await Task.WhenAll(tasks);
|
|
}
|
|
} |