Skip to content

Traced event-store registration duplicates services and overrides existing registrations #611

Description

@nmummau

Related test issue

Discovered while implementing #608 — event-store dependency injection coverage.

Problem

With tracing enabled, both overloads of AddEventStore<T> use unconditional AddSingleton calls for the traced store and its interface registrations. Calling the registration method repeatedly adds duplicate service descriptors. Calling it after explicitly registering IEventStore, IEventReader, or IEventWriter replaces the service returned by normal single-service resolution.

This differs from the tracing-disabled path's TryAddSingleton behavior. Enabling diagnostics can therefore change service selection and route writes to a different backing store.

Reproduction

Repeated registration

  1. Enable diagnostics, preserving the previous setting for restoration afterward.
  2. Call services.AddEventStore<InMemoryEventStore>() twice and build a host.
  3. Resolve GetServices<IEventWriter>(), GetServices<IEventReader>(), and GetServices<IEventStore>().

Actual: each enumeration contains duplicate registrations.

Expected: repeating the default registration leaves one registration per interface.

Explicit service overrides

  1. Register an existing store instance explicitly as IEventStore, IEventReader, and IEventWriter.
  2. Call services.AddEventStore<InMemoryEventStore>() with tracing enabled.
  3. Build the host and append an event through its resolved IEventWriter.
  4. Read the stream through the explicitly registered instance.

Actual: the explicit instance has no such stream; the later registration selected a different store.

Expected: default store registration preserves explicitly registered interfaces, consistently with tracing disabled.

Fix and acceptance criteria

  • Use conditional registration for the traced store and interface aliases.
  • Repeated generic and factory registrations do not add duplicate interface services.
  • Existing interface registrations remain the services resolved by the host.
  • An unused replacement factory is not invoked.
  • Tests verify actual append/read behavior, in addition to service counts and identities.
  • Test both tracing modes and restore the global diagnostics setting with parallel isolation.

Existing regression tests and implementation

StoreRegistrationTests.cs in src/Extensions/test/Eventuous.Tests.DependencyInjection contains RepeatedRegistrationKeepsOneServiceAndOneFactory and RegistrationPreservesExplicitInterfaceOverrides. Their tracing-enabled cases reproduced these failures before the fix.

An implementation is already committed locally on fix/issue-608-store-registrations, commit df3dbe48. It also fixes separate backing instances when tracing is disabled. The complete DI project passes all 31 tests on .NET 10 after both fixes. The branch has not been published or merged as part of this work.

Activity

  1. linear commented on Oct 6, 2026

    @linear
  2. nmummau commented on Oct 6, 2026

    @nmummau
    ContributorAuthor

    I’ve implemented this locally with regression tests. I’m waiting for a prerequisite PR 603 to merge so I can rebase and open a focused PR. I’ll link it here once it’s ready.

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