# ResourceCommandService Methods

- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Type: [ResourceCommandService](/reference/api/csharp/aspire.hosting/resourcecommandservice.md)
- Kind: `Methods`
- Members: `2`

A service to execute resource commands.

## ExecuteCommandAsync(string, string, CancellationToken)

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

Execute a command for the specified resource.

```csharp
public class ResourceCommandService
{
    public Task<ExecuteCommandResult> ExecuteCommandAsync(
        string resourceId,
        string commandName,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## 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).
- `commandName` (`string`)
  The command name.
- `cancellationToken` (`CancellationToken`) `optional`
  The cancellation token.

## Returns

[Task<ExecuteCommandResult>](/reference/api/csharp/aspire.hosting/executecommandresult.md) -- The [ExecuteCommandResult](/reference/api/csharp/aspire.hosting/executecommandresult.md) indicates command success or failure.

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

## ExecuteCommandAsync(IResource, string, CancellationToken)

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

Execute a command for the specified resource.

```csharp
public class ResourceCommandService
{
    public Task<ExecuteCommandResult> ExecuteCommandAsync(
        IResource resource,
        string commandName,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `resource` ([IResource](/reference/api/csharp/aspire.hosting/iresource.md))
  The resource. If the resource has multiple instances, such as replicas, then the command will be executed for each instance.
- `commandName` (`string`)
  The command name.
- `cancellationToken` (`CancellationToken`) `optional`
  The cancellation token.

## Returns

[Task<ExecuteCommandResult>](/reference/api/csharp/aspire.hosting/executecommandresult.md) -- The [ExecuteCommandResult](/reference/api/csharp/aspire.hosting/executecommandresult.md) indicates command success or failure.
