/// <summary>
        /// Uses reflection to add handling rules for every implemented <see cref="IMessageHandler{TContent}"/>
        /// interface.
        /// </summary>
        /// <remarks>
        /// <para>A handling rule will be added for implemented <see cref="IMessageHandler{TContent}"/> interface
        /// of <paramref name="handlerType"/> according to the following convention:</para>
        /// <list type="bullet">
        /// <item>The name pattern will be an exact match of the <see cref="MessageName"/> returned by the
        /// <see cref="IMessageNamingService"/> in the specified <paramref name="configuration"/></item>
        /// <item>The handling function will retrieve an instance of the handler object using the supplied
        /// <paramref name="handlerFactory"/> and invoke the appropriate <see cref="IMessageHandler{TContent}.HandleMessage"/>
        /// method (via reflection)</item>
        /// <item>The designated queue name will be a hash derived from the full names of the <paramref name="handlerType"/>
        /// and the type of message that is handled</item>
        /// </list>
        /// </remarks>
        /// <param name="configuration">The Platibus configuration to which the handling rules will be added</param>
        /// <param name="handlerType">The type of handler returned by the factory method</param>
        /// <param name="handlerFactory">(Optional) A factory method for getting an instance of the handler (may
        /// return a singleton or scoped handler instance).  If no factory method is specified, then the
        /// default constructor will be used.</param>
        /// <param name="queueNameFactory">(Optional) A factory method that will return an appropriate queue
        /// name for each combination of handler type and message type</param>
        /// <param name="queueOptions">(Optional) Options for how queued messages for the handler
        /// should be processed</param>
        public static void AddHandlingRulesForType(this PlatibusConfiguration configuration,
                                                   Type handlerType, Func <object> handlerFactory = null,
                                                   QueueNameFactory queueNameFactory = null, QueueOptions queueOptions = null)
        {
            var diagnosticService  = configuration.DiagnosticService;
            var autoBindInterfaces = handlerType
                                     .GetInterfaces()
                                     .Where(i => i.IsGenericType && typeof(IMessageHandler <>).IsAssignableFrom(i.GetGenericTypeDefinition()));

            if (handlerFactory == null)
            {
                handlerFactory = () => Activator.CreateInstance(handlerType);
            }

            if (queueNameFactory == null)
            {
                queueNameFactory = DeriveQueueName;
            }

            foreach (var autoBindInterface in autoBindInterfaces)
            {
                var messageType   = autoBindInterface.GetGenericArguments().First();
                var messageName   = configuration.MessageNamingService.GetNameForType(messageType);
                var specification = new MessageNamePatternSpecification("^" + Regex.Escape(messageName) + "$");
                var queueName     = queueNameFactory(handlerType, messageType);

                var args   = new[] { messageType, typeof(IMessageContext), typeof(CancellationToken) };
                var method = autoBindInterface.GetMethod("HandleMessage", args);

                if (method == null)
                {
                    continue;
                }

                var handlerActivation = new HandlerActivation(diagnosticService, handlerType, handlerFactory, method);
                var rule = new HandlingRule(specification, handlerActivation, queueName, queueOptions);
                configuration.AddHandlingRule(rule);
            }
        }
        /// <summary>
        /// Uses reflection to add handling rules for every implemented <see cref="IMessageHandler{TContent}"/>
        /// interface.
        /// </summary>
        /// <remarks>
        /// <para>A handling rule will be added for implemented <see cref="IMessageHandler{TContent}"/> interface
        /// of <typeparamref name="THandler"/> according to the following convention:</para>
        /// <list type="bullet">
        /// <item>The name pattern will be an exact match of the <see cref="MessageName"/> returned by the
        /// <see cref="IMessageNamingService"/> in the specified <paramref name="configuration"/></item>
        /// <item>The handling function will retrieve an instance of the handler object using the supplied
        /// <paramref name="handlerFactory"/> and invoke the appropriate <see cref="IMessageHandler{TContent}.HandleMessage"/>
        /// method (via reflection)</item>
        /// <item>The designated queue name will be a hash derived from the full names of the <typeparamref name="THandler"/>
        /// type and the type of message that is handled</item>
        /// </list>
        /// </remarks>
        /// <typeparam name="THandler">The type of message handler</typeparam>
        /// <param name="configuration">The Platibus configuration to which the handling rules will be added</param>
        /// <param name="handlerFactory">(Optional) A factory method for getting an instance of the handler (may
        /// return a singleton or scoped handler instance).  If no factory method is specified, then the
        /// default constructor will be used.</param>
        /// <param name="queueNameFactory">(Optional) A factory method that will return an appropriate queue
        /// name for each combination of handler type and message type</param>
        /// <param name="queueOptions">(Optional) Options for how queued messages for the handler
        /// should be processed</param>
        public static void AddHandlingRules <THandler>(this PlatibusConfiguration configuration,
                                                       Func <THandler> handlerFactory = null, QueueNameFactory queueNameFactory = null,
                                                       QueueOptions queueOptions      = null)
        {
            Func <object> factory = null;

            if (handlerFactory != null)
            {
                factory = () => handlerFactory();
            }
            configuration.AddHandlingRulesForType(typeof(THandler), factory, queueNameFactory, queueOptions);
        }
 /// <summary>
 /// Uses reflection to add handling rules for every implemented <see cref="IMessageHandler{TContent}"/>
 /// interface on the specified <paramref name="handler"/> instance.
 /// </summary>
 /// <remarks>
 /// <para>A handling rule will be added for implemented <see cref="IMessageHandler{TContent}"/> interface
 /// of <typeparamref name="THandler"/> according to the following convention:</para>
 /// <list type="bullet">
 /// <item>The name pattern will be an exact match of the <see cref="MessageName"/> returned by the
 /// <see cref="IMessageNamingService"/> in the specified <paramref name="configuration"/></item>
 /// <item>The handling function will rinvoke the appropriate <see cref="IMessageHandler{TContent}.HandleMessage"/>
 /// method (via reflection) on the specified <paramref name="handler"/> instance</item>
 /// <item>The designated queue name will be a hash derived from the full names of the <typeparamref name="THandler"/>
 /// type and the type of message that is handled</item>
 /// </list>
 /// </remarks>
 /// <typeparam name="THandler">The type of message handler</typeparam>
 /// <param name="configuration">The Platibus configuration to which the handling rules will be added</param>
 /// <param name="handler">The singleton handler instance</param>
 /// <param name="queueNameFactory">(Optional) A factory method that will return an appropriate queue
 /// name for each combination of handler type and message type</param>
 /// <param name="queueOptions">(Optional) Options for how queued messages for the handler
 /// should be processed</param>
 public static void AddHandlingRules <THandler>(this PlatibusConfiguration configuration,
                                                THandler handler, QueueNameFactory queueNameFactory = null,
                                                QueueOptions queueOptions = null)
 {
     configuration.AddHandlingRulesForType(typeof(THandler), () => handler, queueNameFactory, queueOptions);
 }