Beispiel #1
0
 private static void CreateErrorContextFromHeaders(MessageBrokerContext messageContext, InboundBrokeredMessage inboundMessage)
 {
     if (inboundMessage.IsError)
     {
         inboundMessage.ApplicationProperties.TryGetValue(ApplicationProperties.FailureDetails, out var reason);
         inboundMessage.ApplicationProperties.TryGetValue(ApplicationProperties.FailureDescription, out var description);
         var errorContext = new ErrorContext((string)reason, (string)description);
         messageContext.SetFailure(errorContext);
     }
 }
Beispiel #2
0
        async Task ReceiveInboundBrokeredMessage(MessageBrokerContext messageContext,
                                                 TransactionContext transactionContext)
        {
            try
            {
                if (messageContext is null)
                {
                    throw new ArgumentNullException(nameof(messageContext), $"A {typeof(MessageBrokerContext).Name} was not created by the messaging infrastructure.");
                }

                var inboundMessage = messageContext.BrokeredMessage;

                messageContext.ExternalDispatcher = _brokeredMessageDispatcher;

                CreateErrorContextFromHeaders(messageContext, inboundMessage);
                CreateSagaContextFromHeaders(messageContext);
                CreateReplyContextFromHeaders(messageContext, inboundMessage);
                CreateNextDestinationContextFromHeaders(messageContext);
                CreateCompensationContextFromHeaders(messageContext, inboundMessage);

                inboundMessage.WithRouteToSelfPath(this.DestinationPath);
                inboundMessage.UpdateVia(Description);

                if (transactionContext is null)
                {
                    transactionContext = new TransactionContext(MessageReceiverPath, inboundMessage.TransactionMode);
                }

                messageContext.Container.Include(transactionContext);

                var brokeredMessagePayload = inboundMessage.GetMessageFromBody <TMessage>();

                using var scope = _serviceFactory.CreateScope();
                var dispatcher = scope.ServiceProvider.GetRequiredService <IMessageDispatcher>();
                await dispatcher.Dispatch(brokeredMessagePayload, messageContext).ConfigureAwait(false);

                inboundMessage.SuccessfullyReceived = true;
            }
            catch (CompensationRoutingException cre)
            {
                var errorContext = new ErrorContext(
                    $"Routing compensation message of type '{typeof(TMessage).Name}' to '{cre.RoutingContext.DestinationPath?.ToString()}' failed",
                    $"Compensation reason: '{cre.RoutingContext?.ToString()}'\n" +
                    $"Routing failure reason: '{cre.InnerException?.Message}'");

                messageContext.SetFailure(errorContext);

                throw new CriticalBrokeredMessageReceiverException(errorContext, cre);
            }
            catch (Exception e)
            {
                ErrorContext errorContext;

                if (messageContext is null)
                {
                    errorContext = new ErrorContext(
                        $"A brokered message was received with no {typeof(MessageBrokerContext).Name}",
                        $"{e.Message}");

                    throw new CriticalBrokeredMessageReceiverException(errorContext, e);
                }

                errorContext = new ErrorContext(
                    $"An error was encountered receiving message '{typeof(TMessage).Name}'",
                    $"{e.Message}");

                messageContext.SetFailure(errorContext);

                throw new CriticalBrokeredMessageReceiverException(errorContext, e);
            }
        }