2022-08-11 10:45:07 +00:00
|
|
|
using Elsa.Features.Services;
|
2023-01-01 17:02:38 +00:00
|
|
|
using Elsa.Workflows.Core;
|
|
|
|
|
using MassTransit;
|
2022-08-11 10:45:07 +00:00
|
|
|
|
2022-12-31 15:16:43 +00:00
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
|
namespace Elsa.Extensions;
|
2022-08-11 10:45:07 +00:00
|
|
|
|
|
|
|
|
public static class ModuleExtensions
|
|
|
|
|
{
|
2023-01-01 17:02:38 +00:00
|
|
|
private static readonly object ServiceBusConsumerTypesKey = new();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers the specified type for MassTransit service bus consumer discovery.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void AddMassTransitServiceBusConsumerType(this IModule module, Type type)
|
|
|
|
|
{
|
|
|
|
|
var types = DictionaryExtensions.GetOrAdd(module.Properties, ServiceBusConsumerTypesKey, () => new HashSet<Type>());
|
|
|
|
|
types.Add(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns all collected types for discovery of service bus consumers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static IEnumerable<Type> GetServiceBusConsumerTypesFromModule(this IModule module) => DictionaryExtensions.GetOrAdd(module.Properties, ServiceBusConsumerTypesKey, () => new HashSet<Type>());
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds MassTransit to the service container and registers all collected assemblies for discovery of consumers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static IModule AddMassTransitFromModule(this IModule module, Action<IBusRegistrationConfigurator> config)
|
2022-08-11 10:45:07 +00:00
|
|
|
{
|
2023-01-01 17:02:38 +00:00
|
|
|
var consumerTypes = module.GetServiceBusConsumerTypesFromModule().ToArray();
|
|
|
|
|
|
|
|
|
|
module.Services.AddMassTransit(bus =>
|
|
|
|
|
{
|
|
|
|
|
bus.SetKebabCaseEndpointNameFormatter();
|
|
|
|
|
|
|
|
|
|
bus.AddConsumers(consumerTypes);
|
|
|
|
|
|
|
|
|
|
config?.Invoke(bus);
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-11 10:45:07 +00:00
|
|
|
return module;
|
|
|
|
|
}
|
|
|
|
|
}
|