Esempio n. 1
0
        protected IBusInstance CreateBus <T, TConfigurator>(T configurator, IBusRegistrationContext context,
                                                            Action <IBusRegistrationContext, TConfigurator> configure, IEnumerable <IBusInstanceSpecification> specifications)
            where T : TConfigurator, IBusFactory
            where TConfigurator : IBusFactoryConfigurator
        {
            var loggerFactory = context.GetService <ILoggerFactory>();

            if (loggerFactory != null)
            {
                LogContext.ConfigureCurrentLogContext(loggerFactory);
            }

            context.UseHealthCheck(configurator);

            var riders = new RiderConnectable();

            configurator.ConnectBusObserver(new RiderBusObserver(riders));

            configure?.Invoke(context, configurator);

            specifications ??= Enumerable.Empty <IBusInstanceSpecification>();

            var busControl = configurator.Build(specifications);

            var instance = new TransportBusInstance(busControl, _hostConfiguration, riders);

            foreach (var specification in specifications)
            {
                specification.Configure(instance);
            }

            return(instance);
        }
        protected IBusInstance CreateBus <T, TConfigurator>(T configurator, IBusRegistrationContext context,
                                                            Action <IBusRegistrationContext, TConfigurator> configure, IEnumerable <IBusInstanceSpecification> specifications)
            where T : TConfigurator, IBusFactory
            where TConfigurator : IBusFactoryConfigurator
        {
            var loggerFactory = context.GetService <ILoggerFactory>();

            if (loggerFactory != null)
            {
                LogContext.ConfigureCurrentLogContext(loggerFactory);
            }
            else if (LogContext.Current == null)
            {
                LogContext.ConfigureCurrentLogContext();
            }

            _hostConfiguration.LogContext = LogContext.Current;

            context.UseHealthCheck(configurator);

            configure?.Invoke(context, configurator);

            specifications ??= Enumerable.Empty <IBusInstanceSpecification>();

            IEnumerable <IBusInstanceSpecification> busInstanceSpecifications = specifications as IBusInstanceSpecification[] ?? specifications.ToArray();

            IEnumerable <ValidationResult> validationResult = configurator.Validate()
                                                              .Concat(busInstanceSpecifications.SelectMany(x => x.Validate()));

            var result = BusConfigurationResult.CompileResults(validationResult);

            try
            {
                var busReceiveEndpointConfiguration = configurator.CreateBusEndpointConfiguration(x => x.ConfigureConsumeTopology = false);

                var host = _hostConfiguration.Build() as IHost <TEndpointConfigurator>;

                var bus = new MassTransitBus(host, _hostConfiguration.BusConfiguration.BusObservers, busReceiveEndpointConfiguration);

                TaskUtil.Await(() => _hostConfiguration.BusConfiguration.BusObservers.PostCreate(bus));

                var instance = new TransportBusInstance <TEndpointConfigurator>(bus, host, _hostConfiguration, context);

                foreach (var specification in busInstanceSpecifications)
                {
                    specification.Configure(instance);
                }

                return(instance);
            }
            catch (Exception ex)
            {
                TaskUtil.Await(() => _hostConfiguration.BusConfiguration.BusObservers.CreateFaulted(ex));

                throw new ConfigurationException(result, "An exception occurred during bus creation", ex);
            }
        }