Repository navigation
[Mocha] Fix the Postgres outbox with several DbContexts and with commits it is not notified about - #11017
Closed
tobias-tengler wants to merge 13 commits into
Closed
[Mocha] Fix the Postgres outbox with several DbContexts and with commits it is not notified about#11017tobias-tengler wants to merge 13 commits into
tobias-tengler wants to merge 13 commits into
Conversation
tobias-tengler
force-pushed
the
tte/mocha-single-pass-dispatch
branch
from
November 21, 2026 10:49
ace4a39 to
a3faeff
Compare
tobias-tengler
force-pushed
the
tte/mocha-ef-outbox-contexts
branch
from
November 21, 2026 10:49
97a3145 to
e7820a6
Compare
Contributor
Patch coverage82.5% of changed lines covered (165/200) +4 more changed files; see the JSON below for uncovered lines. Uncovered changed lines (JSON){
"sha": "50de3ee67d93479df792ae519c44d4357c49e7f6",
"files": [
{ "path": "src/Mocha/src/Mocha.EntityFrameworkCore/EntityFrameworkConsumerExecutionStrategy.cs", "ranges": [[8, 8], [11, 14], [16, 19], [23, 31], [33, 33]] },
{ "path": "src/Mocha/src/Mocha.EntityFrameworkCore/EntityFrameworkCoreBuilderExtensions.cs", "ranges": [[14, 20], [22, 23]] },
{ "path": "src/Mocha/src/Mocha.EntityFrameworkCore/EntityFrameworkTransactionConsumeMiddleware.cs", "ranges": [[20, 24]] },
{ "path": "src/Mocha/src/Mocha.EntityFrameworkCore/Outbox/OutboxDbTransactionInterceptor.cs", "ranges": [[28, 29]] }
]
}Project coverage: 60.3% (333277/552757 lines) |
tobias-tengler
force-pushed
the
tte/mocha-single-pass-dispatch
branch
from
November 21, 2026 15:47
a3faeff to
126211d
Compare
tobias-tengler
force-pushed
the
tte/mocha-ef-outbox-contexts
branch
from
November 21, 2026 15:47
e7820a6 to
368b4f1
Compare
tobias-tengler
force-pushed
the
tte/mocha-single-pass-dispatch
branch
from
November 21, 2026 15:54
126211d to
f232736
Compare
tobias-tengler
force-pushed
the
tte/mocha-ef-outbox-contexts
branch
2 times, most recently
from
November 21, 2026 19:54
867d35d to
cccf8c5
Compare
tobias-tengler
force-pushed
the
tte/mocha-single-pass-dispatch
branch
from
November 21, 2026 19:54
f232736 to
62e39b1
Compare
tobias-tengler
force-pushed
the
tte/mocha-ef-outbox-contexts
branch
2 times, most recently
from
November 23, 2026 13:31
c4b0ad1 to
020e827
Compare
tobias-tengler
force-pushed
the
tte/mocha-single-pass-dispatch
branch
from
November 23, 2026 13:31
8b1fc2c to
09ca87a
Compare
… registered last UseTransaction() looked up the DbContext type in a bus feature that every AddEntityFramework<TContext>() call overwrote. With two contexts, the consumer transaction was opened on the context registered last, also when UseTransaction() was configured on the first. The middleware now gets the context type of the builder it is configured on. The builder extensions, the execution strategy and the transaction middleware move from MessageBusHostBuilderExtensions.cs into files of their own.
…t use Mocha The messaging options extension captured the application service provider and made it part of EF Core's service provider cache key, and the outbox and scheduling interceptors were created with the signals of that provider. Every host in a process therefore built its own EF Core internal service provider and model. The extension now carries the outbox signal, the scheduler signal and the clock of its application, and the interceptors read them from the context they are called for, so contexts of several hosts share one internal provider and still signal their own application. ConfigureEntityFrameworkServices and MessagingDbContextOptions become internal, because the delegate can no longer capture application services.
…t signaled about An idle outbox processor waited for its signal without a timeout. A row committed without a signal, for example in a transaction that is committed outside EF Core or by another process, stayed in the outbox until something else woke the processor. The processor now also wakes after PostgresOutboxOptions.PollingInterval, ten seconds by default, and an interval that is not positive is rejected. It logs that it is sleeping only when it waits for a message that is due before the next check.
…Contexts use it UsePostgresOutbox() registered the processor and its worker as plain singletons, so with two contexts the host ran only the processor of the context registered last and the rows of the other context were never sent. Each context now gets its own processor, registered as a keyed singleton with the context type as key, and its own worker. Calling UsePostgresOutbox() or UseOutboxCore() again for the same context registers nothing twice and still applies the configure delegate. IMessageBus publishes keep using the outbox of the first context, and the exception for a missing transaction now says so.
…lve it When the DbContext of an outbox was not registered as a service, for example with AddPooledDbContextFactory before EF Core 10, resolving the processor failed with the container's missing service error. The outbox now throws an InvalidOperationException that names the context and the registrations that make it resolvable.
… its connection did not enlist PostgresMessageOutbox accepted a publish as transactional whenever Transaction.Current was set. With Enlist=false in the connection string the connection does not join the ambient transaction, so the outbox row was written outside it and stayed when the scope rolled back. The check now asks the transaction manager of the context. A publish on a connection that has not joined throws the transaction-required exception, and a transaction enlisted through Database.EnlistTransaction without an ambient one is accepted.
…upgrade notes The reliability page says when the outbox processor is woken at once and when it finds a message at its next check, how to configure the polling interval, and which context IMessageBus publishes use when several contexts have an outbox. The migration guide covers the keyed outbox processors, the internal EF Core registration types, UseTransaction() using its own context, the idle check, and EF Core services being shared across hosts.
…nsion PostgresMessageOutbox carried the lookup of a DbContext by its runtime type as an internal static, and the outbox registration called into the store class for it. The lookup is now GetRequiredDbContext on IServiceProvider in Mocha.EntityFrameworkCore, the project that owns the context type of the builder. Its exception moves to that project's ThrowHelper as DbContextNotRegistered and keeps its message.
The guide listed ConfigureEntityFrameworkServices and MessagingDbContextOptions becoming internal, and PostgresOutboxProcessor being registered as a keyed singleton per DbContext type. Both entries are removed from the API breaking changes.
…t wrote no outbox message Every SaveChanges and every transaction commit on an outbox context signaled the outbox processor, so the processor queried the outbox table after each of them, whether or not a message was written. The outbox now marks its context when it writes a message inside a database transaction, and the commit interceptor signals only when the mark is set. OutboxSaveChangesInterceptor is removed, so a save no longer signals. An OutboxMessage entity that is added through the change tracker is found when the processor next checks the database.
All outbox processors of an application waited on one IOutboxSignal, so a commit that wrote a message on one context woke the processor of every context. UseOutboxCore on the Entity Framework Core builder now registers a signal for its context, with the DbContext type as the service key, and the outbox and the processor of the context use it. MessagingDbContextServices is registered once per context type, so the commit interceptor signals the context that committed.
…ests The tests replaced the outbox signal with ResilientOutboxSignal, because an interceptor could signal the disposed signal of another service provider. The interceptors now read the signal of the application that created the context, so the tests run with the signal that the outbox registers and the helper is removed.
PascalSenn
force-pushed
the
tte/mocha-ef-outbox-contexts
branch
from
November 29, 2026 11:33
020e827 to
57c54d3
Compare
PascalSenn
changed the base branch from
tte/mocha-single-pass-dispatch
to
main
November 29, 2026 11:33
This was referenced Nov 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
UseTransaction()opens its transaction on the DbContext it is configured on instead of the one registered last.UsePostgresOutbox()gets its own outbox processor, registered as a keyed singleton with the context type as key, and its ownIOutboxSignal.UsePostgresOutbox()orUseOutboxCore()twice for one context registers once.PostgresOutboxOptions.PollingInterval, ten seconds by default.ConfigureEntityFrameworkServicesandMessagingDbContextOptionsare internal.RequireTransactionrejects a connection that did not join the ambient transaction and accepts one enlisted withDatabase.EnlistTransaction.