public static IConveyBuilder AddRabbitMq(this IConveyBuilder builder, string sectionName = SectionName, Func <IRabbitMqPluginsRegistry, IRabbitMqPluginsRegistry> plugins = null) { if (string.IsNullOrWhiteSpace(sectionName)) { sectionName = SectionName; } var options = builder.GetOptions <RabbitMqOptions>(sectionName); builder.Services.AddSingleton(options); if (!builder.TryRegister(RegistryName)) { Console.WriteLine("NOPE"); return(builder); } if (options.HostNames is null || !options.HostNames.Any()) { throw new ArgumentException("RabbitMQ hostnames are not specified.", nameof(options.HostNames)); } builder.Services.AddSingleton <IContextProvider, ContextProvider>(); builder.Services.AddSingleton <ICorrelationContextAccessor>(new CorrelationContextAccessor()); builder.Services.AddSingleton <IMessagePropertiesAccessor>(new MessagePropertiesAccessor()); builder.Services.AddSingleton <IConventionsBuilder, ConventionsBuilder>(); builder.Services.AddSingleton <IConventionsProvider, ConventionsProvider>(); builder.Services.AddSingleton <IConventionsRegistry, ConventionsRegistry>(); builder.Services.AddSingleton <IRabbitMqSerializer, NewtonsoftJsonRabbitMqSerializer>(); builder.Services.AddSingleton <IRabbitMqClient, RabbitMqClient>(); builder.Services.AddSingleton <IBusPublisher, RabbitMqPublisher>(); builder.Services.AddSingleton <IBusSubscriber, RabbitMqSubscriber>(); builder.Services.AddTransient <RabbitMqExchangeInitializer>(); builder.Services.AddHostedService <RabbitMqHostedService>(); builder.AddInitializer <RabbitMqExchangeInitializer>(); var pluginsRegistry = new RabbitMqPluginsRegistry(); builder.Services.AddSingleton <IRabbitMqPluginsRegistryAccessor>(pluginsRegistry); builder.Services.AddSingleton <IRabbitMqPluginsExecutor, RabbitMqPluginsExecutor>(); plugins?.Invoke(pluginsRegistry); var connection = new ConnectionFactory { Port = options.Port, VirtualHost = options.VirtualHost, UserName = options.Username, Password = options.Password, RequestedConnectionTimeout = options.RequestedConnectionTimeout, SocketReadTimeout = options.SocketReadTimeout, SocketWriteTimeout = options.SocketWriteTimeout, RequestedChannelMax = options.RequestedChannelMax, RequestedFrameMax = options.RequestedFrameMax, RequestedHeartbeat = options.RequestedHeartbeat, UseBackgroundThreadsForIO = options.UseBackgroundThreadsForIO, DispatchConsumersAsync = true, Ssl = options.Ssl is null ? new SslOption() : new SslOption(options.Ssl.ServerName, options.Ssl.CertificatePath, options.Ssl.Enabled) }.CreateConnection(options.HostNames.ToList(), options.ConnectionName);
public static IConveyBuilder AddRabbitMq(this IConveyBuilder builder, string sectionName = SectionName, Func <IRabbitMqPluginsRegistry, IRabbitMqPluginsRegistry> plugins = null) { if (string.IsNullOrWhiteSpace(sectionName)) { sectionName = SectionName; } var options = builder.GetOptions <RabbitMqOptions>(sectionName); builder.Services.AddSingleton(options); if (!builder.TryRegister(RegistryName)) { return(builder); } builder.Services.AddSingleton <IContextProvider, ContextProvider>(); builder.Services.AddSingleton <ICorrelationContextAccessor>(new CorrelationContextAccessor()); builder.Services.AddSingleton <IMessagePropertiesAccessor>(new MessagePropertiesAccessor()); builder.Services.AddSingleton <IConventionsBuilder, ConventionsBuilder>(); builder.Services.AddSingleton <IConventionsProvider, ConventionsProvider>(); builder.Services.AddSingleton <IConventionsRegistry, ConventionsRegistry>(); builder.Services.AddSingleton <IRabbitMqSerializer, NewtonsoftJsonRabbitMqSerializer>(); builder.Services.AddSingleton <IRabbitMqClient, RabbitMqClient>(); builder.Services.AddSingleton <IBusPublisher, RabbitMqPublisher>(); builder.Services.AddSingleton <IBusSubscriber, RabbitMqSubscriber>(); builder.Services.AddTransient <RabbitMqExchangeInitializer>(); builder.AddInitializer <RabbitMqExchangeInitializer>(); var pluginsRegistry = new RabbitMqPluginsRegistry(); builder.Services.AddSingleton <IRabbitMqPluginsRegistryAccessor>(pluginsRegistry); builder.Services.AddSingleton <IRabbitMqPluginsExecutor, RabbitMqPluginsExecutor>(); plugins?.Invoke(pluginsRegistry); if (options.MessageProcessor?.Enabled == true) { pluginsRegistry.Add <UniqueMessagesPlugin>(); switch (options.MessageProcessor.Type?.ToLowerInvariant()) { case "distributed": builder.Services.AddTransient <IMessageProcessor, DistributedMessageProcessor>(); break; default: builder.Services.AddTransient <IMessageProcessor, InMemoryMessageProcessor>(); break; } } else { builder.Services.AddSingleton <IMessageProcessor, EmptyMessageProcessor>(); } builder.Services.AddSingleton(sp => { var connectionFactory = new ConnectionFactory { HostName = options.HostNames?.FirstOrDefault(), Port = options.Port, VirtualHost = options.VirtualHost, UserName = options.Username, Password = options.Password, RequestedConnectionTimeout = options.RequestedConnectionTimeout, SocketReadTimeout = options.SocketReadTimeout, SocketWriteTimeout = options.SocketWriteTimeout, RequestedChannelMax = options.RequestedChannelMax, RequestedFrameMax = options.RequestedFrameMax, RequestedHeartbeat = options.RequestedHeartbeat, UseBackgroundThreadsForIO = options.UseBackgroundThreadsForIO, DispatchConsumersAsync = true, Ssl = options.Ssl is null ? new SslOption() : new SslOption(options.Ssl.ServerName, options.Ssl.CertificatePath, options.Ssl.Enabled) }; return(connectionFactory.CreateConnection(options.ConnectionName)); }); ((IRabbitMqPluginsRegistryAccessor)pluginsRegistry).Get().ToList().ForEach(p => builder.Services.AddTransient(p.PluginType)); return(builder); }
public static IConveyBuilder AddRabbitMq(this IConveyBuilder builder, string sectionName = SectionName, Func <IRabbitMqPluginsRegistry, IRabbitMqPluginsRegistry> plugins = null, Action <ConnectionFactory> connectionFactoryConfigurator = null, IRabbitMqSerializer serializer = null) { if (string.IsNullOrWhiteSpace(sectionName)) { sectionName = SectionName; } var options = builder.GetOptions <RabbitMqOptions>(sectionName); builder.Services.AddSingleton(options); if (!builder.TryRegister(RegistryName)) { return(builder); } if (options.HostNames is null || !options.HostNames.Any()) { throw new ArgumentException("RabbitMQ hostnames are not specified.", nameof(options.HostNames)); } ILogger <IRabbitMqClient> logger; using (var serviceProvider = builder.Services.BuildServiceProvider()) { logger = serviceProvider.GetRequiredService <ILogger <IRabbitMqClient> >(); } builder.Services.AddSingleton <IContextProvider, ContextProvider>(); builder.Services.AddSingleton <ICorrelationContextAccessor>(new CorrelationContextAccessor()); builder.Services.AddSingleton <IMessagePropertiesAccessor>(new MessagePropertiesAccessor()); builder.Services.AddSingleton <IConventionsBuilder, ConventionsBuilder>(); builder.Services.AddSingleton <IConventionsProvider, ConventionsProvider>(); builder.Services.AddSingleton <IConventionsRegistry, ConventionsRegistry>(); builder.Services.AddSingleton <IRabbitMqClient, RabbitMqClient>(); builder.Services.AddSingleton <IBusPublisher, RabbitMqPublisher>(); builder.Services.AddSingleton <IBusSubscriber, RabbitMqSubscriber>(); builder.Services.AddSingleton <MessageSubscribersChannel>(); builder.Services.AddTransient <RabbitMqExchangeInitializer>(); builder.Services.AddHostedService <RabbitMqBackgroundService>(); builder.AddInitializer <RabbitMqExchangeInitializer>(); if (serializer is not null) { builder.Services.AddSingleton(serializer); } else { builder.Services.AddSingleton <IRabbitMqSerializer, SystemTextJsonJsonRabbitMqSerializer>(); } var pluginsRegistry = new RabbitMqPluginsRegistry(); builder.Services.AddSingleton <IRabbitMqPluginsRegistryAccessor>(pluginsRegistry); builder.Services.AddSingleton <IRabbitMqPluginsExecutor, RabbitMqPluginsExecutor>(); plugins?.Invoke(pluginsRegistry); var connectionFactory = new ConnectionFactory { Port = options.Port, VirtualHost = options.VirtualHost, UserName = options.Username, Password = options.Password, RequestedHeartbeat = options.RequestedHeartbeat, RequestedConnectionTimeout = options.RequestedConnectionTimeout, SocketReadTimeout = options.SocketReadTimeout, SocketWriteTimeout = options.SocketWriteTimeout, RequestedChannelMax = options.RequestedChannelMax, RequestedFrameMax = options.RequestedFrameMax, UseBackgroundThreadsForIO = options.UseBackgroundThreadsForIO, DispatchConsumersAsync = true, ContinuationTimeout = options.ContinuationTimeout, HandshakeContinuationTimeout = options.HandshakeContinuationTimeout, NetworkRecoveryInterval = options.NetworkRecoveryInterval, Ssl = options.Ssl is null ? new SslOption() : new SslOption(options.Ssl.ServerName, options.Ssl.CertificatePath, options.Ssl.Enabled) }; ConfigureSsl(connectionFactory, options, logger); connectionFactoryConfigurator?.Invoke(connectionFactory); logger.LogDebug($"Connecting to RabbitMQ: '{string.Join(", ", options.HostNames)}'..."); var consumerConnection = connectionFactory.CreateConnection(options.HostNames.ToList(), $"{options.ConnectionName}_consumer"); var producerConnection = connectionFactory.CreateConnection(options.HostNames.ToList(), $"{options.ConnectionName}_producer"); logger.LogDebug($"Connected to RabbitMQ: '{string.Join(", ", options.HostNames)}'."); builder.Services.AddSingleton(new ConsumerConnection(consumerConnection)); builder.Services.AddSingleton(new ProducerConnection(producerConnection)); ((IRabbitMqPluginsRegistryAccessor)pluginsRegistry).Get().ToList().ForEach(p => builder.Services.AddTransient(p.PluginType)); return(builder); }