elsa-core/src/modules/Elsa.AzureServiceBus/Activities/SendMessage.cs

102 lines
3.6 KiB
C#
Raw Normal View History

using System.Runtime.CompilerServices;
using Azure.Messaging.ServiceBus;
using Elsa.Common.Contracts;
using Elsa.Common.Services;
using Elsa.Extensions;
Add a more generic UIHandler to customize how inputAttributes can be handle by UI (#4688) * add a more generic UIHandler to customize how inputAttributes can be handle by the ui * Add IPropertyUIHandlerResolver and update PropertyUIHandlerResolver Introduced a new interface, IPropertyUIHandlerResolver, to resolve UI options for a property. Refactored PropertyUIHandlerResolver to implement this interface and removed the unnecessary partial class structure. Also, cleaned up some unnecessary usings in various files for better code organization. * Refactor variable name and description in InputDescriptor The 'uISpecifications' variable in the InputDescriptor model is renamed to 'uiSpecifications' for better readability. Additionally, the associated comment was revised to explain that the dictionary is used by the UI. * "Refactor codebase for improved organization and cleaner architecture" The codebase has been significantly refactored, moving several classes to more appropriate namespaces for improved organization and cleaner architecture. This includes shifting UI hint handlers, activities, and memory-related components, amongst others. The changes should improve code readability and maintainability, but as this is a broad refactoring effort, thorough regression testing is advised. * Add CheckList UIHint with associated handler and provider This update introduces a new UIHint called CheckList to the Elsa.Workflows.Core. This includes the necessary handler and provider classes. The handler is registered in the WorkflowsFeature.cs, and the CheckList UIHint key has been added to the InputUIHints.cs. Various associated files have been created in both the Elsa.Api.Client and Elsa.Workflows.Core project to support this new UIHint. --------- Co-authored-by: Jérémie DEVILLARD <jdevillard@users.noreply.github.com> Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com>
2023-12-26 17:56:29 +00:00
using Elsa.Workflows;
using Elsa.Workflows.Attributes;
using Elsa.Workflows.UIHints;
using Elsa.Workflows.Models;
using JetBrains.Annotations;
namespace Elsa.AzureServiceBus.Activities;
/// <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")]
[PublicAPI]
public class SendMessage : CodeActivity
{
/// <inheritdoc />
public SendMessage([CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line)
{
}
/// <summary>
/// The contents of the message to send.
/// </summary>
[Input(Description = "The contents of the message to send.")]
public Input<object> MessageBody { get; set; } = default!;
/// <summary>
/// The queue or topic to send the message to.
/// </summary>
public Input<string> QueueOrTopic { get; set; } = default!;
/// <summary>
/// The content type of the message.
/// </summary>
public Input<string>? ContentType { get; set; }
/// <summary>
/// The subject of the message.
/// </summary>
public Input<string>? Subject { get; set; }
/// <summary>
/// The correlation ID of the message.
/// </summary>
public Input<string>? CorrelationId { get; set; }
/// <summary>
/// The formatter to use when serializing the message body.
/// </summary>
public Input<Type?> FormatterType { get; set; } = default!;
Add a more generic UIHandler to customize how inputAttributes can be handle by UI (#4688) * add a more generic UIHandler to customize how inputAttributes can be handle by the ui * Add IPropertyUIHandlerResolver and update PropertyUIHandlerResolver Introduced a new interface, IPropertyUIHandlerResolver, to resolve UI options for a property. Refactored PropertyUIHandlerResolver to implement this interface and removed the unnecessary partial class structure. Also, cleaned up some unnecessary usings in various files for better code organization. * Refactor variable name and description in InputDescriptor The 'uISpecifications' variable in the InputDescriptor model is renamed to 'uiSpecifications' for better readability. Additionally, the associated comment was revised to explain that the dictionary is used by the UI. * "Refactor codebase for improved organization and cleaner architecture" The codebase has been significantly refactored, moving several classes to more appropriate namespaces for improved organization and cleaner architecture. This includes shifting UI hint handlers, activities, and memory-related components, amongst others. The changes should improve code readability and maintainability, but as this is a broad refactoring effort, thorough regression testing is advised. * Add CheckList UIHint with associated handler and provider This update introduces a new UIHint called CheckList to the Elsa.Workflows.Core. This includes the necessary handler and provider classes. The handler is registered in the WorkflowsFeature.cs, and the CheckList UIHint key has been added to the InputUIHints.cs. Various associated files have been created in both the Elsa.Api.Client and Elsa.Workflows.Core project to support this new UIHint. --------- Co-authored-by: Jérémie DEVILLARD <jdevillard@users.noreply.github.com> Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com>
2023-12-26 17:56:29 +00:00
/// <summary>
/// The application properties to embed with the Service Bus Message
/// </summary>
[Input(Category = "Advanced",
DefaultSyntax = "Json",
SupportedSyntaxes = new[] { "JavaScript", "Json" },
UIHint = InputUIHints.MultiLine)
]
public Input<IDictionary<string, object>?> ApplicationProperties { get; set; } = default;
/// <inheritdoc />
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
var queueOrTopic = context.Get(QueueOrTopic);
var messageBody = context.Get(MessageBody);
var cancellationToken = context.CancellationToken;
var serializedMessageBody = await SerializeMessageBodyAsync(context, messageBody!, cancellationToken);
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
// TODO: Maybe expose additional members?
};
if (context.Get(ApplicationProperties) != null)
foreach (var property in context.Get(ApplicationProperties))
message.ApplicationProperties.Add(property.Key, ((System.Text.Json.JsonElement)property.Value).GetString());
var client = context.GetRequiredService<ServiceBusClient>();
await using var sender = client.CreateSender(queueOrTopic);
await sender.SendMessageAsync(message, cancellationToken);
}
private async ValueTask<BinaryData> SerializeMessageBodyAsync(ActivityExecutionContext context, object value, CancellationToken cancellationToken)
{
if (value is string s) return BinaryData.FromString(s);
2023-03-24 19:57:27 +00:00
var formatterType = FormatterType.GetOrDefault(context) ?? typeof(JsonFormatter);
var formatter = context.GetServices<IFormatter>().First(x => x.GetType() == formatterType);
var data = await formatter.ToStringAsync(value, cancellationToken);
return BinaryData.FromString(data);
}
}