IResourceBuilder<T> interface provides a fluent API for configuring resources in .NET Aspire applications. Every resource added to an application returns a builder that enables method chaining for configuration.
Interface Definition
Properties
Gets the distributed application builder that owns this resource builder. Use this to access application-level configuration or add related resources.
Gets the resource being built. Access this to read or modify resource properties directly.
Core Methods
WithAnnotation
Adds metadata and configuration to resources through annotations.Adds an annotation to the resource being built.Parameters:
annotation- The annotation instance to addbehavior- Controls how the annotation is added:Append(default) - Add the annotation to existing annotationsReplace- Replace any existing annotation of the same type
Using Annotations
Common Configuration Methods
These extension methods are available onIResourceBuilder<T> for different resource types:
Environment Variables
Available for resources implementingIResourceWithEnvironment.
Adds a static environment variable.Parameters:
name- Variable namevalue- Variable value (string, parameter, endpoint reference, or expression)
Adds environment variables through a callback for computed or dynamic values.Parameters:
callback- Action or async function receivingEnvironmentCallbackContext
Command-Line Arguments
Available for resources implementingIResourceWithArgs.
Adds static command-line arguments.Parameters:
args- Array of argument strings
Adds arguments through a callback for computed values.Parameters:
callback- Action or async function receivingCommandLineArgsCallbackContext
Endpoints
Available for resources implementingIResourceWithEndpoints.
Adds an HTTP endpoint.Parameters:
port- Optional external port numbername- Optional endpoint name (defaults to “http”)
Adds an HTTPS endpoint.Parameters:
port- Optional external port numbername- Optional endpoint name (defaults to “https”)
Adds a custom endpoint with full control over configuration.Parameters:
targetPort- Port the resource listens onport- Optional external portscheme- URI scheme (http, https, tcp, etc.)name- Optional endpoint name
Modifies an existing endpoint or creates a new one.Parameters:
name- Endpoint namecallback- Action to modify theEndpointAnnotationcreateIfNotExists- Create the endpoint if it doesn’t exist
Getting Endpoint References
Gets a reference to an endpoint that can be used in references or environment variables.Parameters:
name- Endpoint name
EndpointReference that resolves to the endpoint URL at runtimeEndpoint References
Resource References
Create dependencies between resources usingWithReference.
Adds a reference to another resource, injecting connection strings and/or service discovery information.Parameters:
source- The resource builder to referenceconnectionName- Optional custom name for connection string (only forIResourceWithConnectionString)optional- Whether the connection string is optional
Container-Specific Methods
For resources that areContainerResource:
Image Configuration
Volumes and Bind Mounts
Adds a Docker volume to the container.Parameters:
name- Optional volume name (null creates anonymous volume)target- Mount path in containerisReadOnly- Whether the volume is read-only
Mounts a host directory or file into the container.Parameters:
source- Host path (relative to app host directory unless absolute)target- Mount path in containerisReadOnly- Whether the mount is read-only
Container Runtime
Publishing Configuration
Customizes how the resource is written to the deployment manifest.Parameters:
callback- Action or async function receivingManifestPublishingContext
Custom Manifest Output
Method Chaining Pattern
All builder methods returnIResourceBuilder<T>, enabling fluent configuration:
Complete Example
Related APIs
- DistributedApplication - Creating and running applications
- IResource - Resource interfaces and contracts
- ContainerResource - Container resource specifics
- ProjectResource - .NET project resources