Register the sample's DbContext on the plain Npgsql provider - #1
Merged
Merged
Conversation
The sample referenced Aspire.Npgsql.EntityFrameworkCore.PostgreSQL and called
builder.AddNpgsqlDbContext<AppDbContext>("settingsdb"). It now takes a direct
reference on Npgsql.EntityFrameworkCore.PostgreSQL and registers the context
with AddDbContext + UseNpgsql, reading the connection string the AppHost
injects as ConnectionStrings__settingsdb.
That wrapper package supplied three things on its own, so they are written out
explicitly here to leave the sample behaving as it did:
- readiness, via AddDbContextCheck<AppDbContext>(), so /health reports
Unhealthy while Postgres is unreachable instead of answering Healthy
- telemetry, via Npgsql's ActivitySource and Meter, so a dashboard trace
still shows the SQL a request ran rather than stopping at the HTTP span,
and the Metrics page keeps the connection-pool counters
- connection retry, bounded to 3 attempts backing off to 2s. The default
policy of 6 retries backing off to 30s also governs CanConnectAsync, which
left the readiness check taking about a minute to report a database that
was plainly gone; bounded, it answers in about three seconds.
The api resource gains WithHttpHealthCheck("/health") so that WaitFor(api)
holds the dashboard back until Postgres actually answers, not merely until the
process has started.
Dropping the Npgsql version pin from Directory.Packages.props requires the
matching PackageReference to go from the integration tests too, or central
package management fails the restore with NU1010.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
What
The sample referenced
Aspire.Npgsql.EntityFrameworkCore.PostgreSQLand calledbuilder.AddNpgsqlDbContext<AppDbContext>("settingsdb"). It now takes a direct reference onNpgsql.EntityFrameworkCore.PostgreSQLand registers the context withAddDbContext+UseNpgsql, reading the connection string the AppHost injects asConnectionStrings__settingsdb.Why the extra lines
That wrapper package supplied three things on its own. They are written out explicitly so the sample behaves as it did before:
AddDbContextCheck<AppDbContext>(), so/healthreports Unhealthy while Postgres is unreachable instead of answering Healthy.ActivitySourceandMeter, so a dashboard trace still shows the SQL a request ran rather than stopping at the HTTP span, and the Metrics page keeps the connection-pool counters.CanConnectAsync, which left the readiness check taking about a minute to report a database that was plainly gone. Bounded, it answers in about three seconds.The
apiresource also gainsWithHttpHealthCheck("/health")soWaitFor(api)holds the dashboard back until Postgres actually answers, not merely until the process has started.Coupled change
Dropping the
Npgsqlversion pin fromDirectory.Packages.propsrequires the matchingPackageReferenceto go from the integration tests too, or central package management fails the restore withNU1010.Verification
Run against the full stack under the AppHost, not just compiled:
dotnet build -c Release -warnaserror— 0 warnings, 0 errors/healthagainst a stopped Postgres container: 503 Unhealthy in 3.1s;/alivestayed Healthy; recovered on its own once the container returnedGET api/settings/mail-server/→postgresql → settingsdb(10.85ms) +DATA redis GET → cache(0.89ms)Npgsqlmeter with livedb.client.connection.countdata, split byidle/useddashboardsat in Waiting whileapicame up healthy, then moved to Running — the new health gate working[Sensitive]columns, FluentValidation rejection (400), ETag concurrency (412 stale / 204 current), audit trail, change-notification handler, OpenAPI documentNote
MapDefaultEndpointsonly maps/healthin Development, soWithHttpHealthCheckwould poll a 404 if this AppHost is ever published. Fine fordotnet run; worth revisiting before publish.🤖 Generated with Claude Code