Exemplo n.º 1
0
        /// <summary>
        /// Resolve a message handler delegate for the message type from multiple sources.
        /// This will try resolving a message handler delegate from all sources until a handler
        /// who is either not null or not equal to <see cref="Xer.Delegator.NullMessageHandlerDelegate.Instance"/> is found.
        /// </summary>
        /// <remarks>
        /// If no handler is found, an instance of <see cref="Xer.Delegator.NullMessageHandlerDelegate.Instance"/> is returned.
        /// Any exceptions thrown by the internal resolvers will be propagated.
        /// </remarks>
        /// <param name="messageType">Type of message.</param>
        /// <returns>Message handler delegate.</returns>
        public MessageHandlerDelegate ResolveMessageHandler(Type messageType)
        {
            try
            {
                // Resolvers can either be a list of SingleMessageHandlerResolver or MultiMessageHandlerResolver.
                foreach (IMessageHandlerResolver resolver in _resolvers)
                {
                    MessageHandlerDelegate messageHandlerDelegate = resolver.ResolveMessageHandler(messageType);

                    if (messageHandlerDelegate != null &&
                        messageHandlerDelegate != NullMessageHandlerDelegate.Instance)
                    {
                        return(messageHandlerDelegate);
                    }
                }

                // Return null handler that does nothing.
                return(NullMessageHandlerDelegate.Instance);
            }
            catch (NoMessageHandlerResolvedException)
            {
                // If a source has thrown this exception, just rethrow.
                throw;
            }
            catch (Exception ex)
            {
                throw NoMessageHandlerResolvedException.WithMessageType(messageType, ex);
            }
        }
        /// <summary>
        /// Send message to a delegate with one/many handlers.
        /// </summary>
        /// <typeparam name="TMessage">Type of message.</typeparam>
        /// <param name="message">Message to send.</param>
        /// <param name="cancellationToken">Optional cancellation token to be passed to handlers.</param>
        /// <returns>Asynchronous task which can be awaited for completion.</returns>
        public Task SendAsync <TMessage>(TMessage message, CancellationToken cancellationToken = default(CancellationToken)) where TMessage : class
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            Type messageType = message.GetType();

            MessageHandlerDelegate messageHandler = _messageHandlerResolver.ResolveMessageHandler(messageType);

            if (messageHandler == null)
            {
                throw NoMessageHandlerResolvedException.WithMessageType(messageType);
            }

            return(messageHandler.Invoke(message, cancellationToken));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Resolve a message handler delegate for the message type and throws an exception if no handler is found.
        /// </summary>
        /// <param name="messageType">Type of message.</param>
        /// <returns>Message handler delegate which invokes all registered handlers.</returns>
        /// <exception cref="Xer.Delegator.Exceptions.NoMessageHandlerResolvedException">Throws when no message handler delegate is found.</exception>
        public MessageHandlerDelegate ResolveMessageHandler(Type messageType)
        {
            try
            {
                MessageHandlerDelegate messageHandler = _messageHandlerResolver.ResolveMessageHandler(messageType);

                // Throw if resolved handler is a null message handler.
                if (messageHandler == null || messageHandler == NullMessageHandlerDelegate.Instance)
                {
                    throw NoMessageHandlerResolvedException.WithMessageType(messageType);
                }

                return(messageHandler);
            }
            catch (Exception ex)
            {
                throw NoMessageHandlerResolvedException.WithMessageType(messageType, ex);
            }
        }