Aspire

External parameters

Environments provide context for the application to run in. Parameters express the ability to ask for an external value when running the app. Parameters can be used to provide values to the app when running locally, or to prompt for values when deploying. They can be used to model a wide range of scenarios including secrets, connection strings, and other configuration values that might vary between environments.

Parameter values are read from the Parameters section of the AppHost’s configuration and are used to provide values to the app while running locally. When you run or publish the app, if the value isn’t configured you’re prompted to provide it.

Consider the following example AppHost AppHost.cs file:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
// Add a parameter named "example-parameter-name"
const
const parameter: ParameterResource
parameter
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
('example-parameter-name');
const
const api: DotnetProjectResource
api
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addDotnetProject(name: string, path: string, options?: DotnetProjectOptions): DotnetProjectResource

Adds a .NET application resource.

addDotnetProject
('api', '../ApiService/ApiService.csproj');
await
const api: DotnetProjectResource
api
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): DotnetProjectResource

Sets an environment variable

withEnvironment
('ENVIRONMENT_VARIABLE_NAME',
const parameter: ParameterResource
parameter
);

The preceding code adds a parameter named example-parameter-name to the AppHost. The parameter is then passed to the Projects.ApiService project as an environment variable named ENVIRONMENT_VARIABLE_NAME.

Adding parameters to the builder is only one aspect of the configuration. You must also provide the value for the parameter. The value can be provided in the AppHost configuration file, set as a user secret, or configured in any other standard configuration. When parameter values aren’t found, they’re prompted for when you run or publish the app.

Consider the following AppHost configuration file appsettings.json:

appsettings.json
{
"Parameters": {
"example-parameter-name": "local-value"
}
}

The preceding JSON configures a parameter in the Parameters section of the AppHost configuration. In other words, that AppHost is able to find the parameter as it’s configured. For example, you could access the value using the Parameters:example-parameter-name key:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const key: "Parameters:example-parameter-name"
key
= 'Parameters:example-parameter-name';
const
const value: string
value
=
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.getConfiguration(): IConfiguration

Gets the application configuration.

getConfiguration
().
IConfiguration.getConfigValue(key: string): string

Gets a configuration value by key.

getConfigValue
(
const key: "Parameters:example-parameter-name"
key
); // value = "local-value"

Set parameter values using environment variables

Section titled “Set parameter values using environment variables”

Parameters can be set using environment variables, which is particularly useful in CI/CD pipelines and deployment scenarios. The environment variable name follows the pattern Parameters__{parameter_name}, using double underscores (__) to separate the configuration section from the parameter name and a single underscore to represent dashes.

Consider the following example AppHost AppHost.cs file:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
// Define container registry parameters
const
const endpoint: ParameterResource
endpoint
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
('registry-endpoint');
const
const repository: ParameterResource
repository
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
('registry-repository');
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addContainerRegistry(name: string, endpoint: string | ParameterResource, repository?: string | ParameterResource): ContainerRegistryResource (+1 overload)

Adds a container registry resource

