/// <summary>
        /// 添加RabbitMQEventBus
        /// </summary>
        /// <param name="services"></param>
        /// <param name="connectionAction">使用匿名函数取得连接字符串,用来兼容使用Consul获取服务地址的情况</param>
        /// <param name="eventBusOptionAction"></param>
        /// <returns></returns>
        public static IServiceCollection AddRabbitMQEventBus(this IServiceCollection services, Func <string> connectionAction, Action <RabbitMQEventBusConnectionConfigurationBuild> eventBusOptionAction)
        {
            RabbitMQEventBusConnectionConfiguration      configuration      = new RabbitMQEventBusConnectionConfiguration();
            RabbitMQEventBusConnectionConfigurationBuild configurationBuild = new RabbitMQEventBusConnectionConfigurationBuild(configuration);

            eventBusOptionAction?.Invoke(configurationBuild);
            services.TryAddSingleton <IRabbitMQPersistentConnection>(options =>
            {
                ILogger <DefaultRabbitMQPersistentConnection> logger = options.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >();
                var connection = new DefaultRabbitMQPersistentConnection(configuration, connectionAction, logger);
                connection.TryConnect();
                return(connection);
            });
            services.TryAddSingleton <IEventHandlerModuleFactory, EventHandlerModuleFactory>();
            services.TryAddSingleton <IRabbitMQEventBus, DefaultRabbitMQEventBus>();
            foreach (Type mType in typeof(IEvent).GetAssemblies())
            {
                services.TryAddTransient(mType);
                foreach (Type hType in typeof(IEventHandler <>).GetMakeGenericType(mType))
                {
                    services.TryAddTransient(hType);
                }
            }
            return(services);
        }
Exemple #2
0
        /// <summary>
        /// 添加RabbitMQEventBus
        /// </summary>
        /// <param name="services"></param>
        /// <param name="connectionString"></param>
        /// <param name="eventBusOptionAction"></param>
        /// <returns></returns>
        public static IServiceCollection AddRabbitMQEventBus(this IServiceCollection services, string connectionString, Action <RabbitMQEventBusConnectionConfigurationBuild> eventBusOptionAction)
        {
            RabbitMQEventBusConnectionConfiguration      configuration      = new RabbitMQEventBusConnectionConfiguration();
            RabbitMQEventBusConnectionConfigurationBuild configurationBuild = new RabbitMQEventBusConnectionConfigurationBuild(configuration);

            eventBusOptionAction?.Invoke(configurationBuild);
            services.TryAddSingleton <IRabbitMQPersistentConnection>(options =>
            {
                ILogger <DefaultRabbitMQPersistentConnection> logger = options.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >();
                IConnectionFactory factory = new ConnectionFactory
                {
                    AutomaticRecoveryEnabled = configuration.AutomaticRecoveryEnabled,
                    NetworkRecoveryInterval  = configuration.NetworkRecoveryInterval,
                    Uri = new Uri(connectionString),
                };
                var connection = new DefaultRabbitMQPersistentConnection(configuration, factory, logger);
                connection.TryConnect();
                return(connection);
            });
            services.TryAddSingleton <IEventHandlerModuleFactory, EventHandlerModuleFactory>();
            services.TryAddSingleton <IRabbitMQEventBus, DefaultRabbitMQEventBus>();
            foreach (Type mType in typeof(IEvent).GetAssemblies())
            {
                services.TryAddTransient(mType);
                foreach (Type hType in typeof(IEventHandler <>).GetMakeGenericType(mType))
                {
                    services.TryAddTransient(hType);
                }
            }
            return(services);
        }
Exemple #3
0
 public DefaultRabbitMQPersistentConnection(RabbitMQEventBusConnectionConfiguration configuration, Func <string> connectionAction, ILogger <DefaultRabbitMQPersistentConnection> logger)
 {
     Configuration      = configuration ?? throw new ArgumentNullException(nameof(configuration));
     _connectionAction  = connectionAction ?? throw new ArgumentNullException(nameof(connectionAction));
     _connectionFactory = new ConnectionFactory
     {
         AutomaticRecoveryEnabled = configuration.AutomaticRecoveryEnabled,
         NetworkRecoveryInterval  = configuration.NetworkRecoveryInterval
     };
     _logger = logger ?? throw new ArgumentNullException(nameof(logger));
 }
Exemple #4
0
 public DefaultRabbitMQPersistentConnection(RabbitMQEventBusConnectionConfiguration configuration, IConnectionFactory connectionFactory, ILogger <DefaultRabbitMQPersistentConnection> logger)
 {
     Configuration      = configuration ?? throw new ArgumentNullException(nameof(configuration));
     _connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
     _logger            = logger ?? throw new ArgumentNullException(nameof(logger));
 }