Search code examples
rabbitmqmasstransit

Outbox Publishing is working in one controller and not working in another controller


enter image description here

I have this code,

await _publishEndpoint.Publish(myMessage);

The same code insert into InboxState/OutboxState/OutboxMessage in one controller and not working in another controller.

Even I have used SQL profiler which shows no queries issued?

Why is behaving like this?

Looking at the source look like SaveChanges is not called?

            var outboxMessage = new OutboxMessage
            {
                MessageId = context.MessageId.Value,
                ConversationId = context.ConversationId,
                CorrelationId = context.CorrelationId,
                InitiatorId = context.InitiatorId,
                RequestId = context.RequestId,
                SourceAddress = context.SourceAddress,
                DestinationAddress = context.DestinationAddress,
                ResponseAddress = context.ResponseAddress,
                FaultAddress = context.FaultAddress,
                SentTime = context.SentTime ?? now,
                ContentType = context.ContentType?.ToString() ?? context.Serialization.DefaultContentType.ToString(),
                Body = body.GetString(),
                InboxMessageId = inboxMessageId,
                InboxConsumerId = inboxConsumerId,
                OutboxId = outboxId
            };

            if (context.TimeToLive.HasValue)
                outboxMessage.ExpirationTime = now + context.TimeToLive;

            if (context.Delay.HasValue)
                outboxMessage.EnqueueTime = now + context.Delay;

            outboxMessage.Headers = deserializer.SerializeDictionary(context.Headers.GetAll());

            if (context is TransportSendContext<T> transportSendContext)
            {
                var properties = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

                transportSendContext.WritePropertiesTo(properties);
                outboxMessage.Properties = deserializer.SerializeDictionary(properties);
            }

            await collection.AddAsync(outboxMessage, context.CancellationToken).ConfigureAwait(false);

https://github.com/MassTransit/MassTransit/blob/bda1932f1851aaa102062681f1b8d6fbff9a4871/src/Persistence/MassTransit.EntityFrameworkCoreIntegration/EntityFrameworkCoreIntegration/EntityFrameworkOutboxExtensions.cs#L59


Solution

  • The transactional outbox is designed to be used when an application is performing database operations combined with producing messages. It isn't the responsibility of MassTransit, with the Bus Outbox, to commit changes to the database – it is only adding records to the DbContext and it's up to the application to call SaveChangesAsync and/or commit the overall transaction.

    This is different than the Consumer Outbox, where the entire transaction is managed by MassTransit.