Skip to content

Shared DbContext and Transaction Across Handlers in Wolverine Within a Single Request Scope in a Modular Monolith .NET Application #2517

Closed

Description

@ensstc

Hello, I want to develop a modular monolith .NET application. My main goal is to ensure that all modules in the project do not reference each other directly and that each module works with its own DbContext object.

While modules use different DbContext objects, I want to proceed by sharing a common connection and transaction (wrapping the entire request lifetime with an in-memory transaction, without using outbox or inbox patterns). For this purpose, I defined a Wolverine DbContext object globally and aim to pass its connection and transaction to other modules.

My problem is this: the “Wolverine DbContext” object I obtain in the handler of Module 1 is different from the “Wolverine DbContext” object I obtain in the handler of Module 2 (even though it is registered as scoped). I assume that Wolverine creates a new DI scope for each handler, which causes the object to be recreated.

Is it possible to use the same scope throughout the request lifetime so that all handler calls can share common dependencies? If yes, how can I achieve this?

Thank you.

Activity

  1. jeremydmiller commented on Apr 15, 2026

    @jeremydmiller
    Member

    Nope, nope, nope. Sorry, I want absolutely nothing to do with that!

    "I assume that Wolverine creates a new DI scope for each handler, which causes the object to be recreated" -- yikes, no, not sure where you got that from.

    My consistent advice in anything like this is:

    • You should never need to have a transaction that spans multiple DbContext objects
    • So either change your DbContext boundaries because they're wrong, or...
    • use eventual consistency between modules if you must.

    I'm converting this to a discussion in case someone else has something better to add.

  2. locked and limited conversation to collaborators on Apr 15, 2026
  3. converted this issue into a discussion #2519 on Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions