Skip to content

Add event for when a resource stops to enable integrations for lifecycle management #10584

@davidfowl

Description

@davidfowl

Currently, Aspire supports events for resource initialization, start, readiness, connection string availability, and endpoint allocation. However, there is no event that is triggered when a specific resource stops.

Adding an event that fires when a resource stops would enable users to build integrations that can register and unregister resources appropriately as they start, stop, or restart. This would improve lifecycle management and allow for more robust cleanup and teardown operations for resources.

Proposal:

  • Add a new event (e.g., ResourceStoppedEvent) to the set of available events in EventingExtensions.cs.
  • Provide an extension method (e.g., OnResourceStopped) similar to the existing ones for subscribing to this new event.
  • Ensure that this event is fired when a resource stops, and that appropriate data is provided to the event handler.
  • Model the new event on the existing BeforeResourceStartedEvent class, following its structure and conventions.

Example implementation:

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.Eventing;

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// This event is raised by orchestrators after a resource has stopped.
/// </summary>
/// <param name="resource">The resource that has stopped.</param>
/// <param name="services">The <see cref="IServiceProvider"/> for the app host.</param>
/// <remarks>
/// This event allows for cleanup or unregistration logic when a resource is stopped by an orchestrator.
/// </remarks>
public class ResourceStoppedEvent(IResource resource, IServiceProvider services) : IDistributedApplicationResourceEvent
{
    /// <inheritdoc />
    public IResource Resource { get; } = resource;

    /// <summary>
    /// The <see cref="IServiceProvider"/> for the app host.
    /// </summary>
    public IServiceProvider Services { get; } = services;
}

Relevant file(s):

This enhancement would allow users to cleanly register and unregister integrations when resources start, stop, or restart, improving the developer experience and integration capabilities.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-app-modelIssues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions