public Task SendToOutbox(OutboundBrokeredMessage outboundBrokeredMessage, TransactionContext transactionContext, CancellationToken cancellationToken = default)
        {
            Guid transactionId = Guid.NewGuid();

            if (transactionContext != null)
            {
                transactionContext.Container.TryGet <IPersistanceTransaction>(out var transaction);
                transactionId = transaction?.TransactionId ?? transactionId;
            }

            var outboxMessage = new OutboxMessage
            {
                MessageId                = outboundBrokeredMessage.MessageId,
                MessageContext           = JsonConvert.SerializeObject(outboundBrokeredMessage.MessageContext),
                Destination              = outboundBrokeredMessage.Destination,
                MessageBody              = outboundBrokeredMessage.Stringify(),
                MessageContentType       = outboundBrokeredMessage.ContentType,
                SentToOutboxAtUtc        = DateTime.UtcNow,
                ProcessedFromOutboxAtUtc = null,
                BatchId = transactionId
            };

            _logger.LogTrace($"Outbox message created for message with id: '{outboxMessage.MessageId}'");

            if (!_outbox.TryAdd(outboxMessage.MessageId, outboxMessage))
            {
                var error = $"Unable to add brokered message with id: '{outboxMessage.MessageId}' to the in memory outbox.";
                _logger.LogError(error);
                throw new InvalidOperationException(error);
            }

            _logger.LogTrace($"Outbox message with id: '{outboxMessage.MessageId}' added to the in memory outbox.");

            return(Task.CompletedTask);
        }
        private async Task SendToOutbox(DbSet <OutboxMessage> outbox, OutboundBrokeredMessage outboundBrokeredMessage, TransactionContext transactionContext)
        {
            var  currentTransaction = transactionContext?.Container.GetOrAdd <IPersistanceTransaction>();
            Guid transactionId      = currentTransaction?.TransactionId ?? Guid.Empty;

            //TODO: trim the OutboxMessage object down. likely dont need Body, etc.
            var outboxMessage = new OutboxMessage
            {
                MessageId = outboundBrokeredMessage.MessageId,
                StringifiedApplicationProperties = JsonConvert.SerializeObject(outboundBrokeredMessage.ApplicationProperties),
                Body                     = outboundBrokeredMessage.Body,
                Destination              = outboundBrokeredMessage.Destination,
                StringifiedMessage       = outboundBrokeredMessage.Stringify(),
                SentToOutboxAtUtc        = DateTime.UtcNow,
                ProcessedFromOutboxAtUtc = null,
                BatchId                  = transactionId
            };

            _logger.LogTrace($"Outbox message created for message with id: '{outboxMessage.MessageId}'");

            await outbox.AddAsync(outboxMessage);
        }
示例#3
0
        private async Task SendToOutboxImpl(DbSet <OutboxMessage> outbox, OutboundBrokeredMessage outboundBrokeredMessage, TransactionContext transactionContext, CancellationToken cancellationToken = default)
        {
            var  currentTransaction = transactionContext?.Container.GetOrDefault <IPersistanceTransaction>();
            Guid transactionId      = currentTransaction?.TransactionId ?? Guid.Empty;

            var outboxMessage = new OutboxMessage
            {
                MessageId                = outboundBrokeredMessage.MessageId,
                MessageContext           = JsonConvert.SerializeObject(outboundBrokeredMessage.MessageContext),
                Destination              = outboundBrokeredMessage.Destination,
                MessageBody              = outboundBrokeredMessage.Stringify(),
                MessageContentType       = outboundBrokeredMessage.ContentType,
                SentToOutboxAtUtc        = DateTime.UtcNow,
                ProcessedFromOutboxAtUtc = null,
                BatchId = transactionId
            };

            _logger.LogTrace($"Outbox message created. MessageId: '{outboxMessage.MessageId}', BatchId: {outboxMessage.BatchId}");

            await outbox.AddAsync(outboxMessage, cancellationToken).ConfigureAwait(false);

            _logger.LogTrace($"Outbox message added to outbox. MessageId: '{outboxMessage.MessageId}', BatchId: {outboxMessage.BatchId}");
        }