using Elsa.Features.Services;
using Elsa.Workflows.Core;
using MassTransit;
// ReSharper disable once CheckNamespace
namespace Elsa.Extensions;
public static class ModuleExtensions
{
private static readonly object ServiceBusConsumerTypesKey = new();
///
/// Registers the specified type for MassTransit service bus consumer discovery.
///
public static void AddMassTransitServiceBusConsumerType(this IModule module, Type type)
{
var types = DictionaryExtensions.GetOrAdd(module.Properties, ServiceBusConsumerTypesKey, () => new HashSet());
types.Add(type);
}
///
/// Returns all collected types for discovery of service bus consumers.
///
private static IEnumerable GetServiceBusConsumerTypesFromModule(this IModule module) => DictionaryExtensions.GetOrAdd(module.Properties, ServiceBusConsumerTypesKey, () => new HashSet());
///
/// Adds MassTransit to the service container and registers all collected assemblies for discovery of consumers.
///
public static IModule AddMassTransitFromModule(this IModule module, Action config)
{
var consumerTypes = module.GetServiceBusConsumerTypesFromModule().ToArray();
module.Services.AddMassTransit(bus =>
{
bus.SetKebabCaseEndpointNameFormatter();
bus.AddConsumers(consumerTypes);
config?.Invoke(bus);
});
return module;
}
}