# ResourceNotificationService Methods

- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Type: [ResourceNotificationService](/reference/api/csharp/aspire.hosting/resourcenotificationservice.md)
- Kind: `Methods`
- Members: `11`

A service that allows publishing and subscribing to changes in the state of a resource.

## Dispose

- Name: `Dispose`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs#L948-L949)

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

```csharp
public class ResourceNotificationService
{
    public void Dispose()
    {
        // ...
    }
}
```

## PublishUpdateAsync(IResource, string, Func<CustomResourceSnapshot, CustomResourceSnapshot>)

- Name: `PublishUpdateAsync(IResource, string, Func<CustomResourceSnapshot, CustomResourceSnapshot>)`
- Returns: `Task`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs#L639-L731)

Updates the snapshot of the [CustomResourceSnapshot](/reference/api/csharp/aspire.hosting/customresourcesnapshot.md) for a resource.

```csharp
public class ResourceNotificationService
{
    public Task PublishUpdateAsync(
        IResource resource,
        string resourceId,
        Func<CustomResourceSnapshot, CustomResourceSnapshot> stateFactory)
    {
        // ...
    }
}
```

## Parameters

- `resource` ([IResource](/reference/api/csharp/aspire.hosting/iresource.md))
  The resource to update
- `resourceId` (`string`)
  The id of the resource.
- `stateFactory` (`Func<CustomResourceSnapshot, CustomResourceSnapshot>`)
  A factory that creates the new state based on the previous state.

## PublishUpdateAsync(IResource, Func<CustomResourceSnapshot, CustomResourceSnapshot>)

- Name: `PublishUpdateAsync(IResource, Func<CustomResourceSnapshot, CustomResourceSnapshot>)`
- Returns: `Task`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs#L639-L731)

Updates the snapshot of the [CustomResourceSnapshot](/reference/api/csharp/aspire.hosting/customresourcesnapshot.md) for a resource.

```csharp
public class ResourceNotificationService
{
    public Task PublishUpdateAsync(
        IResource resource,
        Func<CustomResourceSnapshot, CustomResourceSnapshot> stateFactory)
    {
        // ...
    }
}
```

## Parameters

- `resource` ([IResource](/reference/api/csharp/aspire.hosting/iresource.md))
  The resource to update
- `stateFactory` (`Func<CustomResourceSnapshot, CustomResourceSnapshot>`)
  A factory that creates the new state based on the previous state.

## TryGetCurrentState(string, ResourceEvent?)

- Name: `TryGetCurrentState(string, ResourceEvent?)`
- Returns: `bool`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs#L535-L568)

Attempts to retrieve the current state of a resource by resourceId.

```csharp
public class ResourceNotificationService
{
    public bool TryGetCurrentState(
        string resourceId,
        out ResourceEvent? resourceEvent)
    {
        // ...
    }
}
```

## Parameters

- `resourceId` (`string`)
  The 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).
- `resourceEvent` ([ResourceEvent?](/reference/api/csharp/aspire.hosting/resourceevent.md))
  When this method returns, contains the [ResourceEvent](/reference/api/csharp/aspire.hosting/resourceevent.md) for the specified resource id, if found; otherwise, `null`.

## Returns

`bool` -- `true` if specified resource id was found; otherwise, `false`.

## Remarks

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)

- Name: `WaitForDependenciesAsync(IResource, CancellationToken)`
- Returns: `Task`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs#L434-L459)

Waits for all dependencies of the resource to be ready.

```csharp
public class ResourceNotificationService
{
    public Task WaitForDependenciesAsync(
        IResource resource,
        CancellationToken cancellationToken)
    {
        // ...
    }
}
```

## Parameters

- `resource` ([IResource](/reference/api/csharp/aspire.hosting/iresource.md))
  The resource with dependencies to wait for.
- `cancellationToken` (`CancellationToken`)
  The cancellation token.

## Returns

`Task` -- A task.

## Exceptions

- [DistributedApplicationException](/reference/api/csharp/aspire.hosting/distributedapplicationexception.md)

## WaitForResourceAsync(string, string?, CancellationToken)

- Name: `WaitForResourceAsync(string, string?, CancellationToken)`
- Returns: `Task`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs#L102-L103)

Waits for a resource to reach the specified state. See [KnownResourceStates](/reference/api/csharp/aspire.hosting/knownresourcestates.md) for common states.

```csharp
public class ResourceNotificationService
{
    public Task WaitForResourceAsync(
        string resourceName,
        string? targetState = null,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `resourceName` (`string`)
  The name of the resource.
- `targetState` (`string?`) `optional`
  The state to wait for the resource to transition to. See [KnownResourceStates](/reference/api/csharp/aspire.hosting/knownresourcestates.md) for common states.
- `cancellationToken` (`CancellationToken`) `optional`
  A `Threading.CancellationToken`.

## Returns

`Task` -- A `Tasks.Task` representing the wait operation.

## Remarks

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)

- Name: `WaitForResourceAsync(string, IEnumerable<string>, CancellationToken)`
- Returns: `Task<string>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs#L102-L103)

Waits for a resource to reach one of the specified states. See [KnownResourceStates](/reference/api/csharp/aspire.hosting/knownresourcestates.md) for common states.

```csharp
public class ResourceNotificationService
{
    public Task<string> WaitForResourceAsync(
        string resourceName,
        IEnumerable<string> targetStates,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `resourceName` (`string`)
  The name of the resource.
- `targetStates` (`IEnumerable<string>`)
  The set of states to wait for the resource to transition to one of. See [KnownResourceStates](/reference/api/csharp/aspire.hosting/knownresourcestates.md) for common states.
- `cancellationToken` (`CancellationToken`) `optional`
  A cancellation token that cancels the wait operation when signaled.

## Returns

`Task<string>` -- A `Tasks.Task`1` representing the wait operation and which of the target states the resource reached.

