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 AddMongo(this IConveyBuilder builder, MongoDbOptions mongoOptions, Type seederType = null, bool registerConventions = true) { if (!builder.TryRegister(RegistryName)) { return(builder); } if (mongoOptions.SetRandomDatabaseSuffix) { var suffix = $"{Guid.NewGuid():N}"; Console.WriteLine($"Setting a random MongoDB database suffix: '{suffix}'."); mongoOptions.Database = $"{mongoOptions.Database}_{suffix}"; } builder.Services.AddSingleton(mongoOptions); builder.Services.AddSingleton <IMongoClient>(sp => { var options = sp.GetService <MongoDbOptions>(); return(new MongoClient(options.ConnectionString)); }); builder.Services.AddTransient(sp => { var options = sp.GetService <MongoDbOptions>(); var client = sp.GetService <IMongoClient>(); return(client.GetDatabase(options.Database)); }); builder.Services.AddTransient <IMongoDbInitializer, MongoDbInitializer>(); builder.Services.AddTransient <IMongoSessionFactory, MongoSessionFactory>(); if (seederType is null) { builder.Services.AddTransient <IMongoDbSeeder, MongoDbSeeder>(); } else { builder.Services.AddTransient(typeof(IMongoDbSeeder), seederType); } builder.AddInitializer <IMongoDbInitializer>(); if (registerConventions && !_conventionsRegistered) { RegisterConventions(); } return(builder); }
public static IConveyBuilder AddMongo(this IConveyBuilder builder, MongoDbOptions mongoOptions, Type seederType = null, bool registerConventions = true) { if (!builder.TryRegister(RegistryName)) { return(builder); } builder.Services.AddSingleton(mongoOptions); builder.Services.AddSingleton <IMongoClient>(sp => { var options = sp.GetService <MongoDbOptions>(); return(new MongoClient(options.ConnectionString)); }); builder.Services.AddTransient(sp => { var options = sp.GetService <MongoDbOptions>(); var client = sp.GetService <IMongoClient>(); return(client.GetDatabase(options.Database)); }); builder.Services.AddTransient <IMongoDbInitializer, MongoDbInitializer>(); builder.Services.AddTransient <IMongoSessionFactory, MongoSessionFactory>(); if (seederType is null) { builder.Services.AddTransient <IMongoDbSeeder, MongoDbSeeder>(); } else { builder.Services.AddTransient(typeof(IMongoDbSeeder), seederType); } builder.AddInitializer <IMongoDbInitializer>(); if (registerConventions && !_conventionsRegistered) { RegisterConventions(); } return(builder); }
public static IConveyBuilder AddMessageInbox(this IConveyBuilder builder, string sectionName = SectionName) { if (!builder.TryRegister(RegistryName)) { return(builder); } var options = builder.GetOptions <InboxOptions>(sectionName); builder.Services.AddSingleton(options); if (!options.Enabled) { builder.RegisterInMemoryInbox(); return(builder); } switch (options.Type?.ToLowerInvariant() ?? string.Empty) { case "memory": builder.RegisterInMemoryInbox(); break; case "mongo": builder.AddMongo(); builder.Services.AddTransient <IMessageInbox, MongoMessageInbox>(); builder.Services.AddTransient <MongoInboxInitializer>(); builder.AddInitializer <MongoInboxInitializer>(); break; case "redis": builder.Services.AddTransient <IMessageInbox, RedisMessageInbox>(); break; default: builder.RegisterInMemoryInbox(); break; } return(builder); }
public static IConveyBuilder AddMongo(this IConveyBuilder builder, MongoDbOptions mongoOptions, IMongoDbSeeder seeder = null) { if (!builder.TryRegister(RegistryName)) { return(builder); } builder.Services.AddSingleton(mongoOptions); builder.Services.AddSingleton <IMongoClient>(sp => { var options = sp.GetService <MongoDbOptions>(); return(new MongoClient(options.ConnectionString)); }); builder.Services.AddTransient(sp => { var options = sp.GetService <MongoDbOptions>(); var client = sp.GetService <MongoClient>(); return(client.GetDatabase(options.Database)); }); builder.Services.AddTransient <IMongoDbInitializer, MongoDbInitializer>(); builder.Services.AddTransient <IMongoSessionFactory, MongoSessionFactory>(); if (seeder is null) { builder.Services.AddSingleton <IMongoDbSeeder, MongoDbSeeder>(); } else { builder.Services.AddSingleton(seeder); } builder.AddInitializer <IMongoDbInitializer>(); return(builder); }
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); }