# AzureContainerRegistryExtensions Methods

- Package: [Aspire.Hosting.Azure.ContainerRegistry](/reference/api/csharp/aspire.hosting.azure.containerregistry.md)
- Type: [AzureContainerRegistryExtensions](/reference/api/csharp/aspire.hosting.azure.containerregistry/azurecontainerregistryextensions.md)
- Kind: `Methods`
- Members: `5`

Provides extension methods for adding Azure Container Registry resources to the application model.

## AddAzureContainerRegistry(IDistributedApplicationBuilder, string)

- Name: `AddAzureContainerRegistry(IDistributedApplicationBuilder, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureContainerRegistryResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryExtensions.cs#L35-L57)

Adds an Azure Container Registry resource to the application model.

```csharp
public static class AzureContainerRegistryExtensions
{
    public static IResourceBuilder<AzureContainerRegistryResource> AddAzureContainerRegistry(
        this IDistributedApplicationBuilder builder,
        string name)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The builder for the distributed application.
- `name` (`string`)
  The name of the resource.

## Returns

`IResourceBuilder<AzureContainerRegistryResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` builder.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` is null.
- `ArgumentException` -- Thrown when `name` is null or empty.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## GetAzureContainerRegistry(IResourceBuilder<T>)

- Name: `GetAzureContainerRegistry(IResourceBuilder<T>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureContainerRegistryResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryExtensions.cs#L115-L120)

Gets the [AzureContainerRegistryResource](/reference/api/csharp/aspire.hosting.azure.containerregistry/azurecontainerregistryresource.md) associated with the specified Azure compute environment resource.

```csharp
public static class AzureContainerRegistryExtensions
{
    public static IResourceBuilder<AzureContainerRegistryResource> GetAzureContainerRegistry<T>(
        this IResourceBuilder<T> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
  The resource builder for the compute environment resource.

## Returns

`IResourceBuilder<AzureContainerRegistryResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for the associated registry.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` is `null`.
- `InvalidOperationException` -- Thrown when the resource does not have an associated Azure Container Registry, or when the associated container registry is not an [AzureContainerRegistryResource](/reference/api/csharp/aspire.hosting.azure.containerregistry/azurecontainerregistryresource.md).

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithAzureContainerRegistry(IResourceBuilder<T>, IResourceBuilder<AzureContainerRegistryResource>)

- Name: `WithAzureContainerRegistry(IResourceBuilder<T>, IResourceBuilder<AzureContainerRegistryResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryExtensions.cs#L91-L97)

Configures a resource that implements `ApplicationModel.IContainerRegistry` to use the specified Azure Container Registry.

```csharp
public static class AzureContainerRegistryExtensions
{
    public static IResourceBuilder<T> WithAzureContainerRegistry<T>(
        this IResourceBuilder<T> builder,
        IResourceBuilder<AzureContainerRegistryResource> registryBuilder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
  The resource builder for a resource that implements `ApplicationModel.IContainerRegistry`.
- `registryBuilder` (`IResourceBuilder<AzureContainerRegistryResource>`)
  The resource builder for the [AzureContainerRegistryResource](/reference/api/csharp/aspire.hosting.azure.containerregistry/azurecontainerregistryresource.md) to use.

## Returns

`IResourceBuilder<T>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` or `registryBuilder` is null.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithPurgeTask(IResourceBuilder<AzureContainerRegistryResource>, string, string?, TimeSpan?, int, string?)

- Name: `WithPurgeTask(IResourceBuilder<AzureContainerRegistryResource>, string, string?, TimeSpan?, int, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureContainerRegistryResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryExtensions.cs#L161-L239)

Adds a scheduled ACR purge task to remove old or unused container images from the registry.

```csharp
public static class AzureContainerRegistryExtensions
{
    public static IResourceBuilder<AzureContainerRegistryResource> WithPurgeTask(
        this IResourceBuilder<AzureContainerRegistryResource> builder,
        string schedule,
        string? filter = null,
        TimeSpan? ago = null,
        int keep = 3,
        string? taskName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureContainerRegistryResource>`)
  The resource builder for the [AzureContainerRegistryResource](/reference/api/csharp/aspire.hosting.azure.containerregistry/azurecontainerregistryresource.md).
- `schedule` (`string`)
  The cron schedule for the purge task timer trigger. Must be a five-part cron expression ( `minute hour day-of-month month day-of-week`); seconds are not supported.
- `filter` (`string?`) `optional`
  An optional filter for the `acr purge --filter` parameter. Only repositories matching this filter will be purged. Defaults to `".*:.*"` (all repositories and tags) when `null`.
- `ago` (`TimeSpan?`) `optional`
  The age threshold for `acr purge --ago`. Images older than this duration will be considered for removal. Uses Go-style duration format (e.g., `2d3h6m`). Defaults to `0d` when `null`.
- `keep` (`int`) `optional`
  The number of most recent images to keep per repository, regardless of age. Must be greater than zero. Defaults to 3.
- `taskName` (`string?`) `optional`
  An optional name for the ACR task resource. If not provided, a name is auto-generated based on existing purge tasks to avoid conflicts. If a task with the specified name already exists, an `ArgumentException` is thrown.

## Returns

`IResourceBuilder<AzureContainerRegistryResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` is `null`.
- `ArgumentException` -- Thrown when `schedule` is `null`, empty, or whitespace, or when `taskName` conflicts with an existing task.
- `ArgumentOutOfRangeException` -- Thrown when `keep` is less than 1, or `ago` is negative or less than 1 minute (when non-zero).

## Examples

This example configures a daily purge task that removes images older than 7 days, keeping the 5 most recent:

```csharp
var acr = builder.AddAzureContainerRegistry("myregistry")
    .WithPurgeTask("0 1 * * *", ago: TimeSpan.FromDays(7), keep: 5);
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureContainerRegistryResource>, ContainerRegistryBuiltInRole[])

- Name: `WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureContainerRegistryResource>, ContainerRegistryBuiltInRole[])`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryExtensions.cs)

Adds role assignments to the specified Azure Container Registry resource.

```csharp
public static class AzureContainerRegistryExtensions
{
    public static IResourceBuilder<T> WithRoleAssignments<T>(
        this IResourceBuilder<T> builder,
        IResourceBuilder<AzureContainerRegistryResource> target,
        params ContainerRegistryBuiltInRole[] roles)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
  The resource builder for the resource that will have role assignments.
- `target` (`IResourceBuilder<AzureContainerRegistryResource>`)
  The target Azure Container Registry resource.
- `roles` (`ContainerRegistryBuiltInRole[]`)
  The roles to assign to the resource.

## Returns

`IResourceBuilder<T>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This overload is not available in polyglot app hosts. Use [AzureContainerRegistryExtensions.WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureContainerRegistryResource>, ContainerRegistryBuiltInRole[])](/reference/api/csharp/aspire.hosting.azure.containerregistry/azurecontainerregistryextensions/methods.md#withroleassignments-iresourcebuilder-t-iresourcebuilder-azurecontainerregistryresource-containerregistrybuiltinrole) instead.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.
