Skip to content

[docs] Fix Subscribe syntax bug and adopt OnConnectionStringAvailable/OnInitializeResource helpers#943

Draft
IEvangelist wants to merge 1 commit into
mainfrom
dapine/fix-eventing-docs-helpers-315
Draft

[docs] Fix Subscribe syntax bug and adopt OnConnectionStringAvailable/OnInitializeResource helpers#943
IEvangelist wants to merge 1 commit into
mainfrom
dapine/fix-eventing-docs-helpers-315

Conversation

@IEvangelist
Copy link
Copy Markdown
Member

Fixes #315

Summary

Issue #315 asked us to refresh the eventing docs to use the new resource-level helper methods (OnInitializeResource, OnConnectionStringAvailable, OnResourceReady, etc.) and to sweep remaining Eventing.Subscribe<T> usages out of the docs where a helper now applies.

PR #821 already modernized the main eventing landing page and added the helper table, but:

  1. It left a regression in the lower-level fallback example — the Eventing.Subscribe<AfterResourcesCreatedEvent>(...) call is missing its closing });, so the snippet as published on https://aspire.dev/app-host/eventing/ doesn't compile.
  2. It didn't reach the other docs the issue called out:
    • architecture/resource-examples.mdx still uses Eventing.Subscribe<ConnectionStringAvailableEvent>(redis, ...).
    • extensibility/custom-resources.mdx uses Eventing.Subscribe<AfterResourcesCreatedEvent>(resource, ...), which is a compile error today — AfterResourcesCreatedEvent doesn't implement IDistributedApplicationResourceEvent, so the resource overload of Subscribe won't accept it.

This PR fixes the three issues above and leaves all Eventing.Subscribe<T> usages in place where no helper currently exists (e.g. AfterEndpointsAllocatedEvent, AfterResourcesCreatedEvent at the AppHost level) or where it's intentional historical context (release-notes pages, low-level pattern references).

Changes

  • src/frontend/src/content/docs/app-host/eventing.mdx — adds the missing }); that closes the Eventing.Subscribe<AfterResourcesCreatedEvent>(...) lower-level fallback example introduced in [docs] AppHost eventing: document OnBeforeStart, OnBeforePublish, OnAfterPublish builder extension methods #821.
  • src/frontend/src/content/docs/architecture/resource-examples.mdx — replaces the standalone builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(redis, ...) block with OnConnectionStringAvailable(...) chained into the resource's fluent configuration. Renumbers the inline step comments (4 → 4 / 5 → 5.a–5.h) so the prose still tracks the walkthrough's structure.
  • src/frontend/src/content/docs/extensibility/custom-resources.mdx — rewrites the "Using inline event subscriptions" example to use OnInitializeResource((resource, @event, ct) => ...), which actually compiles and is semantically correct for the section's intent (per-resource one-time initialization). Adds a sentence listing the resource-level helpers so readers can see the full set.

Evidence the bugs are live

Captured from https://aspire.dev on the day of this PR (the published HTML for the same code blocks):

https://aspire.dev/app-host/eventing/ — missing });:

builder.Eventing.Subscribe<AfterResourcesCreatedEvent>(
    async (@event, ct) =>
    {
        var logger = @event.Services.GetRequiredService<ILogger<Program>>();
        logger.LogInformation("AfterResourcesCreatedEvent");
        return Task.CompletedTask;

builder.Build().Run();

https://aspire.dev/architecture/resource-examples/ — uses old API:

builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(redis, async (@event, ct) =>
{
    // ...
});

https://aspire.dev/extensibility/custom-resources/ — uses the resource overload with an event type that doesn't satisfy the generic constraint:

builder.Eventing.Subscribe<AfterResourcesCreatedEvent>(resource, async (@event, ct) =>
{
    // Initialize mail server, create test mailboxes, etc.
});

How I verified

  1. API surface check — read src/Aspire.Hosting/DistributedApplicationEventingExtensions.cs and src/Aspire.Hosting/Eventing/IDistributedApplicationEventing.cs in microsoft/aspire to enumerate the AppHost-level helpers (OnBeforeStart, OnBeforePublish, OnAfterPublish) and resource-level helpers (OnBeforeResourceStarted, OnResourceStopped, OnConnectionStringAvailable, OnInitializeResource, OnResourceEndpointsAllocated, OnResourceReady), and to confirm the generic constraints (T : IDistributedApplicationEvent vs T : IDistributedApplicationResourceEvent vs T : IResourceWithConnectionString).

  2. Compile-check — scaffolded an empty AppHost with aspire new aspire-empty --language csharp and copied each of the modified code patterns into it (Subscribe<AfterResourcesCreatedEvent>(...) with the closing });, OnBeforeStart/OnBeforePublish/OnAfterPublish on the builder, OnConnectionStringAvailable((resource, @event, ct) => ...) on a RedisResource chain, OnInitializeResource((resource, @event, ct) => ...) on a resource builder). dotnet build succeeds with no errors or warnings.

  3. Live regression confirmation — fetched the three pages on https://aspire.dev directly and observed the broken/outdated code blocks above.

Notes

  • architecture/resource-hierarchies.mdx still uses Eventing.Subscribe<AfterEndpointsAllocatedEvent>(...) — left as-is, because no helper exists for AfterEndpointsAllocatedEvent.
  • architecture/resource-api-patterns.mdx references Subscribe<T> deliberately as the underlying pattern — left as-is.
  • whats-new/*.mdx release notes also reference Eventing.Subscribe<T> — left as-is; these are historical and intentionally describe the API of that release.
  • The remaining Subscribe<ResourceStoppedEvent>(cache, ...) example near the bottom of app-host/eventing.mdx (line ~416) could be modernized to cache.OnResourceStopped(...). I left it alone to keep this PR scoped to the issue's reported defects; happy to fold it in if reviewers prefer.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

…/OnInitializeResource helpers

Fixes #315

- app-host/eventing.mdx: add missing }); that closes the lower-level
  Eventing.Subscribe<AfterResourcesCreatedEvent>(...) fallback example
  (regression from #821 — the published code did not compile).
- architecture/resource-examples.mdx: replace the standalone
  builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(redis, ...)
  block with the OnConnectionStringAvailable helper chained into the
  fluent resource configuration. Renumber the inline step comments
  (4 → 4 / 5 → 5.a–5.h) so the walkthrough still tracks.
- extensibility/custom-resources.mdx: rewrite the "Using inline event
  subscriptions" example to use OnInitializeResource, which is
  semantically correct for per-resource one-time init and (unlike the
  previous Subscribe<AfterResourcesCreatedEvent>(resource, ...) call)
  actually compiles. Add a sentence listing the resource-level helpers.

Verified by compiling each modified code pattern against the latest
Aspire 13.3 packages (aspire new aspire-empty, dotnet build) and by
cross-referencing src/Aspire.Hosting/DistributedApplicationEventingExtensions.cs
for helper names, generic constraints, and callback signatures.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

[13.2] Update App Host Events

1 participant