< Summary

Information
Class: Elsa.Workflows.Middleware.Activities.NotificationPublishingMiddleware
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Middleware/Activities/NotificationPublishingMiddleware.cs
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 59
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
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()100%44100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Middleware/Activities/NotificationPublishingMiddleware.cs

#LineLine coverage
 1using Elsa.Mediator.Contracts;
 2using Elsa.Workflows.Notifications;
 3using Elsa.Workflows.Pipelines.ActivityExecution;
 4
 5namespace Elsa.Workflows.Middleware.Activities;
 6
 7/// <summary>
 8/// An activity execution middleware component that publishes activity notifications:
 9/// <list type="bullet">
 10/// <item><see cref="ActivityExecuting"/> when an activity starts executing</item>
 11/// <item><see cref="ActivityExecuted"/> when an activity has executed</item>
 12/// <item><see cref="Notifications.ActivityCompleted"/> when an activity transitions from Running to Completed status</i
 13/// </list>
 14/// </summary>
 15public class NotificationPublishingMiddleware : IActivityExecutionMiddleware
 16{
 17    private readonly ActivityMiddlewareDelegate _next;
 18    private readonly INotificationSender _notificationSender;
 19
 20    /// <summary>
 21    /// Constructor.
 22    /// </summary>
 42123    public NotificationPublishingMiddleware(ActivityMiddlewareDelegate next, INotificationSender notificationSender)
 24    {
 42125        _next = next;
 42126        _notificationSender = notificationSender;
 42127    }
 28
 29    /// <inheritdoc />
 30    public async ValueTask InvokeAsync(ActivityExecutionContext context)
 31    {
 32        // Store the status before execution
 301933        var statusBeforeExecution = context.Status;
 34
 35        // Send notification that activity is executing
 301936        await _notificationSender.SendAsync(new ActivityExecuting(context));
 37
 38        // Execute the activity
 301939        await _next(context);
 40
 41        // Send notification that activity has executed
 300642        await _notificationSender.SendAsync(new ActivityExecuted(context));
 43
 44        // Send notification that activity has completed if status transitioned from Running to Completed
 300645        if (statusBeforeExecution == ActivityStatus.Running && context.Status == ActivityStatus.Completed)
 2746            await _notificationSender.SendAsync(new Notifications.ActivityCompleted(context));
 300647    }
 48}
 49
 50/// <summary>
 51/// Extends <see cref="IActivityExecutionPipelineBuilder"/> to install the <see cref="NotificationPublishingMiddleware"/
 52/// </summary>
 53public static class NotificationPublishingMiddlewareExtensions
 54{
 55    /// <summary>
 56    /// Installs the <see cref="NotificationPublishingMiddleware"/> component.
 57    /// </summary>
 58    public static IActivityExecutionPipelineBuilder UseNotifications(this IActivityExecutionPipelineBuilder pipelineBuil
 59}