# DistributedApplicationHostingTestingExtensions Methods

- Package: [Aspire.Hosting.Testing](/reference/api/csharp/aspire.hosting.testing.md)
- Type: [DistributedApplicationHostingTestingExtensions](/reference/api/csharp/aspire.hosting.testing/distributedapplicationhostingtestingextensions.md)
- Kind: `Methods`
- Members: `4`

Extensions for working with `Hosting.DistributedApplication` in test code.

## CreateHttpClient(DistributedApplication, string, string?)

- Name: `CreateHttpClient(DistributedApplication, string, string?)`
- Modifiers: `extension`
- Returns: `HttpClient`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Testing/DistributedApplicationHostingTestingExtensions.cs#L26-L34)

Creates an `Http.HttpClient` configured to communicate with the specified resource.

```csharp
public static class DistributedApplicationHostingTestingExtensions
{
    public static HttpClient CreateHttpClient(
        this DistributedApplication app,
        string resourceName,
        string? endpointName = null)
    {
        // ...
    }
}
```

## Parameters

- `app` (`DistributedApplication`)
  The application.
- `resourceName` (`string`)
  The resourceName of the resource.
- `endpointName` (`string?`) `optional`
  The resourceName of the endpoint on the resource to communicate with.

## Returns

`HttpClient` -- The `Http.HttpClient`.

## Remarks

This method is not available in polyglot app hosts.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## GetConnectionStringAsync(DistributedApplication, string, CancellationToken)

- Name: `GetConnectionStringAsync(DistributedApplication, string, CancellationToken)`
- Modifiers: `extension`
- Returns: `ValueTask<string?>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Testing/DistributedApplicationHostingTestingExtensions.cs#L49-L58)

Gets the connection string for the specified resource.

```csharp
public static class DistributedApplicationHostingTestingExtensions
{
    public static ValueTask<string?> GetConnectionStringAsync(
        this DistributedApplication app,
        string resourceName,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `app` (`DistributedApplication`)
  The application.
- `resourceName` (`string`)
  The resource name.
- `cancellationToken` (`CancellationToken`) `optional`
  A `Threading.CancellationToken`.

## Returns

`ValueTask<string?>` -- The connection string for the specified resource.

## Exceptions

- `ArgumentException` -- The resource was not found or does not expose a connection string.

## Remarks

This overload is not available in polyglot app hosts. Use the exported overload without a cancellation token instead.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## GetEndpoint(DistributedApplication, string, string?)

- Name: `GetEndpoint(DistributedApplication, string, string?)`
- Modifiers: `extension`
- Returns: `Uri`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Testing/DistributedApplicationHostingTestingExtensions.cs#L86-L89)

Gets the endpoint for the specified resource.

```csharp
public static class DistributedApplicationHostingTestingExtensions
{
    public static Uri GetEndpoint(
        this DistributedApplication app,
        string resourceName,
        string? endpointName = null)
    {
        // ...
    }
}
```

## Parameters

- `app` (`DistributedApplication`)
  The application.
- `resourceName` (`string`)
  The resource name.
- `endpointName` (`string?`) `optional`
  The optional endpoint name. If none are specified, the single defined endpoint is returned.

## Returns

`Uri` -- A URI representation of the endpoint.

## Exceptions

- `ArgumentException` -- The resource was not found, no matching endpoint was found, or multiple endpoints were found.
- `InvalidOperationException` -- The resource has no endpoints.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## GetEndpointForNetwork(DistributedApplication, string, NetworkIdentifier?, string?)

- Name: `GetEndpointForNetwork(DistributedApplication, string, NetworkIdentifier?, string?)`
- Modifiers: `extension`
- Returns: `Uri`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.Testing/DistributedApplicationHostingTestingExtensions.cs#L106-L109)

Gets the endpoint for the specified resource.

```csharp
public static class DistributedApplicationHostingTestingExtensions
{
    public static Uri GetEndpointForNetwork(
        this DistributedApplication app,
        string resourceName,
        NetworkIdentifier? networkIdentifier,
        string? endpointName = null)
    {
        // ...
    }
}
```

## Parameters

- `app` (`DistributedApplication`)
  The application.
- `resourceName` (`string`)
  The resource name.
- `networkIdentifier` (`NetworkIdentifier?`)
  The optional network identifier. If none is specified, the default network is used.
- `endpointName` (`string?`) `optional`
  The optional endpoint name. If none are specified, the single defined endpoint is returned.

## Returns

`Uri` -- A URI representation of the endpoint.

## Exceptions

- `ArgumentException` -- The resource was not found, no matching endpoint was found, or multiple endpoints were found.
- `InvalidOperationException` -- The resource has no endpoints.

## Remarks

This overload is not available in polyglot app hosts. Use the exported overload that accepts a network identifier string instead.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.
