2022-02-21 15:13:55 +00:00
|
|
|
using Azure.Messaging.ServiceBus;
|
2023-03-03 21:47:23 +00:00
|
|
|
using Elsa.Common.Contracts;
|
2022-11-10 13:40:37 +00:00
|
|
|
using Elsa.Common.Services;
|
2023-01-12 10:59:31 +00:00
|
|
|
using Elsa.Extensions;
|
2023-07-09 11:12:13 +00:00
|
|
|
using Elsa.Workflows.Core;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Workflows.Core.Attributes;
|
|
|
|
|
using Elsa.Workflows.Core.Models;
|
2023-01-12 10:59:31 +00:00
|
|
|
using JetBrains.Annotations;
|
2022-02-21 15:13:55 +00:00
|
|
|
|
2022-05-26 10:47:31 +00:00
|
|
|
namespace Elsa.AzureServiceBus.Activities;
|
2022-02-21 15:13:55 +00:00
|
|
|
|
2023-01-12 10:59:31 +00:00
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2022-04-27 17:52:58 +00:00
|
|
|
[Activity("Elsa.AzureServiceBus.Send", "Azure Service Bus", "Send a message to a queue or topic")]
|
2023-01-12 10:59:31 +00:00
|
|
|
[PublicAPI]
|
2023-01-19 20:49:24 +00:00
|
|
|
public class SendMessage : CodeActivity
|
2022-02-21 15:13:55 +00:00
|
|
|
{
|
2023-01-12 10:59:31 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// The contents of the message to send.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Input(Description = "The contents of the message to send.")]
|
2022-02-21 15:13:55 +00:00
|
|
|
public Input<object> MessageBody { get; set; } = default!;
|
2023-01-12 10:59:31 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The queue or topic to send the message to.
|
|
|
|
|
/// </summary>
|
2022-02-21 15:13:55 +00:00
|
|
|
public Input<string> QueueOrTopic { get; set; } = default!;
|
2023-01-12 10:59:31 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The content type of the message.
|
|
|
|
|
/// </summary>
|
2022-02-21 15:13:55 +00:00
|
|
|
public Input<string>? ContentType { get; set; }
|
2023-01-12 10:59:31 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The subject of the message.
|
|
|
|
|
/// </summary>
|
2022-02-21 15:13:55 +00:00
|
|
|
public Input<string>? Subject { get; set; }
|
2023-01-12 10:59:31 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The correlation ID of the message.
|
|
|
|
|
/// </summary>
|
2022-02-21 15:13:55 +00:00
|
|
|
public Input<string>? CorrelationId { get; set; }
|
|
|
|
|
|
2023-01-12 10:59:31 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// The formatter to use when serializing the message body.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Input<Type?> FormatterType { get; set; } = default!;
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2022-02-21 15:13:55 +00:00
|
|
|
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
|
|
|
|
|
{
|
|
|
|
|
var queueOrTopic = context.Get(QueueOrTopic);
|
|
|
|
|
var messageBody = context.Get(MessageBody);
|
|
|
|
|
var cancellationToken = context.CancellationToken;
|
2023-01-12 10:59:31 +00:00
|
|
|
var serializedMessageBody = await SerializeMessageBodyAsync(context, messageBody!, cancellationToken);
|
2022-02-21 15:13:55 +00:00
|
|
|
|
|
|
|
|
var message = new ServiceBusMessage(serializedMessageBody)
|
|
|
|
|
{
|
|
|
|
|
ContentType = context.Get(ContentType),
|
|
|
|
|
Subject = context.Get(Subject),
|
|
|
|
|
CorrelationId = context.Get(CorrelationId)
|
2022-04-27 17:52:58 +00:00
|
|
|
|
2023-01-12 10:59:31 +00:00
|
|
|
// TODO: Maybe expose additional members?
|
2022-02-21 15:13:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var client = context.GetRequiredService<ServiceBusClient>();
|
|
|
|
|
|
|
|
|
|
await using var sender = client.CreateSender(queueOrTopic);
|
|
|
|
|
await sender.SendMessageAsync(message, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 10:59:31 +00:00
|
|
|
private async ValueTask<BinaryData> SerializeMessageBodyAsync(ActivityExecutionContext context, object value, CancellationToken cancellationToken)
|
2022-02-21 15:13:55 +00:00
|
|
|
{
|
|
|
|
|
if (value is string s) return BinaryData.FromString(s);
|
|
|
|
|
|
2023-03-24 19:57:27 +00:00
|
|
|
var formatterType = FormatterType.GetOrDefault(context) ?? typeof(JsonFormatter);
|
2023-01-12 10:59:31 +00:00
|
|
|
var formatter = context.GetServices<IFormatter>().First(x => x.GetType() == formatterType);
|
2022-02-21 15:13:55 +00:00
|
|
|
var data = await formatter.ToStringAsync(value, cancellationToken);
|
|
|
|
|
|
|
|
|
|
return BinaryData.FromString(data);
|
|
|
|
|
}
|
|
|
|
|
}
|