Beispiel #1
0
        public static IApplicationConfigureContext UseMessageBus(
            this IApplicationConfigureContext context,
            MessageBusClientNames name,
            IConfiguration configuration = null)
        {
            var services = context.Services;

            services.AddTypedMessagePublisher <IApplicationCommandPublisher, DefaultApplicationCommandPublisher>(name);
            services.AddTypedMessagePublisher <IApplicationCommandPublisher, DefaultApplicationCommandPublisher>(name);
            services.AddTypedMessagePublisher <IApplicationNotificationPublisher, DefaultApplicationNotificationPublisher>(name);

            services.AddMessageHandler <DomainExceptionMessage, ApplicationCommandResultProcessor>();
            services.AddMessageHandler <SagaTransactionDomainNotification, ApplicationCommandResultProcessor>();
            services.AddMessageHandler <DomainCommandHandledNotification, ApplicationCommandResultProcessor>();
            services.AddMessageHandler <DomainEventHandledNotification, ApplicationCommandResultProcessor>();

            services.Configure <ApplicationCommandOptions>(_ => { });

            if (configuration == null)
            {
                return(context);
            }

            var applicationOptions = configuration.GetSection("MDA").GetSection("ApplicationOptions");

            services.Configure <ApplicationCommandOptions>(applicationOptions.GetSection("CommandOptions"));

            return(context);
        }
        /// <summary>
        /// 配置入站、出站消息总线。
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="name">消息总线客户端名称</param>
        /// <param name="configuration">配置信息</param>
        /// <returns>上下文</returns>
        public static IDomainConfigureContext UseMessageBus(
            this IDomainConfigureContext context,
            MessageBusClientNames name,
            IConfiguration configuration = null)
        {
            var services = context.Services;

            services.AddTypedMessagePublisher <IDomainEventPublisher, DefaultDomainEventPublisher>(name);
            services.AddTypedMessagePublisher <IDomainNotificationPublisher, DefaultDomainNotificationPublisher>(name);
            services.AddTypedMessagePublisher <IDomainExceptionMessagePublisher, DefaultDomainExceptionMessagePublisher>(name);

            services.Configure <DomainEventOptions>(_ => { });
            services.Configure <DomainNotificationOptions>(_ => { });
            services.Configure <DomainExceptionOptions>(_ => { });

            if (configuration == null)
            {
                return(context);
            }

            var domainOptions = configuration.GetSection("MDA").GetSection("DomainOptions");

            services.Configure <DomainEventOptions>(domainOptions.GetSection("EventOptions"));
            services.Configure <DomainNotificationOptions>(domainOptions.GetSection("NotificationOptions"));
            services.Configure <DomainExceptionOptions>(domainOptions.GetSection("ExceptionOptions"));

            return(context);
        }
        public IMessagePublisher CreateMessagePublisher(MessageBusClientNames name)
        {
            if (!_activeMessageQueueService.TryGetValue(name, out var messageQueueService))
            {
                var queueService = _serviceProvider.GetServices <IMessageQueueService>()
                                   .SingleOrDefault(it => it.Name == name);

                messageQueueService = _activeMessageQueueService[name] = new Lazy <IMessageQueueService>(() => queueService);
            }

            if (!_activeAsyncMessageQueueService.TryGetValue(name, out var asyncMessageQueueService))
            {
                var asyncQueueService = _serviceProvider.GetServices <IAsyncMessageQueueService>()
                                        .SingleOrDefault(it => it.Name == name);

                asyncMessageQueueService = _activeAsyncMessageQueueService[name] = new Lazy <IAsyncMessageQueueService>(() => asyncQueueService);
            }

            return(new DefaultMessagePublisher(messageQueueService?.Value, asyncMessageQueueService?.Value));
        }
        public static IServiceCollection AddTypedMessagePublisher <TMessagePublisher, TMessagePublisherImpl>(this IServiceCollection services, MessageBusClientNames name)
            where TMessagePublisher : class
            where TMessagePublisherImpl : TMessagePublisher
        {
            services.AddOptions();

            services.TryAddSingleton <IMessagePublisherFactory, DefaultMessagePublisherFactory>();

            services.TryAdd(ServiceDescriptor.Singleton(typeof(ITypedMessagePublisherFactory <>), typeof(DefaultTypedMessagePublisherFactory <>)));

            services.AddTransient <TMessagePublisher>(provider =>
            {
                var messagePublisherFactory = provider.GetRequiredService <IMessagePublisherFactory>();
                var messagePublisher        = messagePublisherFactory.CreateMessagePublisher(name);

                var typeMessagePublisherFactory = provider.GetRequiredService <ITypedMessagePublisherFactory <TMessagePublisherImpl> >();

                return(typeMessagePublisherFactory.CreateMessagePublisher(messagePublisher));
            });

            return(services);
        }