/// <summary>
        /// Configures event callbacks
        /// </summary>
        public static RebusConfigurer Events(this RebusConfigurer configurer, Action <EventConfigurer> configureEvents)
        {
            if (configurer == null)
            {
                throw new ArgumentNullException(nameof(configurer));
            }
            if (configureEvents == null)
            {
                throw new ArgumentNullException(nameof(configureEvents));
            }

            configurer.Options(o =>
            {
                if (!o.Has <EventHooks>())
                {
                    o.Register(c => new EventHooks());

                    o.Decorate <IPipeline>(c =>
                    {
                        var pipeline   = c.Get <IPipeline>();
                        var eventHooks = c.Get <EventHooks>();

                        var step = new MessageSentStep(eventHooks.BeforeMessageSent, eventHooks.AfterMessageSent, c.Get <IBus>);

                        return(new PipelineStepConcatenator(pipeline)
                               .OnSend(step, PipelineAbsolutePosition.Front));
                    });

                    o.Decorate <IPipeline>(c =>
                    {
                        var pipeline   = c.Get <IPipeline>();
                        var eventHooks = c.Get <EventHooks>();

                        var receiveStep = new MessageReceivedStep(eventHooks.BeforeMessageHandled, eventHooks.AfterMessageHandled, c.Get <IBus>);

                        return(new PipelineStepInjector(pipeline)
                               .OnReceive(receiveStep, PipelineRelativePosition.Before, typeof(DispatchIncomingMessageStep)));
                    });
                }

                o.Decorate(c =>
                {
                    var eventConfigurerInstance = new EventConfigurer();
                    configureEvents(eventConfigurerInstance);

                    var eventHooks = c.Get <EventHooks>();
                    eventConfigurerInstance.CopyTo(eventHooks);

                    return(eventHooks);
                });
            });

            return(configurer);
        }
Пример #2
0
        /// <summary>
        /// Starts the bus with 0 workers, thus creating a fully functional bus, only without starting message processing.
        /// The returned <see cref="IBusStarter"/> can then be used to start the bus by calling <see cref="IBusStarter.Start"/>
        /// on it.
        /// </summary>
        public static IBusStarter Create(this RebusConfigurer configurer)
        {
            var desiredNumberOfWorkersWhenStarted = 0;

            var bus = configurer
                      .Options(o =>
            {
                // modify Options by setting number of workers to 0
                o.Decorate(c =>
                {
                    var options = c.Get <Options>();

                    desiredNumberOfWorkersWhenStarted = options.NumberOfWorkers;

                    // delay bus start by doing this
                    options.NumberOfWorkers = 0;

                    return(options);
                });
            })
                      .Start();

            return(new BusStarter(bus, desiredNumberOfWorkersWhenStarted));
        }