/// <summary>
        ///     Adds the <see cref="IIntegrationSpy" /> and its support services to the
        ///     <see cref="Microsoft.Extensions.DependencyInjection.IServiceCollection" />.
        /// </summary>
        /// <param name="silverbackBuilder">
        ///     The <see cref="ISilverbackBuilder" /> that references the <see cref="IServiceCollection" /> to add
        ///     the services to.
        /// </param>
        /// <param name="attachSubscriber">
        ///     Specifies whether a generic subscriber (<see cref="InboundSpySubscriber" /> must be used to monitor the
        ///     inbound messages instead of a behavior (<see cref="InboundSpyBrokerBehavior" />).
        /// </param>
        /// <returns>
        ///     The <see cref="ISilverbackBuilder" /> so that additional calls can be chained.
        /// </returns>
        public static ISilverbackBuilder AddIntegrationSpy(
            this ISilverbackBuilder silverbackBuilder,
            bool attachSubscriber = false)
        {
            Check.NotNull(silverbackBuilder, nameof(silverbackBuilder));

            silverbackBuilder
            .AddSingletonBrokerBehavior <OutboundSpyBrokerBehavior>()
            .AddSingletonBrokerBehavior <RawOutboundSpyBrokerBehavior>()
            .AddSingletonBrokerBehavior <RawInboundSpyBrokerBehavior>();

            if (attachSubscriber)
            {
                silverbackBuilder.AddSingletonSubscriber <InboundSpySubscriber>();
            }
            else
            {
                silverbackBuilder.AddSingletonBrokerBehavior <InboundSpyBrokerBehavior>();
            }

            silverbackBuilder.Services
            .AddSingleton <IIntegrationSpy>(
                serviceProvider => serviceProvider.GetRequiredService <IntegrationSpy>())
            .AddSingleton <IntegrationSpy>();

            return(silverbackBuilder);
        }
        /// <summary>
        /// Allows the subscribers to receive an <see cref="IObservable{T}"/> as parameter.
        /// It also registers the <see cref="IMessageObservable{TMessage}"/> that can be used to
        /// process the entire messages stream using Rx.NET.
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static ISilverbackBuilder AsObservable(this ISilverbackBuilder builder)
        {
            builder.Services
            .AddSingleton <IArgumentResolver, ObservableMessageArgumentResolver>()
            .AddScoped <IReturnValueHandler, ObservableMessagesReturnValueHandler>()
            .AddSingleton <MessageObservable, MessageObservable>()
            .AddSingleton(typeof(IMessageObservable <>), typeof(MessageObservable <>));

            builder
            .AddSingletonSubscriber <MessageObservable>();

            return(builder);
        }