@@ -14,13 +14,13 @@ namespace Aspire.Hosting.ApplicationModel;
14
14
/// <summary>
15
15
/// A service that allows publishing and subscribing to changes in the state of a resource.
16
16
/// </summary>
17
- public class ResourceNotificationService
17
+ public class ResourceNotificationService : IDisposable
18
18
{
19
19
// 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.
20
20
private readonly ConcurrentDictionary < ( IResource , string ) , ResourceNotificationState > _resourceNotificationStates = new ( ) ;
21
21
private readonly ILogger < ResourceNotificationService > _logger ;
22
22
private readonly IServiceProvider _serviceProvider ;
23
- private readonly CancellationToken _applicationStopping ;
23
+ private readonly CancellationTokenSource _disposing = new ( ) ;
24
24
private readonly ResourceLoggerService _resourceLoggerService ;
25
25
26
26
private Action < ResourceEvent > ? OnResourceUpdated { get ; set ; }
@@ -43,7 +43,6 @@ public ResourceNotificationService(ILogger<ResourceNotificationService> logger,
43
43
{
44
44
_logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
45
45
_serviceProvider = new NullServiceProvider ( ) ;
46
- _applicationStopping = hostApplicationLifetime ? . ApplicationStopping ?? throw new ArgumentNullException ( nameof ( hostApplicationLifetime ) ) ;
47
46
_resourceLoggerService = new ResourceLoggerService ( ) ;
48
47
}
49
48
@@ -58,8 +57,10 @@ public ResourceNotificationService(ILogger<ResourceNotificationService> logger,
58
57
{
59
58
_logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
60
59
_serviceProvider = serviceProvider ;
61
- _applicationStopping = hostApplicationLifetime ? . ApplicationStopping ?? throw new ArgumentNullException ( nameof ( hostApplicationLifetime ) ) ;
62
60
_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.
63
64
}
64
65
65
66
private class NullServiceProvider : IServiceProvider
@@ -105,7 +106,7 @@ public Task WaitForResourceAsync(string resourceName, string? targetState = null
105
106
Justification = "targetState(s) parameters are mutually exclusive." ) ]
106
107
public async Task < string > WaitForResourceAsync ( string resourceName , IEnumerable < string > targetStates , CancellationToken cancellationToken = default )
107
108
{
108
- using var watchCts = CancellationTokenSource . CreateLinkedTokenSource ( _applicationStopping , cancellationToken ) ;
109
+ using var watchCts = CancellationTokenSource . CreateLinkedTokenSource ( _disposing . Token , cancellationToken ) ;
109
110
var watchToken = watchCts . Token ;
110
111
await foreach ( var resourceEvent in WatchAsync ( watchToken ) . ConfigureAwait ( false ) )
111
112
{
@@ -273,7 +274,7 @@ public async Task WaitForDependenciesAsync(IResource resource, CancellationToken
273
274
Justification = "predicate and targetState(s) parameters are mutually exclusive." ) ]
274
275
public async Task < ResourceEvent > WaitForResourceAsync ( string resourceName , Func < ResourceEvent , bool > predicate , CancellationToken cancellationToken = default )
275
276
{
276
- using var watchCts = CancellationTokenSource . CreateLinkedTokenSource ( _applicationStopping , cancellationToken ) ;
277
+ using var watchCts = CancellationTokenSource . CreateLinkedTokenSource ( _disposing . Token , cancellationToken ) ;
277
278
var watchToken = watchCts . Token ;
278
279
await foreach ( var resourceEvent in WatchAsync ( watchToken ) . ConfigureAwait ( false ) )
279
280
{
@@ -502,6 +503,12 @@ private static CustomResourceSnapshot GetCurrentSnapshot(IResource resource, Res
502
503
private ResourceNotificationState GetResourceNotificationState ( IResource resource , string resourceId ) =>
503
504
_resourceNotificationStates . GetOrAdd ( ( resource , resourceId ) , _ => new ResourceNotificationState ( ) ) ;
504
505
506
+ /// <inheritdoc/>
507
+ public void Dispose ( )
508
+ {
509
+ _disposing . Cancel ( ) ;
510
+ }
511
+
505
512
/// <summary>
506
513
/// The annotation that allows publishing and subscribing to changes in the state of a resource.
507
514
/// </summary>
0 commit comments