@@ -14,13 +14,13 @@ namespace Aspire.Hosting.ApplicationModel;
1414/// <summary>
1515/// A service that allows publishing and subscribing to changes in the state of a resource.
1616/// </summary>
17- public class ResourceNotificationService
17+ public class ResourceNotificationService : IDisposable
1818{
1919 // Resource state is keyed by the resource and the unique name of the resource. This could be the name of the resource, or a replica ID.
2020 private readonly ConcurrentDictionary < ( IResource , string ) , ResourceNotificationState > _resourceNotificationStates = new ( ) ;
2121 private readonly ILogger < ResourceNotificationService > _logger ;
2222 private readonly IServiceProvider _serviceProvider ;
23- private readonly CancellationToken _applicationStopping ;
23+ private readonly CancellationTokenSource _disposing = new ( ) ;
2424 private readonly ResourceLoggerService _resourceLoggerService ;
2525
2626 private Action < ResourceEvent > ? OnResourceUpdated { get ; set ; }
@@ -43,7 +43,6 @@ public ResourceNotificationService(ILogger<ResourceNotificationService> logger,
4343 {
4444 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
4545 _serviceProvider = new NullServiceProvider ( ) ;
46- _applicationStopping = hostApplicationLifetime ? . ApplicationStopping ?? throw new ArgumentNullException ( nameof ( hostApplicationLifetime ) ) ;
4746 _resourceLoggerService = new ResourceLoggerService ( ) ;
4847 }
4948
@@ -58,8 +57,10 @@ public ResourceNotificationService(ILogger<ResourceNotificationService> logger,
5857 {
5958 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
6059 _serviceProvider = serviceProvider ;
61- _applicationStopping = hostApplicationLifetime ? . ApplicationStopping ?? throw new ArgumentNullException ( nameof ( hostApplicationLifetime ) ) ;
6260 _resourceLoggerService = resourceLoggerService ?? throw new ArgumentNullException ( nameof ( resourceLoggerService ) ) ;
61+
62+ // The IHostApplicationLifetime parameter is not used anymore, but we keep it for backwards compatibility.
63+ // Notfication updates will be cancelled when the service is disposed.
6364 }
6465
6566 private class NullServiceProvider : IServiceProvider
@@ -105,7 +106,7 @@ public Task WaitForResourceAsync(string resourceName, string? targetState = null
105106 Justification = "targetState(s) parameters are mutually exclusive." ) ]
106107 public async Task < string > WaitForResourceAsync ( string resourceName , IEnumerable < string > targetStates , CancellationToken cancellationToken = default )
107108 {
108- using var watchCts = CancellationTokenSource . CreateLinkedTokenSource ( _applicationStopping , cancellationToken ) ;
109+ using var watchCts = CancellationTokenSource . CreateLinkedTokenSource ( _disposing . Token , cancellationToken ) ;
109110 var watchToken = watchCts . Token ;
110111 await foreach ( var resourceEvent in WatchAsync ( watchToken ) . ConfigureAwait ( false ) )
111112 {
@@ -273,7 +274,7 @@ public async Task WaitForDependenciesAsync(IResource resource, CancellationToken
273274 Justification = "predicate and targetState(s) parameters are mutually exclusive." ) ]
274275 public async Task < ResourceEvent > WaitForResourceAsync ( string resourceName , Func < ResourceEvent , bool > predicate , CancellationToken cancellationToken = default )
275276 {
276- using var watchCts = CancellationTokenSource . CreateLinkedTokenSource ( _applicationStopping , cancellationToken ) ;
277+ using var watchCts = CancellationTokenSource . CreateLinkedTokenSource ( _disposing . Token , cancellationToken ) ;
277278 var watchToken = watchCts . Token ;
278279 await foreach ( var resourceEvent in WatchAsync ( watchToken ) . ConfigureAwait ( false ) )
279280 {
@@ -502,6 +503,12 @@ private static CustomResourceSnapshot GetCurrentSnapshot(IResource resource, Res
502503 private ResourceNotificationState GetResourceNotificationState ( IResource resource , string resourceId ) =>
503504 _resourceNotificationStates . GetOrAdd ( ( resource , resourceId ) , _ => new ResourceNotificationState ( ) ) ;
504505
506+ /// <inheritdoc/>
507+ public void Dispose ( )
508+ {
509+ _disposing . Cancel ( ) ;
510+ }
511+
505512 /// <summary>
506513 /// The annotation that allows publishing and subscribing to changes in the state of a resource.
507514 /// </summary>
0 commit comments