Skip to content

Commit 8a3cf0b

Browse files
Merge pull request #3225 from dotnet/main
✅ Merge `main` into `live`
2 parents 115aaaf + 982889c commit 8a3cf0b

21 files changed

+510
-1
lines changed

docs/fundamentals/external-parameters.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,15 @@ var builder = DistributedApplication.CreateBuilder(args);
134134
var redis = builder.AddConnectionString("redis");
135135

136136
builder.AddProject<Projects.WebApplication>("api")
137-
.WithReference(redis);
137+
.WithReference(redis)
138+
.WaitFor(redis);
138139

139140
builder.Build().Run();
140141
```
141142

143+
> [!NOTE]
144+
> Using <xref:Aspire.Hosting.ResourceBuilderExtensions.WaitFor*> with a connection string will implicitly wait for the resource that the connection string connects to.
145+
142146
Now consider the following app host configuration file _:::no-loc text="appsettings.json":::_:
143147

144148
```json
@@ -151,6 +155,18 @@ Now consider the following app host configuration file _:::no-loc text="appsetti
151155

152156
For more information pertaining to connection strings and their representation in the deployment manifest, see [Connection string and binding references](../deployment/manifest-format.md#connection-string-and-binding-references).
153157

158+
### Build connection strings with reference expressions
159+
160+
If you want to construct a connection string from parameters and ensure that it's handled correctly in both development and production, use <xref:Aspire.Hosting.ConnectionStringBuilderExtensions.AddConnectionString*> with a <xref:Aspire.Hosting.ApplicationModel.ReferenceExpression>.
161+
162+
For example, if you have a secret parameter that stores a small part of a connection string, use this code to insert it:
163+
164+
:::code language="csharp" source="snippets/referenceexpressions/AspireReferenceExpressions.AppHost/Program.cs" id="secretkey":::
165+
166+
You can also use reference expressions to append text to connection strings created by .NET Aspire resources. For example, when you add a PostgreSQL resource to your .NET 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:
167+
168+
:::code language="csharp" source="snippets/referenceexpressions/AspireReferenceExpressions.AppHost/Program.cs" id="postgresappend":::
169+
154170
## Parameter example
155171

156172
To express a parameter, consider the following example code:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Sdk Name="Aspire.AppHost.Sdk" Version="9.2.0" />
4+
5+
<PropertyGroup>
6+
<OutputType>Exe</OutputType>
7+
<TargetFramework>net9.0</TargetFramework>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
<Nullable>enable</Nullable>
10+
<UserSecretsId>6329c834-f9f4-422d-b8e3-d9583b2574c3</UserSecretsId>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.2.0" />
15+
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="9.2.1" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\AspireReferenceExpressions.CatalogAPI\AspireReferenceExpressions.CatalogAPI.csproj" />
20+
<ProjectReference Include="..\AspireReferenceExpressions.CustomerAPI\AspireReferenceExpressions.CustomerAPI.csproj" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var builder = DistributedApplication.CreateBuilder(args);
2+
3+
// <secretkey>
4+
var secretKey = builder.AddParameter("secretkey", secret: true);
5+
6+
var connectionString = builder.AddConnectionString(
7+
"composedconnectionstring",
8+
ReferenceExpression.Create($"Endpoint=https://api.contoso.com/v1;Key={secretKey}"));
9+
10+
builder.AddProject<Projects.AspireReferenceExpressions_CatalogAPI>("catalogapi")
11+
.WithReference(connectionString)
12+
.WaitFor(connectionString);
13+
// </secretkey>
14+
15+
// <postgresappend>
16+
var postgres = builder.AddPostgres("postgres");
17+
var database = postgres.AddDatabase("db");
18+
19+
var pgConnectionString = builder.AddConnectionString(
20+
"pgdatabase",
21+
ReferenceExpression.Create($"{database};Include Error Details=true"));
22+
23+
builder.AddProject<Projects.AspireReferenceExpressions_CustomerAPI>("customerapi")
24+
.WithReference(pgConnectionString)
25+
.WaitFor(pgConnectionString);
26+
// </postgresappend>
27+
28+
builder.Build().Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"https": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "https://localhost:17120;http://localhost:15249",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development",
11+
"DOTNET_ENVIRONMENT": "Development",
12+
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21231",
13+
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22183"
14+
}
15+
},
16+
"http": {
17+
"commandName": "Project",
18+
"dotnetRunMessages": true,
19+
"launchBrowser": true,
20+
"applicationUrl": "http://localhost:15249",
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development",
23+
"DOTNET_ENVIRONMENT": "Development",
24+
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19230",
25+
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20092"
26+
}
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning",
6+
"Aspire.Hosting.Dcp": "Warning"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\AspireReferenceExpressions.ServiceDefaults\AspireReferenceExpressions.ServiceDefaults.csproj" />
15+
</ItemGroup>
16+
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@AspireReferenceExpressions.CatalogAPI_HostAddress = http://localhost:5058
2+
3+
GET {{AspireReferenceExpressions.CatalogAPI_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
builder.AddServiceDefaults();
4+
5+
// Add services to the container.
6+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
7+
builder.Services.AddOpenApi();
8+
9+
var app = builder.Build();
10+
11+
app.MapDefaultEndpoints();
12+
13+
// Configure the HTTP request pipeline.
14+
if (app.Environment.IsDevelopment())
15+
{
16+
app.MapOpenApi();
17+
}
18+
19+
app.UseHttpsRedirection();
20+
21+
var summaries = new[]
22+
{
23+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
24+
};
25+
26+
app.MapGet("/weatherforecast", () =>
27+
{
28+
var forecast = Enumerable.Range(1, 5).Select(index =>
29+
new WeatherForecast
30+
(
31+
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
32+
Random.Shared.Next(-20, 55),
33+
summaries[Random.Shared.Next(summaries.Length)]
34+
))
35+
.ToArray();
36+
return forecast;
37+
})
38+
.WithName("GetWeatherForecast");
39+
40+
app.Run();
41+
42+
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
43+
{
44+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": false,
8+
"applicationUrl": "http://localhost:5058",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": false,
17+
"applicationUrl": "https://localhost:7171;http://localhost:5058",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\AspireReferenceExpressions.ServiceDefaults\AspireReferenceExpressions.ServiceDefaults.csproj" />
15+
</ItemGroup>
16+
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@AspireReferenceExpressions.CustomerAPI_HostAddress = http://localhost:5022
2+
3+
GET {{AspireReferenceExpressions.CustomerAPI_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
builder.AddServiceDefaults();
4+
5+
// Add services to the container.
6+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
7+
builder.Services.AddOpenApi();
8+
9+
var app = builder.Build();
10+
11+
app.MapDefaultEndpoints();
12+
13+
// Configure the HTTP request pipeline.
14+
if (app.Environment.IsDevelopment())
15+
{
16+
app.MapOpenApi();
17+
}
18+
19+
var summaries = new[]
20+
{
21+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
22+
};
23+
24+
app.MapGet("/weatherforecast", () =>
25+
{
26+
var forecast = Enumerable.Range(1, 5).Select(index =>
27+
new WeatherForecast
28+
(
29+
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
30+
Random.Shared.Next(-20, 55),
31+
summaries[Random.Shared.Next(summaries.Length)]
32+
))
33+
.ToArray();
34+
return forecast;
35+
})
36+
.WithName("GetWeatherForecast");
37+
38+
app.Run();
39+
40+
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
41+
{
42+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": false,
8+
"applicationUrl": "http://localhost:5022",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsAspireSharedProject>true</IsAspireSharedProject>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
12+
13+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.4.0" />
14+
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="9.2.0" />
15+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.11.2" />
16+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.11.2" />
17+
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.11.1" />
18+
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.11.1" />
19+
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.11.1" />
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)