예제 #1
0
        public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
        {
            if (services.Any(x => x.ServiceType == typeof(IEventBus)))
            {
                return(services);
            }
            services.AddScoped <IIncomingEventHandler, IncomingEventHandler>();
            services.AddConfigurations(configuration);

            services.AddRabbitMqClient(configuration.GetSection(RabbitMqOptions.Key));
            services.AddSingleton <ConnectionFactory>(x =>
            {
                var factory = new ConnectionFactory();

                return(factory.ConfigureFromSection(configuration.GetSection(RabbitMqOptions.Key)));
            });
            services.AddSingleton <IRabbitMQPersistentConnection, DefaultRabbitMQPersistentConnection>(sp =>
                                                                                                       new DefaultRabbitMQPersistentConnection(sp.GetRequiredService <ConnectionFactory>(),
                                                                                                                                               sp.GetRequiredService <IConfiguration>(),
                                                                                                                                               sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(),
                                                                                                                                               sp.GetRequiredService <RabbitMqConnectionOptionsContainer>(),
                                                                                                                                               sp.GetRequiredService <IHostApplicationLifetime>()
                                                                                                                                               ));
            services.AddSingleton <ISubscriptionsManager, SubscriptionsManager>();
            services.AddSingleton <IEventBus, RabbitMQEventBus>();
            services.AddEventHandlers();
            services.AddHostedService <EventBusSubscriptionService>();

            return(services);
        }