| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using JetBrains.Annotations; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using Elsa.Workflows.Attributes; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Runtime.Activities; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Faults the workflow. |
| | | 11 | | /// </summary> |
| | | 12 | | [Activity("Elsa", "Primitives", "Publishes an event.")] |
| | | 13 | | [UsedImplicitly] |
| | 227 | 14 | | public class PublishEvent([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : Activity(source |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// The name of the event to publish. |
| | | 18 | | /// </summary> |
| | | 19 | | [Input(Description = "The name of the event to publish.")] |
| | 839 | 20 | | public Input<string> EventName { get; set; } = null!; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The correlation ID to scope the event to. |
| | | 24 | | /// </summary> |
| | | 25 | | [Input(Description = "The correlation ID to scope the event to.")] |
| | 668 | 26 | | public Input<string?> CorrelationId { get; set; } = null!; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Whether the event is local to the workflow. |
| | | 30 | | /// </summary> |
| | | 31 | | [Input(DisplayName = "Local event", Description = "Whether the event is local to the workflow. When checked, the eve |
| | 836 | 32 | | public Input<bool> IsLocalEvent { get; set; } = null!; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The input to send as the event body. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <remarks> |
| | | 38 | | /// The payload is persisted as part of the receiving workflow's state, and only types with a registered |
| | | 39 | | /// serialization alias survive that round trip as themselves. A payload whose type has no alias — an anonymous |
| | | 40 | | /// type, or any POCO that was never registered — is stored without a type discriminator and read back as a |
| | | 41 | | /// property bag whose keys carry the state serializer's camel-case naming policy, so a consumer looking for |
| | | 42 | | /// <c>Status</c> will find <c>status</c>. Register the type during startup with |
| | | 43 | | /// <c>AddTypeAlias<TPayload>()</c> to keep it intact, or pass a <c>Dictionary<string, object></c>, |
| | | 44 | | /// whose keys are stored verbatim, when a property bag is what you intend. |
| | | 45 | | /// </remarks> |
| | | 46 | | [Input(Description = "The payload to send as the event body.")] |
| | 724 | 47 | | public Input<object> Payload { get; set; } = null!; |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 51 | | { |
| | 6 | 52 | | var eventName = EventName.Get(context); |
| | 6 | 53 | | var correlationId = CorrelationId.GetOrDefault(context).NullIfWhiteSpace(); |
| | 6 | 54 | | var isLocalEvent = IsLocalEvent.GetOrDefault(context); |
| | 6 | 55 | | var workflowInstanceId = isLocalEvent ? context.WorkflowExecutionContext.Id : null; |
| | 6 | 56 | | var payload = Payload.GetOrDefault(context); |
| | 6 | 57 | | var publisher = context.GetRequiredService<IEventPublisher>(); |
| | | 58 | | |
| | 6 | 59 | | await publisher.PublishAsync(eventName, correlationId, workflowInstanceId, null, payload, true, context.Cancella |
| | 6 | 60 | | await context.CompleteActivityAsync(); |
| | 6 | 61 | | } |
| | | 62 | | } |