public async Task Dispatch <TMessage>(TMessage message, IMessageHandlerContext messageHandlerContext) where TMessage : IMessage
        {
            try
            {
                await _messageDispatcher.Dispatch(message, messageHandlerContext).ConfigureAwait(false);

                if (messageHandlerContext is IMessageBrokerContext messageBrokerContext)
                {
                    messageBrokerContext.Container.TryGet <ReplyToRoutingContext>(out var replyContext);
                    await _replyRouter.Route(messageBrokerContext.BrokeredMessage, messageBrokerContext.GetTransactionContext(), replyContext).ConfigureAwait(false);

                    messageBrokerContext.Container.TryGet <RoutingContext>(out var forwardContext);
                    await _forwardMessages.Route(messageBrokerContext.BrokeredMessage, forwardContext?.DestinationPath, messageBrokerContext.GetTransactionContext()).ConfigureAwait(false);
                }
            }
            catch (Exception dispatchFailureException)
            {
                if (messageHandlerContext is IMessageBrokerContext messageBrokerContext)
                {
                    if (!(messageBrokerContext.Container.TryGet <CompensationRoutingContext>(out var compensateContext)))
                    {
                        throw;
                    }

                    var details     = $"{dispatchFailureException.Message} -> {dispatchFailureException.StackTrace}";
                    var description = $"'{typeof(TMessage).Name}' was not received successfully";

                    var newContext = new CompensationRoutingContext(compensateContext?.DestinationPath,
                                                                    details,
                                                                    description,
                                                                    messageBrokerContext?.Container);

                    messageBrokerContext?.Container.Include(newContext);

                    messageBrokerContext.Container.TryGet <CompensationRoutingContext>(out var compensationRoutingContext);
                    await _compensatingRouter.Route(messageBrokerContext.BrokeredMessage, messageBrokerContext.GetTransactionContext(), compensationRoutingContext).ConfigureAwait(false);
                }
                else
                {
                    throw;
                }
            }
        }
Exemple #2
0
 public Task Forward(InboundBrokeredMessage inboundBrokeredMessage, string forwardDestination, TransactionContext transactionContext)
 => _forwarder.Route(inboundBrokeredMessage, forwardDestination, transactionContext);