/// <summary> /// Handle message that is received through <see cref="IMessageSource{TMessage}"/>. /// </summary> /// <param name="receivedMessage">Received message.</param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>Asynchronous task that can be awaited for completion.</returns> private async Task OnMessageReceived(MessageContainer <TMessage> receivedMessage) { // Do not process if null or empty. if (receivedMessage != null && !receivedMessage.IsEmpty) { try { // Process message. await ProcessMessageAsync(receivedMessage, _cancellationToken).ConfigureAwait(false); } catch (Exception ex) { PublishException(ex); } } }
/// <summary> /// Forward message to a message processor. /// </summary> /// <typeparam name="TMessage">Type of message to forward.</typeparam> /// <param name="recipientMessageProcessorName">Name of message processor to forward to.</param> /// <param name="messageToForward">Message to forward.</param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>Task which can be awaited for completion.</returns> public Task ForwardMessageAsync <TMessage>(string recipientMessageProcessorName, MessageContainer <TMessage> messageToForward, CancellationToken cancellationToken = default(CancellationToken)) where TMessage : class { return(_messageProcessorHost.ForwardToMessageProcessorAsync(recipientMessageProcessorName, messageToForward, cancellationToken)); }
/// <summary> /// Process message asynchronously. /// </summary> /// <remarks> /// This method is not awaited by the message handler. /// It is recommended to not let any exceptions exit this method because /// any exceptions that exits this method will not be handled. /// </remarks> /// <param name="receivedMessage">Message received.</param> /// <param name="cancellationToken">Cancellation token. This is cancelled when hosted command handler is stopped.</param> /// <returns>Task which can be awaited for completion.</returns> protected abstract Task ProcessMessageAsync(MessageContainer <TMessage> receivedMessage, CancellationToken cancellationToken);
/// <summary> /// Receive a message in-process and schedule for processing. /// </summary> /// <param name="message">Message to receive.</param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>Task which can be awaited for completion.</returns> public Task ReceiveMessageAsync(MessageContainer <TMessage> message, CancellationToken cancellationToken = default(CancellationToken)) { return(MessageSource.ReceiveAsync(message, cancellationToken)); }