addContainerRegistry
('container-registry',
const endpoint: ParameterResource
endpoint
,
const repository: ParameterResource
repository
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

These parameters can be provided via environment variables:

  • Parameters__registry_endpoint - Registry URL (e.g., ghcr.io)
  • Parameters__registry_repository - Repository path (e.g., username/reponame)

The following example shows how to set parameters as environment variables in a GitHub Actions workflow:

.github/workflows/deploy.yml
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push images with Aspire
env:
Parameters__registry_endpoint: ghcr.io
Parameters__registry_repository: your-org/your-repo
run: aspire do push

Aspire resolves parameter values in the following order:

  1. Environment variables - Values set using the Parameters__* syntax
  2. Configuration files - Values from appsettings.json, user secrets, or other configuration sources
  3. User prompts - If no value is found, the dashboard or CLI prompts for the value

This resolution order allows you to override configuration file values with environment variables, making it easy to adapt your application to different environments without modifying configuration files.

Prompt for parameter values in the dashboard

Section titled “Prompt for parameter values in the dashboard”

If your code adds parameters but doesn’t set them, you’ll see a prompt to configure their values in the Aspire dashboard. The Unresolved parameters message appears, and you can select Enter values to resolve the problem:

Screenshot of the Aspire dashboard warning that appears when there are unresolved parameters.

When you select Enter values, Aspire displays a form that you can use to configure values for each of the missing parameters.

You can also control how the dashboard displays these parameters, by using these methods:

  • WithDescriptionwithDescriptionWithDescription: Use this method to provide a text description that helps users understand the purpose of the parameter. To provide a formatted description in Markdown, use the enableMarkdown: true parameter.
  • WithCustomInputwithCustomInputWithCustomInput: Use this method to provide a callback method that customizes the parameter dialog. For example, in this callback you can customize the default value, input type, label, and placeholder text. When using WithCustomInput, you must copy the EnableDescriptionMarkdown property from the parameter to the InteractionInput object to preserve markdown rendering.

This code shows how to set a description and use the callback:

apphost.mts
import {
type InputType = "Text" | "SecretText" | "Choice" | "Boolean" | "Number" | "File"
const InputType: {
readonly Text: "Text";
readonly SecretText: "SecretText";
readonly Choice: "Choice";
readonly Boolean: "Boolean";
readonly Number: "Number";
readonly File: "File";
}

Enum Aspire.Hosting.InputType

InputType
,
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const externalServiceUrl: ParameterResource
externalServiceUrl
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
('external-service-url');
await
const externalServiceUrl: ParameterResource
externalServiceUrl
.
ParameterResource.withDescription(description: string, options?: {
enableMarkdown?: boolean;
} | undefined): ParameterResource (+1 overload)

Sets the description of the parameter resource.

withDescription
(
'The URL of the external service. See [example.com](https://example.com) for details.',
{
enableMarkdown?: boolean | undefined
enableMarkdown
: true }
)
.
ParameterResource.withCustomInput(options: ParameterCustomInputOptions): ParameterResource (+1 overload)

Customizes the parameter input shown by the dashboard

withCustomInput
({
ParameterCustomInputOptions.inputType?: InputType | undefined
inputType
:
const InputType: {
readonly Text: "Text";
readonly SecretText: "SecretText";
readonly Choice: "Choice";
readonly Boolean: "Boolean";
readonly Number: "Number";
readonly File: "File";
}

Enum Aspire.Hosting.InputType

InputType
.
type Text: "Text"
Text
,
ParameterCustomInputOptions.label?: string | undefined
label
: 'External service URL',
ParameterCustomInputOptions.placeholder?: string | undefined
placeholder
: 'Enter value for external-service-url',
});
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addExternalService(name: string, url: string | uri | ParameterResource): ExternalServiceResource

Adds an external service resource

addExternalService
('external-service',
const externalServiceUrl: ParameterResource
externalServiceUrl
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

The code renders this control in the dashboard:

Screenshot of the Aspire dashboard parameter completion dialog with customizations.

Parameters can be used to model secrets. When a parameter is marked as a secret, it serves as a hint to the deployment system that the value should be treated as a secret. When you publish the app, the value is prompted for and stored in a secure location. When you run the app locally, the value is read from the Parameters section of the AppHost configuration.

Consider the following example AppHost AppHost.cs file:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
// Add a secret parameter named "secret"
const
const secret: ParameterResource
secret
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
('secret', {
secret?: boolean | undefined
secret
: true });
const
const api: DotnetProjectResource
api
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addDotnetProject(name: string, path: string, options?: DotnetProjectOptions): DotnetProjectResource

Adds a .NET application resource.

addDotnetProject
('api', '../ApiService/ApiService.csproj');
await
const api: DotnetProjectResource
api
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): DotnetProjectResource

Sets an environment variable

withEnvironment
('SECRET',
const secret: ParameterResource
secret
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

Now consider the following AppHost configuration file appsettings.json:

appsettings.json
{
"Parameters": {
"secret": "local-secret"
}
}

The manifest representation is as follows:

{
"resources": {
"value": {
"type": "parameter.v0",
"value": "{value.inputs.value}",
"inputs": {
"value": {
"type": "string",
"secret": true
}
}
}
}
}

By default, a parameter is required: if no value is configured, you’re prompted for one when you run or publish the app, and publishing fails until a value is supplied. An empty string doesn’t satisfy a required parameter either, including secret and connection-string parameters.

When a required value is missing or empty during aspire run, the parameter shows a ValueMissing state, and the dashboard asks you to supply the value. When the dashboard can’t prompt, such as in publish or non-interactive runs, initialization fails with a MissingParameterValueException that names the parameter.

Some parameters don’t need a value in every environment. Use AsOptional to mark a parameter as optional:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const messagePrefix: ParameterResource
messagePrefix
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
('message-prefix').
ParameterResource.asOptional(): ParameterResource (+1 overload)

Marks the parameter resource as optional

asOptional
();
const
const api: DotnetProjectResource
api
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addDotnetProject(name: string, path: string, options?: DotnetProjectOptions): DotnetProjectResource

Adds a .NET application resource.

addDotnetProject
('api', '../ApiService/ApiService.csproj');
await
const api: DotnetProjectResource
api
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): DotnetProjectResource

Sets an environment variable

withEnvironment
('MESSAGE_PREFIX',
const messagePrefix: ParameterResource
messagePrefix
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

When an optional parameter has no configured value, you aren’t prompted for it, and resources that depend on it don’t wait for it. The parameter resolves to null, and a null value is omitted when it’s used as an environment variable, so in the preceding example MESSAGE_PREFIX isn’t set. If a value is configured, it’s used just like a required parameter’s value, and an explicitly configured empty string stays "".

Mark a parameter optional only when an absent or empty value is genuinely valid for your app. Don’t make required credentials optional to avoid the missing-value check.

Set and inspect parameter values programmatically

Section titled “Set and inspect parameter values programmatically”

In addition to configuration, environment variables, and dashboard prompts, AppHost code can read and set a parameter’s value directly on the ParameterResource. This is useful when a value should be derived from other resources or computed logic instead of coming from an external source.

Use TryGetCurrentValue to check whether a parameter currently has a value without waiting for an unresolved prompt, and SetValueAsync to set or clear it:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const messagePrefix: ParameterResource
messagePrefix
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
('message-prefix').
ParameterResource.asOptional(): ParameterResource (+1 overload)

Marks the parameter resource as optional

asOptional
();
if ((await
const messagePrefix: ParameterResource
messagePrefix
.
ParameterResource.tryGetCurrentValue(): Promise<string | null> (+1 overload)

Gets the current value for this parameter without waiting for unresolved input

tryGetCurrentValue
()) === null) {
await
const messagePrefix: ParameterResource
messagePrefix
.
ParameterResource.setValueAsync(options?: {
value?: string | null;
} | undefined): Promise<void> (+1 overload)

Sets or replaces the value for this parameter

setValueAsync
({
value?: string | null | undefined
value
: 'local' });
}

TryGetCurrentValue returns false (or null in TypeScript) when the parameter is unresolved, an empty string, failed to initialize, or is an optional parameter that resolved without a value. SetValueAsync sets or replaces the value and updates any dependent dashboard state. Calling SetValueAsync without a value (setValueAsync() in TypeScript) clears it.

In some scenarios, you might want to read parameter values directly from a specific configuration key rather than following the standard Parameters:* pattern. The AddParameterFromConfigurationaddParameterFromConfigurationAddParameterFromConfiguration method allows you to specify a custom configuration key for a parameter.

public static IResourceBuilder<ParameterResource> AddParameterFromConfiguration(
this IDistributedApplicationBuilder builder,
string name,
string configurationKey,
bool secret = false
);
  • name - The name of the parameter resource
  • configurationKey - The configuration key to read the value from
  • secret - Optional flag indicating whether the parameter should be treated as a secret (default: false)

Consider a scenario where you have configuration values stored in a custom section:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
// Read from a custom configuration key
const
const dbPassword: ParameterResource
dbPassword
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameterFromConfiguration(name: string, configurationKey: string, options?: {
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource to the application, with a value coming from configuration.

addParameterFromConfiguration
(
'db-password',
'CustomConfig:Database:Password',
{
secret?: boolean | undefined
secret
: true }
);
const
const api: DotnetProjectResource
api
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addDotnetProject(name: string, path: string, options?: DotnetProjectOptions): DotnetProjectResource

Adds a .NET application resource.

addDotnetProject
('api', '../ApiService/ApiService.csproj');
await
const api: DotnetProjectResource
api
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): DotnetProjectResource

Sets an environment variable

withEnvironment
('DB_PASSWORD',
const dbPassword: ParameterResource
dbPassword
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

The corresponding configuration file would look like:

appsettings.json
{
"CustomConfig": {
"Database": {
"Password": "your-secure-password"
}
}
}

This method is useful when:

  • You need to integrate with existing configuration structures
  • Your configuration follows a different pattern than the standard Parameters:* convention
  • You want to read values from specific configuration sections

Parameters can be used to model connection strings. When you publish the app, the value is prompted for and stored in a secure location. When you run the app locally, the value is read from the ConnectionStrings section of the AppHost configuration.

Consider the following example AppHost AppHost.cs file:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const redis: IResourceWithConnectionString
redis
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addConnectionString(name: string, options?: {
environmentVariableNameOrExpression?: string | ReferenceExpression;
}): IResourceWithConnectionString (+1 overload)

Adds a connection string resource

addConnectionString
('redis');
const
const api: DotnetProjectResource
api
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addDotnetProject(name: string, path: string, options?: DotnetProjectOptions): DotnetProjectResource

Adds a .NET application resource.

addDotnetProject
(
'api',
'../WebApplication/WebApplication.csproj'
);
await
const api: DotnetProjectResource
api
.
ExecutableResource.withReference(source: IResource | EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): DotnetProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const redis: IResourceWithConnectionString
redis
).
ExecutableResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): DotnetProjectResource

Waits for another resource to be ready

waitFor
(
const redis: IResourceWithConnectionString
redis
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

Now consider the following AppHost configuration file appsettings.json:

appsettings.json
{
"ConnectionStrings": {
"redis": "local-connection-string"
}
}

Build connection strings with reference expressions

Section titled “Build connection strings with reference expressions”

If you want to construct a connection string from parameters and ensure that it’s handled correctly in both development and production, use AddConnectionStringaddConnectionStringAddConnectionString with a ReferenceExpression.

For example, if you have a secret parameter that stores a small part of a connection string, use this code to insert it:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
,
function refExpr(strings: TemplateStringsArray, ...values: unknown[]): ReferenceExpression

Creates a reference expression from a tagged template literal

refExpr
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const secretKey: ParameterResource
secretKey
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
('secretkey', {
secret?: boolean | undefined
secret
: true });
const
const connectionString: IResourceWithConnectionString
connectionString
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addConnectionString(name: string, options?: {
environmentVariableNameOrExpression?: string | ReferenceExpression;
}): IResourceWithConnectionString (+1 overload)

Adds a connection string resource

addConnectionString
(
'composedconnectionstring',
{
environmentVariableNameOrExpression?: string | ReferenceExpression | undefined
environmentVariableNameOrExpression
:
function refExpr(strings: TemplateStringsArray, ...values: unknown[]): ReferenceExpression

Creates a reference expression from a tagged template literal

refExpr
`Endpoint=https://api.contoso.com/v1;Key=${
const secretKey: ParameterResource
secretKey
}`,
}
);
const
const catalogApi: DotnetProjectResource
catalogApi
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addDotnetProject(name: string, path: string, options?: DotnetProjectOptions): DotnetProjectResource

Adds a .NET application resource.

addDotnetProject
(
'catalogapi',
'../CatalogApi/CatalogApi.csproj'
);
await
const catalogApi: DotnetProjectResource
catalogApi
.
ExecutableResource.withReference(source: IResource | EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): DotnetProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const connectionString: IResourceWithConnectionString
connectionString
).
ExecutableResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): DotnetProjectResource

Waits for another resource to be ready

waitFor
(
const connectionString: IResourceWithConnectionString
connectionString
);

You can also use reference expressions to append text to connection strings created by Aspire resources. For example, when you add a PostgreSQL resource to your Aspire solution, the database server runs in a container and a connection string is formulated for it. In the following code, the extra property Include Error Details is appended to that connection string before it’s passed to consuming projects:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
,
function refExpr(strings: TemplateStringsArray, ...values: unknown[]): ReferenceExpression

Creates a reference expression from a tagged template literal

refExpr
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const postgres: PostgresServerResource
postgres
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addPostgres(name: string, options?: {
userName?: string | ParameterResource;
password?: string | ParameterResource;
port?: number;
}): PostgresServerResource (+1 overload)

Adds a PostgreSQL resource to the application model. A container is used for local development.

addPostgres
('postgres');
const
const database: PostgresDatabaseResource
database
= await
const postgres: PostgresServerResource
postgres
.
PostgresServerResource.addDatabase(name: string, options?: {
databaseName?: string;
} | undefined): PostgresDatabaseResource (+1 overload)

Adds a PostgreSQL database to the application model.

addDatabase
('db');
const
const pgConnectionString: IResourceWithConnectionString
pgConnectionString
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addConnectionString(name: string, options?: {
environmentVariableNameOrExpression?: string | ReferenceExpression;
}): IResourceWithConnectionString (+1 overload)

Adds a connection string resource

addConnectionString
('pgdatabase', {
environmentVariableNameOrExpression?: string | ReferenceExpression | undefined
environmentVariableNameOrExpression
:
function refExpr(strings: TemplateStringsArray, ...values: unknown[]): ReferenceExpression

Creates a reference expression from a tagged template literal

refExpr
`${
const database: PostgresDatabaseResource
database
};Include Error Details=true`,
});
const
const customerApi: DotnetProjectResource
customerApi
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addDotnetProject(name: string, path: string, options?: DotnetProjectOptions): DotnetProjectResource

Adds a .NET application resource.

addDotnetProject
(
'customerapi',
'../CustomerApi/CustomerApi.csproj'
);
await
const customerApi: DotnetProjectResource
customerApi
.
ExecutableResource.withReference(source: IResource | EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): DotnetProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const pgConnectionString: IResourceWithConnectionString
pgConnectionString
).
ExecutableResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): DotnetProjectResource

Waits for another resource to be ready

waitFor
(
const pgConnectionString: IResourceWithConnectionString
pgConnectionString
);

To express a parameter, consider the following example code:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
// In run mode, add the actual SQL Server resource.
// In publish mode, use a connection string representing an external SQL Server instance.
const
const isRunMode: boolean
isRunMode
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.executionContext: PropertyAccessor<DistributedApplicationExecutionContext>

Execution context for this invocation of the AppHost.

executionContext
.
DistributedApplicationExecutionContext.isRunMode: () => Promise<boolean>

Returns true if the current operation is running.

isRunMode
();
const
const db: IResourceWithConnectionString
db
=
const isRunMode: boolean
isRunMode
? (await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addSqlServer(name: string, options?: {
password?: string | ParameterResource;
port?: number;
}): SqlServerServerResource (+1 overload)

Adds a SQL Server resource to the application model. A container is used for local development.

addSqlServer
('sql')).
SqlServerServerResource.addDatabase(name: string, options?: {
databaseName?: string;
} | undefined): SqlServerDatabaseResource (+1 overload)

Adds a SQL Server database resource

addDatabase
('db')
: await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addConnectionString(name: string, options?: {
environmentVariableNameOrExpression?: string | ReferenceExpression;
}): IResourceWithConnectionString (+1 overload)

Adds a connection string resource

addConnectionString
('db');
const
const insertionRows: ParameterResource
insertionRows
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
('insertionRows');
const
const api: DotnetProjectResource
api
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addDotnetProject(name: string, path: string, options?: DotnetProjectOptions): DotnetProjectResource

Adds a .NET application resource.

addDotnetProject
(
'api',
'../Parameters.ApiService/Parameters.ApiService.csproj'
);
await
const api: DotnetProjectResource
api
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): DotnetProjectResource

Sets an environment variable

withEnvironment
('InsertionRows',
const insertionRows: ParameterResource
insertionRows
);
await
const api: DotnetProjectResource
api
.
ExecutableResource.withReference(source: IResource | EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): DotnetProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const db: IResourceWithConnectionString
db
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

The following steps are performed:

  1. In run mode, adds a SQL Server resource named sql and a database named db. In publish mode, uses an external connection string named db.
  2. Adds a parameter named insertionRows.
  3. Adds a project named api and associates it with the Projects.Parameters_ApiService project resource type-parameter.
  4. Passes the insertionRows parameter to the api project.
  5. References the db database.

The value for the insertionRows parameter is read from the Parameters section of the AppHost configuration file appsettings.json:

appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
},
"Parameters": {
"insertionRows": "1"
}
}

The Parameters_ApiService project consumes the insertionRows parameter. Consider the Program.cs example file:

Program.cs
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
int insertionRows = builder.Configuration.GetValue<int>("InsertionRows", 1);
builder.AddServiceDefaults();
builder.AddSqlServerDbContext<MyDbContext>("db");
var app = builder.Build();
app.MapGet("/", async (MyDbContext context) =>
{
// You wouldn't normally do this on every call,
// but doing it here just to make this simple.
context.Database.EnsureCreated();
for (var i = 0; i < insertionRows; i++)
{
var entry = new Entry();
await context.Entries.AddAsync(entry);
}
await context.SaveChangesAsync();
var entries = await context.Entries.ToListAsync();
return new
{
totalEntries = entries.Count,
entries
};
});
app.Run();