# DistributedApplication

- Kind: `class`
- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Version: `13.3.0`
- Namespace: `Aspire.Hosting`
- Target framework: `net8.0`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/DistributedApplication.cs)
- Implements: `IHost`, `IAsyncDisposable`, `IDisposable`

Represents a distributed application that implements the `Hosting.IHost` and `IAsyncDisposable` interfaces.

## Definition

```csharp
namespace Aspire.Hosting;

public class DistributedApplication
    : Microsoft.Extensions.Hosting.IHost,
      System.IAsyncDisposable,
      System.IDisposable
{
    // ...
}
```

## ATS metadata

### ATS export

- Type ID: `Aspire.Hosting/DistributedApplication`

## Remarks

The [DistributedApplication](/reference/api/csharp/aspire.hosting/distributedapplication.md) is an implementation of the `Hosting.IHost` interface that orchestrates an Aspire application. To build an instance of the [DistributedApplication](/reference/api/csharp/aspire.hosting/distributedapplication.md) class, use the [DistributedApplication.CreateBuilder](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#createbuilder) method to create an instance of the [IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md) interface. Using the [IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md) interface you can configure the resources that comprise the distributed application and describe the dependencies between them.

Once the distributed application has been defined use the [IDistributedApplicationBuilder.Build](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder/methods.md#build) method to create an instance of the [DistributedApplication](/reference/api/csharp/aspire.hosting/distributedapplication.md) class. The [DistributedApplication](/reference/api/csharp/aspire.hosting/distributedapplication.md) class exposes a [DistributedApplication.Run](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#run) method which then starts the distributed application and its resources.

The [DistributedApplication.CreateBuilder(DistributedApplicationOptions)](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#createbuilder-distributedapplicationoptions) method provides additional options for constructing the [IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md) including disabling the Aspire dashboard (see [DistributedApplicationOptions.DisableDashboard](/reference/api/csharp/aspire.hosting/distributedapplicationoptions/properties.md#disabledashboard)) or allowing unsecured communication between the browser and dashboard, and dashboard and app host (see [DistributedApplicationOptions.AllowUnsecuredTransport](/reference/api/csharp/aspire.hosting/distributedapplicationoptions/properties.md#allowunsecuredtransport).

The following example shows creating a PostgreSQL server resource with a database and referencing that database in a .NET project.

```csharp
var builder = DistributedApplication.CreateBuilder(args);
var inventoryDatabase = builder.AddPostgres("mypostgres").AddDatabase("inventory");
builder.AddProject<Projects.InventoryService>()
       .WithReference(inventoryDatabase);

builder.Build().Run();
```

## Constructors

- [DistributedApplication(IHost)](/reference/api/csharp/aspire.hosting/distributedapplication/constructors.md#constructor-ihost) -- Initializes a new instance of the [DistributedApplication](/reference/api/csharp/aspire.hosting/distributedapplication.md) class.

## Properties

- [ResourceCommands](/reference/api/csharp/aspire.hosting/distributedapplication/properties.md#resourcecommands) : [ResourceCommandService](/reference/api/csharp/aspire.hosting/resourcecommandservice.md) `get` -- Gets the service for executing resource commands.
- [ResourceNotifications](/reference/api/csharp/aspire.hosting/distributedapplication/properties.md#resourcenotifications) : [ResourceNotificationService](/reference/api/csharp/aspire.hosting/resourcenotificationservice.md) `get` -- Gets the service for monitoring and responding to resource state changes in the distributed application.
- [Services](/reference/api/csharp/aspire.hosting/distributedapplication/properties.md#services) : `IServiceProvider` `get` -- Gets the `IServiceProvider` instance configured for the application.

## Methods

- [CreateBuilder](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#createbuilder) : [IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md) `static` -- Creates a new instance of the [IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md) interface.
- [CreateBuilder(string[])](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#createbuilder-string) : [IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md) `static` `ats ignored` -- Creates a new instance of [IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md) with the specified command-line arguments.
- [CreateBuilder(DistributedApplicationOptions)](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#createbuilder-distributedapplicationoptions) : [IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md) `static` -- Creates a new instance of the [IDistributedApplicationBuilder](/reference/api/csharp/aspire.hosting/idistributedapplicationbuilder.md) interface with the specified `options`.
- [Dispose](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#dispose) `virtual` -- Disposes the distributed application by disposing the `Hosting.IHost`.
- [DisposeAsync](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#disposeasync) : `ValueTask` `virtual` -- Asynchronously disposes the distributed application by disposing the `Hosting.IHost`.
- [Run](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#run) -- Runs an application and blocks the calling thread until host shutdown is triggered and all `Hosting.IHostedService` instances are stopped.
- [RunAsync(CancellationToken)](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#runasync-cancellationtoken) : `Task` `virtual` `ats export` -- Runs an application and returns a Task that only completes when the token is triggered or shutdown is triggered and all `Hosting.IHostedService` instances are stopped.
- [StartAsync(CancellationToken)](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#startasync-cancellationtoken) : `Task` `virtual` -- Starts the `Hosting.IHostedService` objects configured for the program. The application will run until interrupted or until `StopApplication` is called.
- [StopAsync(CancellationToken)](/reference/api/csharp/aspire.hosting/distributedapplication/methods.md#stopasync-cancellationtoken) : `Task` `virtual` -- Attempts to gracefully stop the program.
