< Summary

Information
Class: Elsa.Mediator.Middleware.Command.Components.CommandHandlerInvokerMiddleware
Assembly: Elsa.Mediator
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Middleware/Command/Components/CommandHandlerInvokerMiddleware.cs
Line coverage
91%
Covered lines: 21
Uncovered lines: 2
Coverable lines: 23
Total lines: 52
Line coverage: 91.3%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
InvokeAsync()50%4490.47%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Middleware/Command/Components/CommandHandlerInvokerMiddleware.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using Elsa.Mediator.Contexts;
 3using Elsa.Mediator.Contracts;
 4using Elsa.Mediator.Middleware.Command.Contracts;
 5using JetBrains.Annotations;
 6using Microsoft.Extensions.DependencyInjection;
 7
 8namespace Elsa.Mediator.Middleware.Command.Components;
 9
 10/// <summary>
 11/// A command middleware that invokes the command.
 12/// </summary>
 13[UsedImplicitly]
 114public class CommandHandlerInvokerMiddleware(CommandMiddlewareDelegate next) : ICommandMiddleware
 15{
 16    /// <inheritdoc />
 17    [UnconditionalSuppressMessage("Trimming", "IL2060:Call to MakeGenericMethod can not be statically analyzed", Justifi
 18    public async ValueTask InvokeAsync(CommandContext context)
 19    {
 20        // Find all handlers for the specified command.
 5521        var command = context.Command;
 5522        var commandType = command.GetType();
 5523        var resultType = context.ResultType;
 5524        var handlerType = typeof(ICommandHandler<,>).MakeGenericType(commandType, resultType);
 5525        var serviceProvider = context.ServiceProvider;
 5526        var commandHandlers = serviceProvider.GetServices<ICommandHandler>();
 49527        var handlers = commandHandlers.DistinctBy(x => x.GetType()).Where(x => handlerType.IsInstanceOfType(x)).ToArray(
 28
 5529        if (handlers.Length == 0)
 030            throw new InvalidOperationException($"There is no handler to handle the {commandType.FullName} command");
 31
 5532        if (handlers.Length > 1)
 033            throw new InvalidOperationException($"Multiple handlers were found to handle the {commandType.FullName} comm
 34
 5535        var handler = handlers.First();
 5536        var strategyContext = new CommandStrategyContext(context, handler, serviceProvider, context.CancellationToken);
 5537        var strategy = context.CommandStrategy;
 5538        var executeMethod = strategy.GetType().GetMethod(nameof(ICommandStrategy.ExecuteAsync))!;
 5539        var executeMethodWithReturnType = executeMethod.MakeGenericMethod(resultType);
 40
 41        // Execute command.
 5542        var task = executeMethodWithReturnType.Invoke(strategy, [strategyContext]);
 43
 44        // Get the result of the task.
 5545        var taskWithReturnType = typeof(Task<>).MakeGenericType(resultType);
 5546        var resultProperty = taskWithReturnType.GetProperty(nameof(Task<object>.Result))!;
 5547        context.Result = resultProperty.GetValue(task);
 48
 49        // Invoke next middleware.
 5550        await next(context);
 5551    }
 52}