Skip to content

fix(postgresql): register NpgsqlDataSource as a keyed service - #48

Merged
giammin merged 1 commit into
mainfrom
fix/postgresql-keyed-data-source
Oct 22, 2026
Merged

giammin merged 1 commit into
mainfrom
fix/postgresql-keyed-data-source

Conversation

@giammin

@giammin giammin commented Oct 22, 2026

Copy link
Copy Markdown
Owner

Summary

PersistKeysWithDapperInPostgreSQL registered its data source as an unkeyed NpgsqlDataSource singleton, which collided with a data source registered by the application (for example with AddNpgsqlDataSource): the last registration won, silently routing either the application queries or the keys to the wrong database. The instance registration also meant the container never disposed it.

  • Register the data source as a keyed singleton (internal Constants.DataSourceServiceKey), created by a factory so the container owns and disposes it.
  • Resolve it in PostgreSQLDataProtectionRepository with [FromKeyedServices]; the public constructor signature is unchanged.
  • README and AGENTS.md updated.

Behavior changes

  • An application that resolved the NpgsqlDataSource registered by this provider, without registering its own, now gets a resolution error. This relied on the bug, so it is not marked as breaking.
  • A malformed connection string now fails when the data source is first resolved (UseDapperDataProtection() at startup) instead of at registration, as with the SQL Server provider.

Tests

  • End-to-end Theory covering both registration orders: the application data source targets a non-existent database, the application still resolves its own instance, and the keys round-trip through the provider's table.
  • Unit tests: no unkeyed NpgsqlDataSource registration; the data source is disposed with the service provider; the IncludeErrorDetail test now resolves the keyed instance.
  • The new tests fail against the previous wiring (7 failures, both registration orders included).
  • Release build clean; full suite 83/83.

Closes #19

The provider registered its data source as an unkeyed NpgsqlDataSource
singleton, which collided with a data source registered by the
application: depending on the registration order, either the
application queries went to the keys database or the keys went to the
application database. The instance registration also prevented the
container from disposing it.

Register the data source as a keyed singleton created by a factory, so
the container owns and disposes it, and resolve it in the repository
with [FromKeyedServices]. The public repository constructor is
unchanged. A malformed connection string now fails when the data source
is first resolved (UseDapperDataProtection) instead of at registration.

Closes #19
@giammin
giammin merged commit 7fd4ef1 into main Oct 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL provider registers an unkeyed NpgsqlDataSource that collides with the application's

1 participant