public static bool TryGetRoutingSlip(this IMessageBrokerContext mbc, out RoutingSlip routingSlip) { try { if (mbc.BrokeredMessage != null) { if (mbc.BrokeredMessage.MessageContext != null) { if (mbc.BrokeredMessage.MessageContext.TryGetValue(MessageContext.RoutingSlip, out var rs)) { RoutingSlip theSlip = JsonConvert.DeserializeObject <RoutingSlip>((string)rs); routingSlip = theSlip; return(true); } } } if (mbc.Container.TryGet <RoutingSlip>(out var slipFromContainer)) { routingSlip = slipFromContainer; return(true); } routingSlip = null; return(false); } catch { routingSlip = null; return(false); } }
public async Task ReceiveViaInbox <TMessage>(TMessage message, IMessageBrokerContext messageBrokerContext, Func <Task> messageReceiver) { var id = messageBrokerContext.BrokeredMessage.MessageId; if (string.IsNullOrWhiteSpace(id)) { throw new ArgumentException("A brokered message must have a message id to be persisted in the inbox.", nameof(id)); } if (_inbox.ContainsKey(id)) { _logger.LogTrace($"Brokered message of type '{typeof(TMessage).Name}' with id: '{id}' was already received."); return; } await messageReceiver().ConfigureAwait(false); if (!_inbox.TryAdd(id, true)) { var error = $"Unable to retrieve brokered message of type '{typeof(TMessage).Name}' with id: '{id}' from the in memory inbox."; _logger.LogError(error); throw new InvalidOperationException(error); } _logger.LogTrace($"Brokered message of type '{typeof(TMessage).Name}' with id: '{id}' was successfully received and added to inbox."); }
public async Task ReceiveViaInbox <TMessage>(TMessage message, IMessageBrokerContext messageBrokerContext, Func <Task> handler) { var messageId = messageBrokerContext.BrokeredMessage.MessageId; if (string.IsNullOrWhiteSpace(messageId)) { throw new ArgumentException("Message id to be processed cannot be empty.", nameof(messageId)); } var inbox = _context.Set <InboxMessage>(); if (await inbox.AnyAsync(m => m.MessageId == messageId)) { return; } //TODO: this whole class needs to be cleaned up and needs logging. try { await handler(); var inboxMessage = new InboxMessage() { MessageId = messageId, ReceivedByInboxAtUtc = DateTime.UtcNow }; await inbox.AddAsync(inboxMessage); } catch (Exception ex) { throw; } }
/// <summary> /// Receives a message and verifies if it's been handled previously by checking the inbox /// </summary> /// <typeparam name="TMessage">The type of message being received</typeparam> /// <param name="message">The message being received</param> /// <param name="messageBrokerContext">The brokered message context received with the message</param> /// <param name="handler">The message handler to be executed if the message is not found within the inbox</param> /// <returns>An awaitable task</returns> public async Task ReceiveViaInbox <TMessage>(TMessage message, IMessageBrokerContext messageBrokerContext, Func <Task> handler) { var messageId = messageBrokerContext?.BrokeredMessage?.MessageId; if (string.IsNullOrWhiteSpace(messageId)) { _logger.LogDebug("Unable to receve message using inbox because message id is null or whitespace. Executing handler."); await handler(); return; } _logger.LogTrace($"Checking inbox for brokered message with message id '{messageId}'."); var inbox = _context.Set <InboxMessage>(); if (await inbox.AnyAsync(m => m.MessageId == messageId)) { _logger.LogTrace($"Message with id '{messageId}' found in inbox. Message will not be handled."); return; } try { _logger.LogDebug("Executing message handler from inbox"); await handler(); var inboxMessage = new InboxMessage() { MessageId = messageId, ReceivedByInboxAtUtc = DateTime.UtcNow }; _logger.LogDebug("Message handler executed successfully from inbox"); _logger.LogTrace($"Adding inbox message with id '{inboxMessage.MessageId}' and date received '{inboxMessage.ReceivedByInboxAtUtc}'."); await inbox.AddAsync(inboxMessage); _logger.LogTrace($"Message with id '{messageId}' added to inbox."); } catch (Exception ex) { _logger.LogTrace($"Error adding message with id '{messageId}' to inbox: {ex.StackTrace}"); throw; } }
public AzureServiceBusContextDispatcher(IMessageBrokerContext context) { _context = context; _context?.BrokeredMessage?.UseMessagingInfrastructure(it => it.AzureServiceBus()); }
public Task Forward(string forwardDestination, IMessageBrokerContext context) => Forward(context.BrokeredMessage, forwardDestination, context?.GetTransactionContext());
public async Task InvokeStep <TMessage>(Func <TMessage, IMessageHandlerContext, Task> sagaStepHandler, ISagaMessage message, IMessageBrokerContext context) where TMessage : IMessage { SagaContext saga = await _sagaInitializer.Initialize(message, context).ConfigureAwait(false); if (!(sagaStepHandler is null)) { await sagaStepHandler((TMessage)message, context).ConfigureAwait(false); } saga.InProgress(); context.BrokeredMessage.WithSagaStatus(saga.Status.Status); await _sagaPersister.Persist(saga, message, context).ConfigureAwait(false); }
public async Task Complete <TMessage>(Func <TMessage, IMessageHandlerContext, Task> sagaStepHandler, ISagaMessage message, IMessageBrokerContext context) where TMessage : IMessage { SagaContext saga = await _sagaInitializer.Initialize(message, context).ConfigureAwait(false); if (!(sagaStepHandler is null)) { await sagaStepHandler((TMessage)message, context).ConfigureAwait(false); } if (context.Container.TryGet <ErrorContext>(out var errorContext)) { saga.Fail(errorContext.ToString()); } else if (context.Container.TryGet <CompensationRoutingContext>(out var compensateContext)) { saga.Fail(compensateContext.ToString()); } else { saga.Success(); } context.BrokeredMessage.WithSagaStatus(saga.Status.Status); await _sagaPersister.Persist(saga, message, context).ConfigureAwait(false); }