using MassTransit; using Microsoft.Extensions.DependencyInjection; using System.Reflection; namespace IM.InitCommon { public static class RabbitMqExtension { public static IServiceCollection AddRabbitMq(this IServiceCollection services, RabbitMqOptions options, IEnumerable assemblies) { var safeAssemblies = assemblies .Where(a => a.FullName != null && !a.FullName.StartsWith("MassTransit", StringComparison.OrdinalIgnoreCase) && !a.FullName.StartsWith("System", StringComparison.OrdinalIgnoreCase) && !a.FullName.StartsWith("Microsoft", StringComparison.OrdinalIgnoreCase)) .ToArray(); return services.AddMassTransit(x => { x.AddConsumers(safeAssemblies); x.UsingRabbitMq((context, cfg) => { cfg.Host(options.Host, (ushort)options.Port, "/", c => { c.Username(options.Username); c.Password(options.Password); }); cfg.UseMessageRetry(retry => retry.Intervals( TimeSpan.FromMilliseconds(200), TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5))); cfg.ConfigureEndpoints(context); }); }); } } }