/// <summary>
        /// Initializes a new instance of the <see cref="RabbitMqHostBuilder"/> class.
        /// </summary>
        /// <param name="services"><see cref="IServiceCollection"/></param>
        /// <param name="connectionName">The client-provided connection name.</param>
        /// <param name="configuration">The <see cref="IConfiguration"/> being bound.</param>
        public RabbitMqHostBuilder(IServiceCollection services, string connectionName, IConfiguration configuration)
            : this(services, connectionName)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            services.AddOptions();
            services.Configure <RabbitMqOptions>(ConnectionName, configuration);

            _hostConfiguratorFactory = serviceProvider =>
            {
                var optionsSnapshot = serviceProvider.GetRequiredService <IOptionsSnapshot <RabbitMqOptions> >();
                var options         = optionsSnapshot.Get(ConnectionName);

                var hostConfigurator = new RabbitMqHostConfigurator(options.HostAddress, ConnectionName);

                if (options.Heartbeat.HasValue)
                {
                    hostConfigurator.Heartbeat(options.Heartbeat.Value);
                }

                if (!string.IsNullOrEmpty(options.Username))
                {
                    hostConfigurator.Username(options.Username);
                }

                if (!string.IsNullOrEmpty(options.Password))
                {
                    hostConfigurator.Password(options.Password);
                }

                return(hostConfigurator);
            };
        }
示例#2
0
 /// <inheritdoc />
 public virtual IRabbitMqHostBuilder UseHeartbeat(ushort heartbeat)
 {
     _hostConfigurator.Heartbeat(heartbeat);
     return(this);
 }