Skip to content
DocsTry Aspire
DocsTry

ResourceNotificationService Methods

ClassMethods11 members
A service that allows publishing and subscribing to changes in the state of a resource.
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public class ResourceNotificationService
{
public void Dispose()
{
// ...
}
}
PublishUpdateAsync(IResource, string, Func<CustomResourceSnapshot, CustomResourceSnapshot>)Section titled PublishUpdateAsync(IResource, string, Func<CustomResourceSnapshot, CustomResourceSnapshot>)Task
Updates the snapshot of the CustomResourceSnapshot for a resource.
public class ResourceNotificationService
{
public Task PublishUpdateAsync(
IResource resource,
string resourceId,
Func<CustomResourceSnapshot, CustomResourceSnapshot> stateFactory)
{
// ...
}
}
resourceIResourceThe resource to update
resourceIdstring The id of the resource.
stateFactoryFunc<CustomResourceSnapshot, CustomResourceSnapshot>A factory that creates the new state based on the previous state.
PublishUpdateAsync(IResource, Func<CustomResourceSnapshot, CustomResourceSnapshot>)Section titled PublishUpdateAsync(IResource, Func<CustomResourceSnapshot, CustomResourceSnapshot>)Task
Updates the snapshot of the CustomResourceSnapshot for a resource.
public class ResourceNotificationService
{
public Task PublishUpdateAsync(
IResource resource,
Func<CustomResourceSnapshot, CustomResourceSnapshot> stateFactory)
{
// ...
}
}
resourceIResourceThe resource to update
stateFactoryFunc<CustomResourceSnapshot, CustomResourceSnapshot>A factory that creates the new state based on the previous state.
TryGetCurrentState(string, ResourceEvent?)Section titled TryGetCurrentState(string, ResourceEvent?)bool
Attempts to retrieve the current state of a resource by resourceId.
public class ResourceNotificationService
{
public bool TryGetCurrentState(
string resourceId,
out ResourceEvent? resourceEvent)
{
// ...
}
}
resourceIdstringThe resource id. This id can either exactly match the unique id of the resource or the displayed resource name if the resource name doesn't have duplicates (i.e. replicas).
resourceEventResourceEvent?When this method returns, contains the ResourceEvent for the specified resource id, if found; otherwise, null.
booltrue if specified resource id was found; otherwise, false.

A resource id can be either the unique id of the resource or the displayed resource name.

Projects, executables and containers typically have a unique id that combines the display name and a unique suffix. For example, a resource named cache could have a resource id of cache-abcdwxyz. This id is used to uniquely identify the resource in the app host.

The resource name can be also be used to retrieve the resource state, but it must be unique. If there are multiple resources with the same name, then this method will not return a match. For example, if a resource named cache has multiple replicas, then specifing cache won't return a match.

