# RequiredCommandResourceExtensions Methods

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

Provides extension methods for adding required command annotations to resources.

## WithRequiredCommand(IResourceBuilder<T>, string, string?)

- Name: `WithRequiredCommand(IResourceBuilder<T>, string, string?)`
- Modifiers: `extension`
- Returns: [IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/RequiredCommandResourceExtensions.cs#L34-L44)

Declares that a resource requires a specific command/executable to be available on the local machine PATH before it can start.

```csharp
public static class RequiredCommandResourceExtensions
{
    public static IResourceBuilder<T> WithRequiredCommand<T>(
        this IResourceBuilder<T> builder,
        string command,
        string? helpLink = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  The resource builder.
- `command` (`string`)
  The command string (file name or path) that should be validated.
- `helpLink` (`string?`) `optional`
  An optional help link URL to guide users when the command is missing.

## Returns

[IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md) -- The resource builder.

## Remarks

The command is considered valid if either: 1. It is an absolute or relative path (contains a directory separator) that points to an existing file, or 2. It is discoverable on the current process PATH (respecting PATHEXT on Windows). If the command is not found, a warning message will be logged but the resource will be allowed to attempt to start.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithRequiredCommand(IResourceBuilder<T>, string, Func<RequiredCommandValidationContext, Task<RequiredCommandValidationResult>>, string?)

> **Experimental:** ASPIRECOMMAND001 - [Learn more](/diagnostics/aspirecommand001/)

- Name: `WithRequiredCommand(IResourceBuilder<T>, string, Func<RequiredCommandValidationContext, Task<RequiredCommandValidationResult>>, string?)`
- Modifiers: `extension`
- Returns: [IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/RequiredCommandResourceExtensions.cs#L71-L81)

Declares that a resource requires a specific command/executable to be available on the local machine PATH before it can start, with custom validation logic.

```csharp
public static class RequiredCommandResourceExtensions
{
    public static IResourceBuilder<T> WithRequiredCommand<T>(
        this IResourceBuilder<T> builder,
        string command,
        Func<RequiredCommandValidationContext, Task<RequiredCommandValidationResult>> validationCallback,
        string? helpLink = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  The resource builder.
- `command` (`string`)
  The command string (file name or path) that should be validated.
- `validationCallback` (`Func<RequiredCommandValidationContext, Task<RequiredCommandValidationResult>>`)
  A callback that validates the resolved command path. Receives a [RequiredCommandValidationContext](/reference/api/csharp/aspire.hosting/requiredcommandvalidationcontext.md) and returns a [RequiredCommandValidationResult](/reference/api/csharp/aspire.hosting/requiredcommandvalidationresult.md).
- `helpLink` (`string?`) `optional`
  An optional help link URL to guide users when the command is missing or fails validation.

## Returns

[IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md) -- The resource builder.

## Remarks

This method is not available in polyglot app hosts. Use the overload without validation callback instead.

The command is first resolved to a full path. If found, the validation callback is invoked with the context containing the resolved path and service provider. The callback should return a [RequiredCommandValidationResult](/reference/api/csharp/aspire.hosting/requiredcommandvalidationresult.md) indicating whether the command is valid. If the command is not found or fails validation, a warning message will be logged but the resource will be allowed to attempt to start.

## ATS metadata

### Ignored by ATS

- Reason: RequiredCommandValidationContext exposes IServiceProvider -- not usable from polyglot hosts.
