示例#1
0
        public static IServiceCollection AddRabbitMqConnection(this IServiceCollection services, RabbitMqOptions options)
        {
            services.AddSingleton <IRabbitMqPersistentConnection>(sp =>
            {
                var retryCount = 5;
                var factory    = new ConnectionFactory
                {
                    HostName = options.Host,
                    DispatchConsumersAsync = options.DispatchConsumersAsync
                };

                if (!string.IsNullOrEmpty(options.VirtualHost))
                {
                    factory.VirtualHost = options.VirtualHost;
                }

                if (!string.IsNullOrEmpty(options.Username))
                {
                    factory.UserName = options.Username;
                }

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

                if (!string.IsNullOrEmpty(options.RetryCount))
                {
                    retryCount = int.Parse(options.RetryCount);
                }

                return(new DefaultRabbitMqPersistentConnection(factory, retryCount));
            });

            return(services);
        }
示例#2
0
        public static IServiceCollection AddRabbitMqRegistration(this IServiceCollection services, RabbitMqOptions options)
        {
            services.AddSingleton <IEventBus, EventBusRabbitMq>(sp =>
            {
                var rabbitMqPersistentConnection = sp.GetRequiredService <IRabbitMqPersistentConnection>();
                var lifetimeScope = sp.GetRequiredService <ILifetimeScope>();
                var eventBusSubscriptionsManager = sp.GetRequiredService <IEventBusSubscriptionManager>();
                var brokerName       = options.BrokerName;
                var autofacScopeName = options.AutofacScopeName;
                var queueName        = options.QueueName;
                var retryCount       = 5;

                if (!string.IsNullOrEmpty(options.RetryCount))
                {
                    retryCount = int.Parse(options.RetryCount);
                }

                return(new EventBusRabbitMq(rabbitMqPersistentConnection,
                                            lifetimeScope,
                                            eventBusSubscriptionsManager,
                                            brokerName,
                                            autofacScopeName,
                                            queueName,
                                            retryCount));
            });

            services.AddSingleton <IEventBusSubscriptionManager, InMemoryEventBusSubscriptionManager>();
            return(services);
        }