예제 #1
0
        public static IEventBusBuilder AddEventListeners(this IEventBusBuilder builder, Action <EventServiceCollectionConfigurator> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            var configurator = new ServiceCollectionConfigurator(builder.Services);
            var wrapper      = new EventServiceCollectionConfigurator(configurator);

            configure(wrapper);

            builder.Services.AddSingleton(configurator);
            builder.Services.AddSingleton(provider => EventBusFactory.Build(cfg =>
            {
                var host = cfg.Host(new Uri(builder.Settings.ConnectionString), h =>
                {
                    h.Username(builder.Settings.UserName);
                    h.Password(builder.Settings.Password);
                });

                cfg.ReceiveEndpoint(host, wrapper.EventQueueName, e =>
                {
                    e.LoadFrom(provider);
                });
            }));

            builder.Services.AddSingleton <IHostedService, ServiceBusEventHost>();
            return(builder);
        }
        /// <summary>
        /// Add EventBus related stuff to IoC
        /// </summary>
        /// <param name="services">The services</param>
        /// <param name="configureSettings">Action to be able to configure the event bus</param>
        /// <returns></returns>
        public static IEventBusBuilder AddEventBus(this IServiceCollection services, Action <ServiceBusSettings> configureSettings = null)
        {
            var settings = new ServiceBusSettings();

            configureSettings?.Invoke(settings);

            var builder = new EventBusBuilder(services, settings);

            services.AddSingleton(settings);
            services.AddMassTransit();
            services.AddSingleton(provider => EventBusFactory.Build(cfg =>
            {
                cfg.Host(new Uri(settings.ConnectionString), h =>
                {
                    h.Username(settings.UserName);
                    h.Password(settings.Password);
                });
            }));

            builder.AddEventPublisher();
            return(builder);
        }