/// <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);
        }
Пример #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);
        }