Ejemplo n.º 1
0
        public static ICommandBusBuilder AddCommandListeners(this ICommandBusBuilder builder, Action <CommandServiceCollectionConfigurator> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

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

            configure(wrapper);

            builder.Services.AddSingleton(configurator);
            builder.Services.AddSingleton(provider => CommandBusFactory.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.CommandQueueName, e =>
                {
                    e.LoadFrom(provider);
                });
            }));

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

            configureSettings?.Invoke(settings);

            var builder = new CommandBusBuilder(services, settings);

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

            builder.AddCommandPublisher();
            return(builder);
        }