# DistributedApplicationBuilderExtensions Methods

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

Extensions for [IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md).

## CreateResourceBuilder(IDistributedApplicationBuilder, string)

- Name: `CreateResourceBuilder(IDistributedApplicationBuilder, 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/DistributedApplicationBuilderExtensions.cs#L65-L78)

Creates a new resource builder based on the name of an existing resource.

```csharp
public static class DistributedApplicationBuilderExtensions
{
    public static IResourceBuilder<T> CreateResourceBuilder<T>(
        this IDistributedApplicationBuilder builder,
        string name)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md))
  The distributed application builder.
- `name` (`string`)
  The name of an existing resource.

## Returns

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

## Remarks

This method is not available in polyglot app hosts.

The [DistributedApplicationBuilderExtensions.CreateResourceBuilder(IDistributedApplicationBuilder, string)](/reference/api/csharp/aspire.hosting/distributedapplicationbuilderextensions/methods.md#createresourcebuilder-idistributedapplicationbuilder-string) method is used to create an `ApplicationModel.IResourceBuilder`1` for a specific resource where the original resource builder cannot be referenced. This does not create a new resource, but instead returns a resource builder for an existing resource based on its name.

This method is typically used when testing Aspire applications where the original resource builder cannot be referenced directly. Using the [DistributedApplicationBuilderExtensions.CreateResourceBuilder(IDistributedApplicationBuilder, string)](/reference/api/csharp/aspire.hosting/distributedapplicationbuilderextensions/methods.md#createresourcebuilder-idistributedapplicationbuilder-string) method allows for easier mutation of resources within the test scenario.

In this example, the MyAspireApp.AppHost project has previously added a Redis resource named "cache" to the application host. The test project, MyAspireApp.AppHost.Tests, modifies that resource so that it sleeps instead of starting the Redis container. This allows the test case to verify that the application's health check returns an 'Unhealthy' status when the Redis resource is not available.

```csharp
[Fact]
public async Task GetWebResourceHealthReturnsUnhealthyWhenRedisUnavailable()
{
    // Arrange
    var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.MyAspireApp_AppHost>();

    // Get the "cache" resource and modify it to sleep for 1 day instead of starting Redis.
    var redis = appHost.CreateResourceBuilder<ContainerResource>("cache"));
    redis.WithEntrypoint("sleep 1d");

    await using var app = await appHost.BuildAsync();
    await app.StartAsync();

    // Act
    var httpClient = new HttpClient { BaseAddress = app.GetEndpoint("webfrontend") };
    var response = await httpClient.GetAsync("/health");

    // Assert
    Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
    Assert.Equal("Unhealthy", await response.Content.ReadAsStringAsync());
}
```

## ATS metadata

### Ignored by ATS

- Reason: Takes raw IResource generic constraint -- low-level builder API for testing scenarios.

## TryCreateResourceBuilder(IDistributedApplicationBuilder, string, IResourceBuilder<T>)

- Name: `TryCreateResourceBuilder(IDistributedApplicationBuilder, string, IResourceBuilder<T>)`
- Modifiers: `extension`
- Returns: `bool`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/DistributedApplicationBuilderExtensions.cs#L97-L108)

Attempts to create a new resource builder based on the name of an existing resource.

```csharp
public static class DistributedApplicationBuilderExtensions
{
    public static bool TryCreateResourceBuilder<T>(
        this IDistributedApplicationBuilder builder,
        string name,
        out IResourceBuilder<T>? resourceBuilder)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md))
  The distributed application builder.
- `name` (`string`)
  The name of an existing resource.
- `resourceBuilder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  When this method returns, contains the resource builder if the resource was found and is of the correct type; otherwise, `null`.

## Returns

`bool` -- `true` if the resource was found and is of the correct type; otherwise, `false`.

## Remarks

This method is not available in polyglot app hosts.

This method is similar to [DistributedApplicationBuilderExtensions.CreateResourceBuilder(IDistributedApplicationBuilder, string)](/reference/api/csharp/aspire.hosting/distributedapplicationbuilderextensions/methods.md#createresourcebuilder-idistributedapplicationbuilder-string) but returns `false` instead of throwing an exception when the resource is not found or is not of the correct type.

## ATS metadata

### Ignored by ATS

- Reason: Uses out parameter which is not ATS-compatible.
