Skip to content

[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
mainfrom
tte/mocha-ef-outbox-contexts
Closed

tobias-tengler wants to merge 13 commits into
mainfrom
tte/mocha-ef-outbox-contexts

Conversation

@tobias-tengler

@tobias-tengler tobias-tengler commented Nov 21, 2026 •

Copy link
Copy Markdown
Member
  • UseTransaction() opens its transaction on the DbContext it is configured on instead of the one registered last.
  • Every context registered with UsePostgresOutbox() gets its own outbox processor, registered as a keyed singleton with the context type as key, and its own IOutboxSignal.
  • Calling UsePostgresOutbox() or UseOutboxCore() twice for one context registers once.
  • A commit wakes the outbox processor only when its transaction wrote an outbox message. A save no longer wakes it.
  • An idle outbox processor checks the database every PostgresOutboxOptions.PollingInterval, ten seconds by default.
  • Hosts in one process share EF Core's internal service provider. The interceptors read their signals and clock from the context.
  • ConfigureEntityFrameworkServices and MessagingDbContextOptions are internal.
  • RequireTransaction rejects a connection that did not join the ambient transaction and accepts one enlisted with Database.EnlistTransaction.
  • A DbContext that is not registered as a service fails with an exception that names the fix.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Patch coverage

82.5% of changed lines covered (165/200)

File Covered Changed Patch %
…/EntityFrameworkConsumerExecutionStrategy.cs 0 19 0.0% 🔴
…/EntityFrameworkCoreBuilderExtensions.cs 6 15 40.0% 🔴
…/EntityFrameworkTransactionConsumeMiddleware.cs 16 21 76.2% 🔴
…/Outbox/OutboxDbTransactionInterceptor.cs 7 9 77.8% 🔴
…/Mocha.EntityFrameworkCore.Postgres/Outbox/OutboxProcessor.cs 19 19 100.0% 🟢
…/Outbox/OutboxServiceCollectionExtensions.cs 22 22 100.0% 🟢
…/Outbox/PostgresMessageOutbox.cs 16 16 100.0% 🟢
…/Outbox/PostgresOutboxOptions.cs 1 1 100.0% 🟢
…/src/Mocha.EntityFrameworkCore.Postgres/Outbox/ThrowHelper.cs 8 8 100.0% 🟢
…/DbContextMessagingServicesExtensions.cs 1 1 100.0% 🟢
…/DbContextOptionsBuilderExtensions.cs 1 1 100.0% 🟢
…/DbContextServiceProviderExtensions.cs 2 2 100.0% 🟢
…/EntityFrameworkCorePersistenceBuilderExtensions.cs 1 1 100.0% 🟢
…/Mocha.EntityFrameworkCore/MessageBusHostBuilderExtensions.cs 13 13 100.0% 🟢
…/src/Mocha.EntityFrameworkCore/MessagingDbContextOptions.cs 1 1 100.0% 🟢
…/MessagingDbContextOptionsConfiguration.cs 4 4 100.0% 🟢
…/MessagingDbContextOptionsExtension.cs 6 6 100.0% 🟢
…/src/Mocha.EntityFrameworkCore/MessagingDbContextServices.cs 7 7 100.0% 🟢
…/Outbox/OutboxDbContextRegistration.cs 1 1 100.0% 🟢
…/OutboxEntityFrameworkCorePersistenceBuilderExtensions.cs 11 11 100.0% 🟢

+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
tobias-tengler force-pushed the tte/mocha-single-pass-dispatch branch from a3faeff to 126211d Compare November 21, 2026 15:47
@tobias-tengler
tobias-tengler force-pushed the tte/mocha-ef-outbox-contexts branch from e7820a6 to 368b4f1 Compare November 21, 2026 15:47
@tobias-tengler
tobias-tengler force-pushed the tte/mocha-single-pass-dispatch branch from 126211d to f232736 Compare November 21, 2026 15:54
@tobias-tengler
tobias-tengler force-pushed the tte/mocha-ef-outbox-contexts branch 2 times, most recently from 867d35d to cccf8c5 Compare November 21, 2026 19:54
@tobias-tengler
tobias-tengler force-pushed the tte/mocha-single-pass-dispatch branch from f232736 to 62e39b1 Compare November 21, 2026 19:54
@tobias-tengler
tobias-tengler force-pushed the tte/mocha-ef-outbox-contexts branch 2 times, most recently from c4b0ad1 to 020e827 Compare November 23, 2026 13:31
@tobias-tengler
tobias-tengler force-pushed the tte/mocha-single-pass-dispatch branch from 8b1fc2c to 09ca87a Compare November 23, 2026 13:31
… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📚 documentation This issue is about working on our documentation. 🌶️ mocha

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant