コード例 #1
0
        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));
        };
コード例 #2
0
        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));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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));
        };
コード例 #5
0
 internal static IServiceCollection AddRabbitMQPersistentConnection(this IServiceCollection services, RabbitMQOptions options) =>
 services.AddSingleton(RabbitMQPersistentConnectionBuilder(options));
コード例 #6
0
 internal static IServiceCollection AddEventBus(this IServiceCollection services, RabbitMQOptions options) =>
 services.AddSingleton <IEventBus, EventBusRabbitMQ>(EventBusRabbitMQBuilder(options))
 .AddSingleton <IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();