Aspire

Bending Aspire to your will - Build a custom resource (Part 2)

Bart Wullems (The Art of Simplicity) Bart Wullems
Aspire

The second half of a custom Aspire resource: the Scalar container discovers OpenAPI documents at startup and reports them to the dashboard as state and logs. Annotations, a deferred environment callback and an eventing subscriber in one example.

Key points

  • AddScalar() reuses an existing ScalarResource instead of starting a second container.
  • The scalarapi/api-reference 0.6.22 image is wired with port 8080, a /health probe and ExcludeFromManifest().
  • A deferred WithEnvironment callback collects OpenApiAnnotation entries at startup and serializes them into API_REFERENCE_CONFIG.
  • Documents must resolve to a browser-facing ResourceUrlAnnotation, so the APIs need CORS, enabled only in Development in the demo.
  • A missing endpoint or an AppHost without WithOpenApi() throws an exception that says what to fix.
  • ScalarEndpointsDiscoveredEvent is handled by a subscriber that publishes state and logs through ResourceNotificationService and ResourceLoggerService.

Why it matters

Most teams reach for a custom Aspire resource before checking what already exists, and this series is a good reminder of the ordering: search the integration catalogue and the Community Toolkit first, then copy the patterns for your own tool. The work lands in AppHost code, and the deferred callback means your resource now depends on browser-facing URLs and CORS, so verify the Development CORS policy on every API that exposes a document. Publishing state and logs through the eventing services is what makes a container-based tool debuggable. Before adopting, confirm ExcludeFromManifest() keeps the tool out of publish and that the health check points at a real endpoint.

Real-world use

  • An AppHost hosting several services that each expose an OpenAPI document, where one dashboard URL should open every API reference.
  • A developer-only tool container that belongs in the local resource graph but must stay out of publish and deploy.
  • Diagnosing a resource container that came up empty by surfacing the discovered endpoints as custom state and log lines.

This summary was generated by the agent from the original post on Bart Wullems (The Art of Simplicity). The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message