WaitForDependenciesAsync(IResource, CancellationToken)Section titled WaitForDependenciesAsync(IResource, CancellationToken)Task
Waits for all dependencies of the resource to be ready.
public class ResourceNotificationService
{
public Task WaitForDependenciesAsync(
IResource resource,
CancellationToken cancellationToken)
{
// ...
}
}
resourceIResourceThe resource with dependencies to wait for.
cancellationTokenCancellationTokenThe cancellation token.
TaskA task.
WaitForResourceAsync(string, string?, CancellationToken)Section titled WaitForResourceAsync(string, string?, CancellationToken)Task
Waits for a resource to reach the specified state. See KnownResourceStates for common states.
public class ResourceNotificationService
{
public Task WaitForResourceAsync(
string resourceName,
string? targetState = null,
CancellationToken cancellationToken = default(CancellationToken))
{
// ...
}
}
resourceNamestringThe name of the resource.
targetStatestring?optionalThe state to wait for the resource to transition to. See KnownResourceStates for common states.
cancellationTokenCancellationTokenoptionalA Threading.CancellationToken.
TaskA Tasks.Task representing the wait operation.
This method returns a task that will complete when the resource reaches the specified target state. If the resource is already in the target state, the method will return immediately. If the resource doesn't reach one of the target states before cancellationToken is signaled, this method will throw OperationCanceledException.
WaitForResourceAsync(string, IEnumerable<string>, CancellationToken)Section titled WaitForResourceAsync(string, IEnumerable<string>, CancellationToken)Task<string>
Waits for a resource to reach one of the specified states. See KnownResourceStates for common states.
public class ResourceNotificationService
{
public Task<string> WaitForResourceAsync(
string resourceName,
IEnumerable<string> targetStates,
CancellationToken cancellationToken = default(CancellationToken))
{
// ...
}
}
resourceNamestringThe name of the resource.
targetStatesIEnumerable<string>The set of states to wait for the resource to transition to one of. See KnownResourceStates for common states.
cancellationTokenCancellationTokenoptionalA cancellation token that cancels the wait operation when signaled.
Task<string>A Tasks.Task`1 representing the wait operation and which of the target states the resource reached.
This method returns a task that will complete when the resource reaches one of the specified target states. If the resource is already in the target state, the method will return immediately. If the resource doesn't reach one of the target states before cancellationToken is signaled, this method will throw OperationCanceledException.
WaitForResourceAsync(string, Func<ResourceEvent, bool>, CancellationToken)Section titled WaitForResourceAsync(string, Func<ResourceEvent, bool>, CancellationToken)Task<ResourceEvent>
Waits until a resource satisfies the specified predicate.
public class ResourceNotificationService
{
public Task<ResourceEvent> WaitForResourceAsync(
string resourceName,
Func<ResourceEvent, bool> predicate,
CancellationToken cancellationToken = default(CancellationToken))
{
// ...
}
}
resourceNamestringThe name of the resource.
predicateFunc<ResourceEvent, bool>A predicate which is evaluated for each ResourceEvent for the selected resource.
cancellationTokenCancellationTokenoptionalA cancellation token that cancels the wait operation when signaled.
Task<ResourceEvent>A Tasks.Task`1 representing the wait operation and which of the target states the resource reached.
This method returns a task that will complete when the specified predicate returns true. If the predicate isn't satisfied before cancellationToken is signaled, this method will throw OperationCanceledException.
Waits for a resource to become healthy.
public class ResourceNotificationService
{
public Task<ResourceEvent> WaitForResourceHealthyAsync(
string resourceName,
CancellationToken cancellationToken = default(CancellationToken))
{
// ...
}
}
resourceNamestringThe name of the resource.
cancellationTokenCancellationTokenoptionalThe cancellation token.

This method returns a task that will complete with the resource is healthy. A resource without HealthCheckAnnotation annotations will be considered healthy once it reaches a KnownResourceStates.Running state.

If the resource enters an unavailable state such as KnownResourceStates.FailedToStart then this method will continue to wait to enable scenarios where a resource could be restarted and recover. To control this behavior use ResourceNotificationService.WaitForResourceHealthyAsync or configure the default behavior with ResourceNotificationServiceOptions.DefaultWaitBehavior.

WaitForResourceHealthyAsync(string, WaitBehavior, CancellationToken)Section titled WaitForResourceHealthyAsync(string, WaitBehavior, CancellationToken)Task<ResourceEvent>
Waits for a resource to become healthy.
public class ResourceNotificationService
{
public Task<ResourceEvent> WaitForResourceHealthyAsync(
string resourceName,
WaitBehavior waitBehavior,
CancellationToken cancellationToken = default(CancellationToken))
{
// ...
}
}
resourceNamestringThe name of the resource.
waitBehaviorWaitBehaviorThe wait behavior.
cancellationTokenCancellationTokenoptionalThe cancellation token.

This method returns a task that will complete with the resource is healthy. A resource without HealthCheckAnnotation annotations will be considered healthy once it reaches a KnownResourceStates.Running state. The WaitBehavior controls how the wait operation behaves when the resource enters an unavailable state such as KnownResourceStates.FailedToStart.

When WaitBehavior.WaitOnResourceUnavailable is specified the wait operation will continue to wait until the resource reaches a KnownResourceStates.Running state.

When WaitBehavior.StopOnResourceUnavailable is specified the wait operation will throw a DistributedApplicationException if the resource enters an unavailable state.

Watch for changes to the state for all resources.
public class ResourceNotificationService
{
public IAsyncEnumerable<ResourceEvent> WatchAsync(
CancellationToken cancellationToken = default(CancellationToken))
{
// ...
}
}
cancellationTokenCancellationTokenoptional