private static void ConfigureServices(IServiceCollection services) { var rabbitMqConfiguration = new RabbitMqServiceOptions { HostName = "127.0.0.1", Port = 5672, UserName = "******", Password = "******" }; var exchangeOptions = new RabbitMqExchangeOptions { Queues = new List <RabbitMqQueueOptions> { new RabbitMqQueueOptions { Name = "myqueue", RoutingKeys = new HashSet <string> { "routing.key" } } } }; services.AddRabbitMqProducer(rabbitMqConfiguration) .AddProductionExchange("exchange.name", exchangeOptions); }
/// <inheritdoc/> public IConnection CreateRabbitMqConnection(RabbitMqServiceOptions options) { if (options is null) { return(null); } var factory = new ConnectionFactory { Port = options.Port, UserName = options.UserName, Password = options.Password, VirtualHost = options.VirtualHost, AutomaticRecoveryEnabled = options.AutomaticRecoveryEnabled, TopologyRecoveryEnabled = options.TopologyRecoveryEnabled, RequestedConnectionTimeout = options.RequestedConnectionTimeout, RequestedHeartbeat = options.RequestedHeartbeat, DispatchConsumersAsync = true }; if (options.TcpEndpoints?.Any() == true) { return(CreateConnectionWithTcpEndpoints(options, factory)); } return(string.IsNullOrEmpty(options.ClientProvidedName) ? CreateConnection(options, factory) : CreateNamedConnection(options, factory)); }
private static IConnection CreateConnectionWithTcpEndpoints(RabbitMqServiceOptions options, ConnectionFactory factory) { var clientEndpoints = new List <AmqpTcpEndpoint>(); foreach (var endpoint in options.TcpEndpoints) { var sslOption = endpoint.SslOption; if (sslOption != null) { var convertedOption = new SslOption(sslOption.ServerName, sslOption.CertificatePath, sslOption.Enabled); if (!string.IsNullOrEmpty(sslOption.CertificatePassphrase)) { convertedOption.CertPassphrase = sslOption.CertificatePassphrase; } if (sslOption.AcceptablePolicyErrors != null) { convertedOption.AcceptablePolicyErrors = sslOption.AcceptablePolicyErrors.Value; } clientEndpoints.Add(new AmqpTcpEndpoint(endpoint.HostName, endpoint.Port, convertedOption)); } else { clientEndpoints.Add(new AmqpTcpEndpoint(endpoint.HostName, endpoint.Port)); } } return(TryToCreateConnection(() => factory.CreateConnection(clientEndpoints), options.InitialConnectionRetries, options.InitialConnectionRetryTimeoutMilliseconds)); }
public static async Task Main() { var builder = new HostBuilder() .ConfigureAppConfiguration((hostingContext, config) => { config.AddJsonFile("appsettings.json", optional: false); }) .ConfigureServices((hostContext, services) => { // Let's configure two different BatchMessageHandlers with different methods. // First - configuring an appsettings.json section. services.AddBatchMessageHandler <CustomBatchMessageHandler>(hostContext.Configuration.GetSection("RabbitMq")); // Second one - passing configuration instance. var rabbitMqConfiguration = new RabbitMqServiceOptions { HostName = "127.0.0.1", Port = 5672, UserName = "******", Password = "******" }; services.AddBatchMessageHandler <CustomBatchMessageHandler>(rabbitMqConfiguration); // Use either of them. Do not register batch message handlers multiple times in your real project. }) .ConfigureLogging((hostingContext, logging) => { logging.AddConsole(); }); await builder.RunConsoleAsync(); }
internal static RabbitMqServiceOptions GetRabbitMqServiceOptionsInstance(IConfiguration configuration) { var options = new RabbitMqServiceOptions(); configuration.Bind(options); return(options); }
private static IConnection CreateConnection(RabbitMqServiceOptions options, ConnectionFactory factory) { if (options.HostNames?.Any() == true) { return(TryToCreateConnection(() => factory.CreateConnection(options.HostNames.ToList()), options.InitialConnectionRetries, options.InitialConnectionRetryTimeoutMilliseconds)); } factory.HostName = options.HostName; return(TryToCreateConnection(factory.CreateConnection, options.InitialConnectionRetries, options.InitialConnectionRetryTimeoutMilliseconds)); }
public void ShouldProperlyRetryCreatingInitialConnection(int retries) { var connectionOptions = new RabbitMqServiceOptions { HostName = "anotherHost", InitialConnectionRetries = retries, InitialConnectionRetryTimeoutMilliseconds = 20 }; ExecuteUnsuccessfulConnectionCreationAndAssertResults(connectionOptions); }
protected BaseBatchMessageHandler( IRabbitMqConnectionFactory rabbitMqConnectionFactory, IEnumerable <BatchConsumerConnectionOptions> batchConsumerConnectionOptions, IEnumerable <IBatchMessageHandlingMiddleware> batchMessageHandlingMiddlewares, ILoggingService loggingService) { var optionsContainer = batchConsumerConnectionOptions.FirstOrDefault(x => x.Type == GetType()); if (optionsContainer is null) { throw new ArgumentNullException($"Client connection options for {nameof(BaseBatchMessageHandler)} has not been found.", nameof(batchConsumerConnectionOptions)); } _serviceOptions = optionsContainer.ServiceOptions; _rabbitMqConnectionFactory = rabbitMqConnectionFactory; _batchMessageHandlingMiddlewares = batchMessageHandlingMiddlewares; _loggingService = loggingService; }
protected BaseBatchMessageHandler( IRabbitMqConnectionFactory rabbitMqConnectionFactory, IEnumerable <BatchConsumerConnectionOptions> batchConsumerConnectionOptions, IEnumerable <IBatchMessageHandlingFilter> batchMessageHandlingFilters, ILogger <BaseBatchMessageHandler> logger) { var optionsContainer = batchConsumerConnectionOptions.FirstOrDefault(x => x.Type == GetType()); if (optionsContainer is null) { throw new ArgumentNullException($"Client connection options for {nameof(BaseBatchMessageHandler)} has not been found.", nameof(batchConsumerConnectionOptions)); } _serviceOptions = optionsContainer.ServiceOptions ?? throw new ArgumentNullException($"Consumer client options is null for {nameof(BaseBatchMessageHandler)}.", nameof(optionsContainer.ServiceOptions)); _rabbitMqConnectionFactory = rabbitMqConnectionFactory; _batchMessageHandlingFilters = batchMessageHandlingFilters ?? Enumerable.Empty <IBatchMessageHandlingFilter>(); _logger = logger; }
/// <summary> /// Add a singleton producing RabbitMQ service <see cref="IProducingService"/> and required infrastructure. /// </summary> /// <param name="services">Service collection.</param> /// <param name="configuration">RabbitMq configuration <see cref="RabbitMqServiceOptions"/>.</param> /// <param name="behaviourConfiguration">Custom behaviour configuration options.</param> /// <returns>Service collection.</returns> public static IServiceCollection AddRabbitMqProducer(this IServiceCollection services, RabbitMqServiceOptions configuration, Action <BehaviourConfiguration>?behaviourConfiguration = null) { services.AddBehaviourConfiguration(behaviourConfiguration); services.AddRabbitMqInfrastructure(); services.ConfigureRabbitMqProducingOnlyServiceOptions(configuration); services.AddRabbitMqServices(); return(services); }
private IConnection?CreateConnection(RabbitMqServiceOptions options) => _rabbitMqConnectionFactory.CreateRabbitMqConnection(options);
internal static IServiceCollection ConfigureRabbitMqConnectionOptions(this IServiceCollection services, RabbitMqServiceOptions options) => services.Configure <RabbitMqConnectionOptions>(x => { x.ProducerOptions = options; x.ConsumerOptions = options; });
/// <summary> /// Add batch message handler. /// </summary> /// <typeparam name="TBatchMessageHandler">Batch message handler type.</typeparam> /// <param name="services">Service collection.</param> /// <param name="configuration">RabbitMq configuration <see cref="RabbitMqServiceOptions"/>.</param> /// <returns>Service collection.</returns> public static IServiceCollection AddBatchMessageHandler <TBatchMessageHandler>(this IServiceCollection services, RabbitMqServiceOptions configuration) where TBatchMessageHandler : BaseBatchMessageHandler { CheckIfBatchMessageHandlerAlreadyConfigured <TBatchMessageHandler>(services); services.TryAddSingleton <IRabbitMqConnectionFactory, RabbitMqConnectionFactory>(); services.ConfigureBatchConsumerConnectionOptions <TBatchMessageHandler>(configuration); services.AddHostedService <TBatchMessageHandler>(); return(services); }
/// <summary> /// Add a singleton producing RabbitMQ service <see cref="IProducingService"/> and required infrastructure. /// </summary> /// <param name="services">Service collection.</param> /// <param name="configuration">RabbitMq configuration <see cref="RabbitMqServiceOptions"/>.</param> /// <returns>Service collection.</returns> public static IServiceCollection AddRabbitMqProducer(this IServiceCollection services, RabbitMqServiceOptions configuration) { services.AddRabbitMqInfrastructure(); services.ConfigureRabbitMqProducingOnlyServiceOptions(configuration); services.AddRabbitMqServices(); return(services); }
/// <summary> /// Add fully-functional RabbitMQ services and required infrastructure. /// RabbitMQ services consists of two components: consumer <see cref="IConsumingService"/> and producer <see cref="IProducingService"/>. /// </summary> /// <param name="services">Service collection.</param> /// <param name="configuration">RabbitMq configuration <see cref="RabbitMqServiceOptions"/>.</param> /// <returns>Service collection.</returns> public static IServiceCollection AddRabbitMqServices(this IServiceCollection services, RabbitMqServiceOptions configuration) { services.AddRabbitMqInfrastructure(); services.ConfigureRabbitMqConnectionOptions(configuration); services.AddRabbitMqServices(); services.AddConsumptionStarter(); return(services); }
internal static IServiceCollection ConfigureRabbitMqProducingOnlyServiceOptions(this IServiceCollection services, RabbitMqServiceOptions options) => services.Configure <RabbitMqConnectionOptions>(x => x.ProducerOptions = options);
public BatchConsumerConnectionOptions(Type type, RabbitMqServiceOptions options) { Type = type; ServiceOptions = options; }
private static IServiceCollection ConfigureBatchConsumerConnectionOptions <TBatchMessageHandler>(this IServiceCollection services, RabbitMqServiceOptions serviceOptions) where TBatchMessageHandler : BaseBatchMessageHandler { var options = new BatchConsumerConnectionOptions { Type = typeof(TBatchMessageHandler), ServiceOptions = serviceOptions }; var serviceDescriptor = new ServiceDescriptor(typeof(BatchConsumerConnectionOptions), options); services.Add(serviceDescriptor); return(services); }