# AzureProvisioningResource Methods

- Package: [Aspire.Hosting.Azure](/reference/api/csharp/aspire.hosting.azure.md)
- Type: [AzureProvisioningResource](/reference/api/csharp/aspire.hosting.azure/azureprovisioningresource.md)
- Kind: `Methods`
- Members: `6`

An Aspire resource that supports use of Azure Provisioning APIs to create Azure resources.

## AddAsExistingResource(AzureResourceInfrastructure)

- Name: `AddAsExistingResource(AzureResourceInfrastructure)`
- Modifiers: `virtual`
- Returns: `ProvisionableResource`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure/AzureProvisioningResource.cs#L38)

Adds a new `Primitives.ProvisionableResource` into `infra`. The new resource represents a reference to the current [AzureProvisioningResource](/reference/api/csharp/aspire.hosting.azure/azureprovisioningresource.md) via https://learn.microsoft.com/azure/azure-resource-manager/bicep/existing-resource.

```csharp
public class AzureProvisioningResource
{
    public virtual ProvisionableResource AddAsExistingResource(
        AzureResourceInfrastructure infra)
    {
        // ...
    }
}
```

## Parameters

- `infra` ([AzureResourceInfrastructure](/reference/api/csharp/aspire.hosting.azure/azureresourceinfrastructure.md))
  The [AzureResourceInfrastructure](/reference/api/csharp/aspire.hosting.azure/azureresourceinfrastructure.md) to add the existing resource into.

## Returns

`ProvisionableResource` -- A new `Primitives.ProvisionableResource`, typically using the FromExisting method on the derived `Primitives.ProvisionableResource` class.

## AddRoleAssignments(IAddRoleAssignmentsContext)

- Name: `AddRoleAssignments(IAddRoleAssignmentsContext)`
- Modifiers: `virtual`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure/AzureProvisioningResource.cs#L46-L61)

Adds role assignments to this Azure resource.

```csharp
public class AzureProvisioningResource
{
    public virtual void AddRoleAssignments(
        IAddRoleAssignmentsContext roleAssignmentContext)
    {
        // ...
    }
}
```

## Parameters

- `roleAssignmentContext` ([IAddRoleAssignmentsContext](/reference/api/csharp/aspire.hosting.azure/iaddroleassignmentscontext.md))
  The context containing information about the role assignments and what principal to use.

## CreateExistingOrNewProvisionableResource(AzureResourceInfrastructure, Func<string, BicepValue<string>, T>, Func<AzureResourceInfrastructure, T>)

- Name: `CreateExistingOrNewProvisionableResource(AzureResourceInfrastructure, Func<string, BicepValue<string>, T>, Func<AzureResourceInfrastructure, T>)`
- Modifiers: `static`
- Returns: `T`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure/AzureProvisioningResource.cs#L128-L151)

Encapsulates the logic for creating an existing or new `Primitives.ProvisionableResource` based on whether or not the [ExistingAzureResourceAnnotation](/reference/api/csharp/aspire.hosting.azure/existingazureresourceannotation.md) exists on the resource.

```csharp
public class AzureProvisioningResource
{
    public static T CreateExistingOrNewProvisionableResource<T>(
        AzureResourceInfrastructure infrastructure,
        Func<string, BicepValue<string>, T> createExisting,
        Func<AzureResourceInfrastructure, T> createNew)
    {
        // ...
    }
}
```

## Parameters

- `infrastructure` ([AzureResourceInfrastructure](/reference/api/csharp/aspire.hosting.azure/azureresourceinfrastructure.md))
  The [AzureResourceInfrastructure](/reference/api/csharp/aspire.hosting.azure/azureresourceinfrastructure.md) that will contain the `Primitives.ProvisionableResource`.
- `createExisting` (`Func<string, BicepValue<string>, T>`)
  A callback to create the existing resource.
- `createNew` (`Func<AzureResourceInfrastructure, T>`)
  A callback to create the new resource.

## Returns

`T` -- The provisioned resource.

## GetBicepTemplateFile(string?, bool)

- Name: `GetBicepTemplateFile(string?, bool)`
- Modifiers: `override`
- Returns: [BicepTemplateFile](/reference/api/csharp/aspire.hosting.azure/biceptemplatefile.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure/AzureProvisioningResource.cs#L81-L99)

Gets the path to the bicep file. If the template is a string or embedded resource, it will be written to a temporary file.

```csharp
public class AzureProvisioningResource
{
    public override BicepTemplateFile GetBicepTemplateFile(
        string? directory = null,
        bool deleteTemporaryFileOnDispose = true)
    {
        // ...
    }
}
```

## Parameters

- `directory` (`string?`) `optional`
  The directory where the bicep file will be written to (if it's a temporary file)
- `deleteTemporaryFileOnDispose` (`bool`) `optional`
  A boolean that determines if the file should be deleted on disposal of the [BicepTemplateFile](/reference/api/csharp/aspire.hosting.azure/biceptemplatefile.md).

## Returns

[BicepTemplateFile](/reference/api/csharp/aspire.hosting.azure/biceptemplatefile.md) -- A [BicepTemplateFile](/reference/api/csharp/aspire.hosting.azure/biceptemplatefile.md) that represents the bicep file.

## Exceptions

- `InvalidOperationException`

## GetBicepTemplateString

- Name: `GetBicepTemplateString`
- Modifiers: `override`
- Returns: `string`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure/AzureProvisioningResource.cs#L107-L113)

Get the bicep template as a string. Does not write to disk.

```csharp
public class AzureProvisioningResource
{
    public override string GetBicepTemplateString()
    {
        // ...
    }
}
```

## TryApplyExistingResourceAnnotation(IAzureResource, AzureResourceInfrastructure, ProvisionableResource)

- Name: `TryApplyExistingResourceAnnotation(IAzureResource, AzureResourceInfrastructure, ProvisionableResource)`
- Modifiers: `static`
- Returns: `bool`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure/AzureProvisioningResource.cs#L169-L225)

Attempts to apply the name and (optionally) the resource group scope for the `Primitives.ProvisionableResource` from an [ExistingAzureResourceAnnotation](/reference/api/csharp/aspire.hosting.azure/existingazureresourceannotation.md) attached to `aspireResource`.

```csharp
public class AzureProvisioningResource
{
    public static bool TryApplyExistingResourceAnnotation(
        IAzureResource aspireResource,
        AzureResourceInfrastructure infra,
        ProvisionableResource provisionableResource)
    {
        // ...
    }
}
```

## Parameters

- `aspireResource` ([IAzureResource](/reference/api/csharp/aspire.hosting.azure/iazureresource.md))
  The Aspire resource that may have an [ExistingAzureResourceAnnotation](/reference/api/csharp/aspire.hosting.azure/existingazureresourceannotation.md).
- `infra` ([AzureResourceInfrastructure](/reference/api/csharp/aspire.hosting.azure/azureresourceinfrastructure.md))
  The infrastructure used for converting parameters into provisioning expressions.
- `provisionableResource` (`ProvisionableResource`)
  The `Primitives.ProvisionableResource` resource to configure.

## Returns

`bool` -- `true` if an [ExistingAzureResourceAnnotation](/reference/api/csharp/aspire.hosting.azure/existingazureresourceannotation.md) was present and applied; otherwise `false`.

## Remarks

When the annotation includes a resource group, a synthetic `scope` property is added to the resource's provisionable properties to correctly scope the existing resource in the generated Bicep. The caller is responsible for setting a generated name when the method returns `false`.
