Skip to content

Conversation

@afscrome
Copy link
Contributor

@afscrome afscrome commented Jun 27, 2025

Description

Add a number of extension method on IResourceBuilder<T> to easily subscribe to IDistributedApplicationResourceEvent on a resource

Refactored a few subscription calls over to use the new methods. I can refactor more, just wanted to get a small amount (one per event) to prove the concept out.

Fixes #9168

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected. - Follow up PR to migrate existing uses of .Subscribe<T> over to the new methods
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?

Add a number of extension method on `IResourceBuilder<T>` to easily subscribe to `IDistributedApplicationResourceEvent` on a resource

Refactored a few subscription calls over to use the new methods - at least one for each event
@github-actions github-actions bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Jun 27, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jun 27, 2025
@afscrome
Copy link
Contributor Author

cc @DamianEdwards

@afscrome afscrome changed the title WIP: Add IResourceBuilder<T> extension method for event subscriptions WIP: Add IResourceBuilder<T> extension method for event subscriptions Jun 27, 2025
@davidfowl
Copy link
Member

We're trying to decide When* vs On*

@mitchdenny
Copy link
Member

Yeah, I think On* already is an established pattern in .NET and I'd prefer not to muddy the waters there anymore (there is already some inconsistency there that we could make worse by doing this with Aspire).

I think what we want is When<T>(...) and WhenEvent(...) which calls When<T>(...).

@davidfowl davidfowl requested a review from Copilot July 1, 2025 04:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new set of extension methods on IResourceBuilder for event subscriptions and refactors existing subscriptions across various resource builders to use these extensions. Key changes include the creation of new event subscription extension methods, refactoring of subscription calls in SQL Server, Azure, CosmosDB, and custom resource code, and updates to fluent API chaining.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Aspire.Hosting/ResourceBuilderExtensions.cs Refactored event subscriptions to use the new extension methods.
src/Aspire.Hosting/EventingExtensions.cs Added new extension methods for event subscriptions.
src/Aspire.Hosting.SqlServer/SqlServerBuilderExtensions.cs Updated SQL Server resource builder to chain event subscriptions.
src/Aspire.Hosting.Azure.Storage/AzureStorageExtensions.cs Refactored Azure Storage event subscription calls.
src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBExtensions.cs Refactored CosmosDB subscription and iterated over database collection.
src/Aspire.Hosting.Azure.AIFoundry/AzureAIFoundryExtensions.cs Updated custom resource subscriptions to use the extension methods.
playground/mongo/Mongo.AppHost/Program.cs Refactored MongoDB subscription to use the new event extension method.
playground/CustomResources/CustomResources.AppHost/TalkingClockResource.cs Updated custom resource initialization and event handling.
Comments suppressed due to low confidence (1)

src/Aspire.Hosting/EventingExtensions.cs:15

  • The XML documentation for OnBeforeResourceStarted contains an extraneous quotation mark and trailing '/>' at the end. Consider removing these characters for clarity.
    /// Subscribes a callback to the <see cref="BeforeResourceStartedEvent"/> event within the AppHost."/>

});

CosmosClient? cosmosClient = null;
builder.OnConnectionStringAvailable(async (cosmosDb, @event, ct) =>{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting here is all messed up.

Copy link
Member

@davidfowl davidfowl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! We can do a follow up to finishing consuming these APIs everywhere

- Fix formatting in AzureCosmosDBExtensions
- Fix rogue `"/>` in `On*` method xml docs
@afscrome afscrome changed the title WIP: Add IResourceBuilder<T> extension method for event subscriptions Add IResourceBuilder<T> extension method for event subscriptions Jul 1, 2025
@mitchdenny
Copy link
Member

Agree with @davidfowl - this was a good idea @afscrome

@davidfowl davidfowl merged commit 82d22d9 into dotnet:main Jul 1, 2025
252 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jul 31, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Fluent methods on IResourceBuilder<T> for subscribing to resource events

3 participants