API reference for .NET project resources in Aspire
The ProjectResource class represents a .NET project that can be run as part of your distributed application. It provides automatic service discovery, connection string injection, and seamless integration with the .NET ecosystem.
public class ProjectResource : Resource, IResourceWithEnvironment, IResourceWithArgs, IResourceWithServiceDiscovery, IResourceWithWaitSupport, IResourceWithProbes{ public ProjectResource(string name);}
Project resources automatically participate in service discovery. Other services can reference them to get automatically configured HTTP/HTTPS endpoints.
var postgres = builder.AddPostgres("postgres");var db = postgres.AddDatabase("mydb");builder.AddProject<Projects.Api>("api") .WithReference(db);// Injects: ConnectionStrings__mydb={connectionString}
When you reference a project with .WithReference(), these environment variables are automatically injected:
Service Discovery Variables
# For HTTPS endpoint named "https":services__api__https__0=https://localhost:7001# For HTTP endpoint named "http":services__api__http__0=http://localhost:5000# Multiple endpoints of the same scheme:services__api__https__0=https://localhost:7001services__api__https__1=https://localhost:7002
These variables work with .NET’s Service Discovery to automatically configure HttpClient instances.
var api = builder.AddProject<Projects.Api>("api") .WithHttpsEndpoint();var httpsEndpoint = api.GetEndpoint("https");builder.AddProject<Projects.Frontend>("frontend") .WithEnvironment("API_URL", httpsEndpoint);
var sql = builder.AddSqlServer("sql");var db = sql.AddDatabase("mydb");builder.AddProject<Projects.Api>("api") .WithReference(db);// Injects: ConnectionStrings__mydb={connectionString}
var postgres = builder.AddPostgres("postgres");var db = postgres.AddDatabase("mydb");builder.AddProject<Projects.Api>("api") .WithReference(db) .WaitFor(postgres); // Wait for postgres to be ready
When generating manifests for deployment, projects are published as container images.
Publishing Configuration
var api = builder.AddProject<Projects.Api>("api") .WithHttpsEndpoint();// During 'dotnet publish', this project will be:// 1. Built as a container image// 2. Included in the deployment manifest// 3. Tagged with the registry from builder options