## Remarks

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)

- Name: `WaitForResourceAsync(string, Func<ResourceEvent, bool>, CancellationToken)`
- Returns: [Task<ResourceEvent>](/reference/api/csharp/aspire.hosting/resourceevent.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs#L102-L103)

Waits until a resource satisfies the specified predicate.

```csharp
public class ResourceNotificationService
{
    public Task<ResourceEvent> WaitForResourceAsync(
        string resourceName,
        Func<ResourceEvent, bool> predicate,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `resourceName` (`string`)
  The name of the resource.
- `predicate` (`Func<ResourceEvent, bool>`)
  A predicate which is evaluated for each [ResourceEvent](/reference/api/csharp/aspire.hosting/resourceevent.md) for the selected resource.
- `cancellationToken` (`CancellationToken`) `optional`
  A cancellation token that cancels the wait operation when signaled.

## Returns

[Task<ResourceEvent>](/reference/api/csharp/aspire.hosting/resourceevent.md) -- A `Tasks.Task`1` representing the wait operation and which of the target states the resource reached.

## Remarks

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`.

## WaitForResourceHealthyAsync(string, CancellationToken)

- Name: `WaitForResourceHealthyAsync(string, CancellationToken)`
- Returns: [Task<ResourceEvent>](/reference/api/csharp/aspire.hosting/resourceevent.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs)

Waits for a resource to become healthy.

```csharp
public class ResourceNotificationService
{
    public Task<ResourceEvent> WaitForResourceHealthyAsync(
        string resourceName,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `resourceName` (`string`)
  The name of the resource.
- `cancellationToken` (`CancellationToken`) `optional`
  The cancellation token.

## Returns

[Task<ResourceEvent>](/reference/api/csharp/aspire.hosting/resourceevent.md) -- A task.

## Remarks

This method returns a task that will complete with the resource is healthy. A resource without [HealthCheckAnnotation](/reference/api/csharp/aspire.hosting/healthcheckannotation.md) annotations will be considered healthy once it reaches a [KnownResourceStates.Running](/reference/api/csharp/aspire.hosting/knownresourcestates/fields.md#running) state.

If the resource enters an unavailable state such as [KnownResourceStates.FailedToStart](/reference/api/csharp/aspire.hosting/knownresourcestates/fields.md#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(string, CancellationToken)](/reference/api/csharp/aspire.hosting/resourcenotificationservice/methods.md#waitforresourcehealthyasync-string-cancellationtoken) or configure the default behavior with [ResourceNotificationServiceOptions.DefaultWaitBehavior](/reference/api/csharp/aspire.hosting/resourcenotificationserviceoptions/properties.md#defaultwaitbehavior).

## WaitForResourceHealthyAsync(string, WaitBehavior, CancellationToken)

- Name: `WaitForResourceHealthyAsync(string, WaitBehavior, CancellationToken)`
- Returns: [Task<ResourceEvent>](/reference/api/csharp/aspire.hosting/resourceevent.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs)

Waits for a resource to become healthy.

```csharp
public class ResourceNotificationService
{
    public Task<ResourceEvent> WaitForResourceHealthyAsync(
        string resourceName,
        WaitBehavior waitBehavior,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `resourceName` (`string`)
  The name of the resource.
- `waitBehavior` ([WaitBehavior](/reference/api/csharp/aspire.hosting/waitbehavior.md))
  The wait behavior.
- `cancellationToken` (`CancellationToken`) `optional`
  The cancellation token.

## Returns

[Task<ResourceEvent>](/reference/api/csharp/aspire.hosting/resourceevent.md) -- A task.

## Remarks

This method returns a task that will complete with the resource is healthy. A resource without [HealthCheckAnnotation](/reference/api/csharp/aspire.hosting/healthcheckannotation.md) annotations will be considered healthy once it reaches a [KnownResourceStates.Running](/reference/api/csharp/aspire.hosting/knownresourcestates/fields.md#running) state. The [WaitBehavior](/reference/api/csharp/aspire.hosting/waitbehavior.md) controls how the wait operation behaves when the resource enters an unavailable state such as [KnownResourceStates.FailedToStart](/reference/api/csharp/aspire.hosting/knownresourcestates/fields.md#failedtostart).

When [WaitBehavior.WaitOnResourceUnavailable](/reference/api/csharp/aspire.hosting/waitbehavior/fields.md) is specified the wait operation will continue to wait until the resource reaches a [KnownResourceStates.Running](/reference/api/csharp/aspire.hosting/knownresourcestates/fields.md#running) state.

When [WaitBehavior.StopOnResourceUnavailable](/reference/api/csharp/aspire.hosting/waitbehavior/fields.md) is specified the wait operation will throw a [DistributedApplicationException](/reference/api/csharp/aspire.hosting/distributedapplicationexception.md) if the resource enters an unavailable state.

## WatchAsync(CancellationToken)

- Name: `WatchAsync(CancellationToken)`
- Returns: [IAsyncEnumerable<ResourceEvent>](/reference/api/csharp/aspire.hosting/resourceevent.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs#L576-L629)

Watch for changes to the state for all resources.

```csharp
public class ResourceNotificationService
{
    public IAsyncEnumerable<ResourceEvent> WatchAsync(
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `cancellationToken` (`CancellationToken`) `optional`
