private static Func <IServiceProvider, IRabbitMQPersistentConnection> RabbitMQPersistentConnectionBuilder( RabbitMQOptions options) => serviceProvider => { var logger = serviceProvider.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >(); var factory = new ConnectionFactory() { HostName = options.HostName, DispatchConsumersAsync = true }; if (!string.IsNullOrEmpty(options.Username)) { factory.UserName = options.Username; } if (!string.IsNullOrEmpty(options.Password)) { factory.Password = options.Password; } var retryCount = options.RetryCount != 0 ? options.RetryCount : 5; return(new DefaultRabbitMQPersistentConnection(factory, logger, retryCount)); };
public static IServiceCollection AddRabbitMQEventBus( this IServiceCollection services, Action <RabbitMQOptions> optionsBuilder) { var options = new RabbitMQOptions(); optionsBuilder(options); options = options.Validate(); return(services .AddEventBus(options) .AddRabbitMQPersistentConnection(options)); }
private static RabbitMQOptions Validate(this RabbitMQOptions options) { Ensure.String.IsNotNullOrWhiteSpace(options.HostName, nameof(options.HostName)); Ensure.String.IsNotEmptyOrWhiteSpace(options.HostName, nameof(options.HostName)); Ensure.String.IsNotEmptyOrWhiteSpace(options.SubscriptionName, nameof(options.SubscriptionName)); Ensure.String.IsNotNullOrWhiteSpace(options.SubscriptionName, nameof(options.SubscriptionName)); if (options.RetryCount != 0) { Ensure.Comparable.IsGt(options.RetryCount, 0, nameof(options.RetryCount)); } return(options); }
private static Func <IServiceProvider, EventBusRabbitMQ> EventBusRabbitMQBuilder(RabbitMQOptions options) => sp => { var subscriptionClientName = options.SubscriptionName; var rabbitMQPersistentConnection = sp.GetRequiredService <IRabbitMQPersistentConnection>(); var logger = sp.GetRequiredService <ILogger <EventBusRabbitMQ> >(); var eventBusSubcriptionsManager = sp.GetRequiredService <IEventBusSubscriptionsManager>(); var retryCount = options.RetryCount != 0 ? options.RetryCount : 5; return(new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, sp, eventBusSubcriptionsManager, subscriptionClientName, retryCount)); };
internal static IServiceCollection AddRabbitMQPersistentConnection(this IServiceCollection services, RabbitMQOptions options) => services.AddSingleton(RabbitMQPersistentConnectionBuilder(options));
internal static IServiceCollection AddEventBus(this IServiceCollection services, RabbitMQOptions options) => services.AddSingleton <IEventBus, EventBusRabbitMQ>(EventBusRabbitMQBuilder(options)) .AddSingleton <IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();