public static EventBusOptionsBuilder UseRabbitMQ(this EventBusOptionsBuilder optionsBuilder, ILinFxBuilder builder, Action <EventBusRabbitMqOptions> optionsAction)
        {
            Check.NotNull(optionsAction, nameof(optionsAction));

            var options = new EventBusRabbitMqOptions();

            optionsAction?.Invoke(options);

            builder.Services.AddSingleton((Func <IServiceProvider, IRabbitMQPersistentConnection>)(sp =>
            {
                var logger  = sp.GetRequiredService <ILogger <DefaultRabbitMQPersistentConnection> >();
                var factory = new ConnectionFactory
                {
                    UserName = options.UserName,
                    Password = options.Password,
                    HostName = options.Host,
                };
                return(new DefaultRabbitMQPersistentConnection((IConnectionFactory)factory, (ILogger <DefaultRabbitMQPersistentConnection>)logger));
            }));

            builder.Services.AddSingleton <IEventBus, EventBusRabbitMQ>(sp =>
            {
                var logger = sp.GetRequiredService <ILogger <EventBusRabbitMQ> >();
                var rabbitMQPersistentConnection = sp.GetRequiredService <IRabbitMQPersistentConnection>();
                var iServiceScopeFactory         = sp.GetRequiredService <IServiceScopeFactory>();
                var eventBusSubcriptionsManager  = sp.GetRequiredService <IEventBusSubscriptionsManager>();
                return(new EventBusRabbitMQ(logger, rabbitMQPersistentConnection, eventBusSubcriptionsManager, iServiceScopeFactory, optionsBuilder.Options));
            });

            return(optionsBuilder);
        }
        public static EventBusOptionsBuilder UseRabbitMQ(this EventBusOptionsBuilder optionsBuilder, ILinFxBuilder fx, Action <RabbitMQOptions> optionsAction)
        {
            Check.NotNull(optionsAction, nameof(optionsAction));

            var options = new RabbitMQOptions();

            optionsAction?.Invoke(options);

            fx.AddRabbitMQ(x =>
            {
                x.Host     = options.Host;
                x.UserName = options.UserName;
                x.Password = options.Password;
            });

            fx.Services.AddSingleton <IEventBus, EventBusRabbitMQ>(sp =>
            {
                var logger = sp.GetRequiredService <ILogger <EventBusRabbitMQ> >();
                var rabbitMQPersistentConnection = sp.GetRequiredService <IRabbitMQPersistentConnection>();
                var iServiceScopeFactory         = sp.GetRequiredService <IServiceScopeFactory>();
                var eventBusSubcriptionsManager  = sp.GetRequiredService <IEventBusSubscriptionsManager>();
                return(new EventBusRabbitMQ(logger, rabbitMQPersistentConnection, eventBusSubcriptionsManager, iServiceScopeFactory, optionsBuilder.Options));
            });

            return(optionsBuilder);
        }
        /// <summary>
        /// 添加 EventBus 模块注册
        /// </summary>
        /// <param name="services">服务集合对象</param>
        /// <param name="configureOptionsBuilder">事件总线配置选项构建器委托</param>
        /// <returns>服务集合实例</returns>
        public static IServiceCollection AddEventBus(this IServiceCollection services, Action <EventBusOptionsBuilder> configureOptionsBuilder)
        {
            // 创建初始事件总线配置选项构建器
            var eventBusOptionsBuilder = new EventBusOptionsBuilder();

            configureOptionsBuilder.Invoke(eventBusOptionsBuilder);

            return(services.AddEventBus(eventBusOptionsBuilder));
        }
Exemple #4
0
        //public static ILinFxBuilder AddEventBus(
        //    [NotNull] this ILinFxBuilder builder,
        //    [NotNull] Action<EventBusOptions> optionsAction)
        //{
        //    Check.NotNull(builder, nameof(builder));
        //    Check.NotNull(optionsAction, nameof(optionsAction));

        //    var optionsBuilder = new EventBusOptions();
        //    optionsAction?.Invoke(optionsBuilder);

        //    return builder;
        //}

        public static LinFxBuilder AddEventBus(this LinFxBuilder fx, Action <EventBusOptionsBuilder> optionsAction)
        {
            var options        = new EventBusOptions();
            var optionsBuilder = new EventBusOptionsBuilder(fx, options);

            optionsAction?.Invoke(optionsBuilder);

            fx.Services.AddSingleton <IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();

            return(fx);
        }
Exemple #5
0
        public static EventBusOptionsBuilder UseRabbitMQ(
            [NotNull] this EventBusOptionsBuilder optionsBuilder,
            [NotNull] Action <RabbitMqOptions> optionsAction)
        {
            Check.NotNull(optionsBuilder, nameof(optionsBuilder));
            Check.NotNull(optionsAction, nameof(optionsAction));

            optionsBuilder.Fx.AddRabbitMQ(optionsAction);
            optionsBuilder.Fx.Services.AddSingleton <IEventBus, RabbitMqDistributedEventBus>();

            return(optionsBuilder);
        }
Exemple #6
0
        //public static ILinFxBuilder AddEventBus(
        //    [NotNull] this ILinFxBuilder builder,
        //    [NotNull] Action<EventBusOptions> optionsAction)
        //{
        //    Check.NotNull(builder, nameof(builder));
        //    Check.NotNull(optionsAction, nameof(optionsAction));

        //    var optionsBuilder = new EventBusOptions();
        //    optionsAction?.Invoke(optionsBuilder);

        //    return builder;
        //}

        public static ILinFxBuilder AddEventBus(
            [NotNull] this ILinFxBuilder builder,
            [NotNull] Action <EventBusOptionsBuilder> optionsAction)
        {
            var options        = new EventBusOptions();
            var optionsBuilder = new EventBusOptionsBuilder(builder, options);

            optionsAction?.Invoke(optionsBuilder);

            builder.Services.AddSingleton <IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();

            return(builder);
        }
        public static EventBusOptionsBuilder UseRabbitMQ(
            [NotNull] this EventBusOptionsBuilder optionsBuilder,
            [NotNull] Action <RabbitMqEventBusOptions> optionsAction)
        {
            Check.NotNull(optionsBuilder, nameof(optionsBuilder));
            Check.NotNull(optionsAction, nameof(optionsAction));

            optionsBuilder.Fx.Services.Configure(optionsAction);

            var options = new RabbitMqEventBusOptions();

            optionsAction?.Invoke(options);
            optionsBuilder.Fx.AddRabbitMq(x =>
            {
                x.Host     = options.Host;
                x.UserName = options.UserName;
                x.Password = options.Password;
            });
            optionsBuilder.Fx.Services.AddSingleton <IEventBus, RabbitMqDistributedEventBus>();
            return(optionsBuilder);
        }
        /// <summary>
        /// 注册内部服务
        /// </summary>
        /// <param name="services">服务集合对象</param>
        /// <param name="eventBusOptions">事件总线配置选项</param>
        /// <returns>服务集合实例</returns>
        private static IServiceCollection AddInternalService(this IServiceCollection services, EventBusOptionsBuilder eventBusOptions)
        {
            // 注册后台任务队列接口/实例为单例,采用工厂方式创建
            services.AddSingleton <IEventSourceStorer>(_ =>
            {
                // 创建默认内存通道事件源对象
                return(new ChannelEventSourceStorer(eventBusOptions.ChannelCapacity));
            });

            // 注册默认内存通道事件发布者
            services.AddSingleton <IEventPublisher, ChannelEventPublisher>();

            return(services);
        }
        /// <summary>
        /// 添加 EventBus 模块注册
        /// </summary>
        /// <param name="services">服务集合对象</param>
        /// <param name="eventBusOptionsBuilder">事件总线配置选项构建器</param>
        /// <returns>服务集合实例</returns>
        public static IServiceCollection AddEventBus(this IServiceCollection services, EventBusOptionsBuilder eventBusOptionsBuilder = default)
        {
            // 初始化事件总线配置项
            eventBusOptionsBuilder ??= new EventBusOptionsBuilder();

            // 注册内部服务
            services.AddInternalService(eventBusOptionsBuilder);

            // 通过工厂模式创建
            services.AddHostedService(serviceProvider =>
            {
                // 创建事件总线后台服务对象
                var eventBusHostedService = ActivatorUtilities.CreateInstance <EventBusHostedService>(serviceProvider);

                // 订阅未察觉任务异常事件
                var unobservedTaskExceptionHandler = eventBusOptionsBuilder.UnobservedTaskExceptionHandler;
                if (unobservedTaskExceptionHandler != default)
                {
                    eventBusHostedService.UnobservedTaskException += unobservedTaskExceptionHandler;
                }

                return(eventBusHostedService);
            });

            // 构建事件总线服务
            eventBusOptionsBuilder.Build(services);

            return(services);
        }