ResourceBuilderExtensions Methods
AsHttp2Service(IResourceBuilder<T>) Section titled AsHttp2Service(IResourceBuilder<T>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> AsHttp2Service<T>( this IResourceBuilder<T> builder) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. ClearContainerFilesSources(IResourceBuilder<T>) Section titled ClearContainerFilesSources(IResourceBuilder<T>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> ClearContainerFilesSources<T>( this IResourceBuilder<T> builder) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder to which the container files source annotations should be removed. Cannot be null. Returns
IResourceBuilder<T> The resource builder instance with the container files source annotation applied. ExcludeFromManifest(IResourceBuilder<T>) Section titled ExcludeFromManifest(IResourceBuilder<T>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> ExcludeFromManifest<T>( this IResourceBuilder<T> builder) { // ... }}Parameters
builder IResourceBuilder<T> The resource to exclude. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. ExcludeFromMcp(IResourceBuilder<T>) Section titled ExcludeFromMcp(IResourceBuilder<T>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> ExcludeFromMcp<T>( this IResourceBuilder<T> builder) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. GetConnectionProperty(IResourceWithConnectionString, string) Section titled GetConnectionProperty(IResourceWithConnectionString, string) extension ReferenceExpression public static class ResourceBuilderExtensions{ public static ReferenceExpression GetConnectionProperty( this IResourceWithConnectionString resource, string key) { // ... }}Parameters
resource IResourceWithConnectionString The resource that provides the connection properties. Cannot be null. key string The key of the connection property to retrieve. Cannot be null. Returns
ReferenceExpression The value associated with the specified connection property key. Remarks
GetEndpoint(IResourceBuilder<T>, string, NetworkIdentifier) Section titled GetEndpoint(IResourceBuilder<T>, string, NetworkIdentifier) extension EndpointReference EndpointReference by name from the resource. These endpoints are declared either using ResourceBuilderExtensions.WithEndpoint or by launch settings (for project resources). The EndpointReference can be used to resolve the address of the endpoint in ResourceBuilderExtensions.WithEnvironment. public static class ResourceBuilderExtensions{ public static EndpointReference GetEndpoint<T>( this IResourceBuilder<T> builder, string name, NetworkIdentifier contextNetworkID) { // ... }}Parameters
builder IResourceBuilder<T> The the resource builder. name string The name of the endpoint. contextNetworkID NetworkIdentifier The network context in which to resolve the endpoint. If null, localhost (loopback) network context will be used. Returns
EndpointReference An EndpointReference that can be used to resolve the address of the endpoint after resource allocation has occurred. GetEndpoint(IResourceBuilder<T>, string) Section titled GetEndpoint(IResourceBuilder<T>, string) extension EndpointReference EndpointReference by name from the resource. These endpoints are declared either using ResourceBuilderExtensions.WithEndpoint or by launch settings (for project resources). The EndpointReference can be used to resolve the address of the endpoint in ResourceBuilderExtensions.WithEnvironment. public static class ResourceBuilderExtensions{ public static EndpointReference GetEndpoint<T>( this IResourceBuilder<T> builder, string name) { // ... }}Parameters
builder IResourceBuilder<T> The the resource builder. name string The name of the endpoint. Returns
EndpointReference An EndpointReference that can be used to resolve the address of the endpoint after resource allocation has occurred. PublishWithContainerFiles(IResourceBuilder<T>, IResourceBuilder<IResourceWithContainerFiles>, string) Section titled PublishWithContainerFiles(IResourceBuilder<T>, IResourceBuilder<IResourceWithContainerFiles>, string) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> PublishWithContainerFiles<T>( this IResourceBuilder<T> builder, IResourceBuilder<IResourceWithContainerFiles> source, string destinationPath) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder to which container files will be copied to. source IResourceBuilder<IResourceWithContainerFiles> The resource which contains the container files to be copied. destinationPath string The destination path within the resource's container where the files will be copied. WaitFor(IResourceBuilder<T>, IResourceBuilder<IResource>) Section titled WaitFor(IResourceBuilder<T>, IResourceBuilder<IResource>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WaitFor<T>( this IResourceBuilder<T> builder, IResourceBuilder<IResource> dependency) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder for the resource that will be waiting. dependency IResourceBuilder<IResource> The resource builder for the dependency resource. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
This method is useful when a resource should wait until another has started running. This can help reduce errors in logs during local development where dependency resources.
Some resources automatically register health checks with the application host container. For these resources, calling ResourceBuilderExtensions.WaitFor also results in the resource being blocked from starting until the health checks associated with the dependency resource return HealthStatus.Healthy.
The ResourceBuilderExtensions.WithHealthCheck method can be used to associate additional health checks with a resource.
var builder = DistributedApplication.CreateBuilder(args);var messaging = builder.AddRabbitMQ("messaging");builder.AddProject<Projects.MyApp>("myapp") .WithReference(messaging) .WaitFor(messaging);WaitFor(IResourceBuilder<T>, IResourceBuilder<IResource>, WaitBehavior) Section titled WaitFor(IResourceBuilder<T>, IResourceBuilder<IResource>, WaitBehavior) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WaitFor<T>( this IResourceBuilder<T> builder, IResourceBuilder<IResource> dependency, WaitBehavior waitBehavior) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder for the resource that will be waiting. dependency IResourceBuilder<IResource> The resource builder for the dependency resource. waitBehavior WaitBehavior The wait behavior to use. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
This method is useful when a resource should wait until another has started running. This can help reduce errors in logs during local development where dependency resources.
Some resources automatically register health checks with the application host container. For these resources, calling ResourceBuilderExtensions.WaitFor also results in the resource being blocked from starting until the health checks associated with the dependency resource return HealthStatus.Healthy.
The ResourceBuilderExtensions.WithHealthCheck method can be used to associate additional health checks with a resource.
The waitBehavior parameter can be used to control the behavior of the wait operation. When WaitBehavior.WaitOnResourceUnavailable is specified, the wait operation will continue to wait until the resource becomes healthy. This is the default behavior with the ResourceBuilderExtensions.WaitFor overload.
When WaitBehavior.StopOnResourceUnavailable is specified, the wait operation will throw a DistributedApplicationException if the resource enters an unavailable state.
var builder = DistributedApplication.CreateBuilder(args);var messaging = builder.AddRabbitMQ("messaging");builder.AddProject<Projects.MyApp>("myapp") .WithReference(messaging) .WaitFor(messaging, WaitBehavior.StopOnResourceUnavailable);WaitForCompletion(IResourceBuilder<T>, IResourceBuilder<IResource>, int) Section titled WaitForCompletion(IResourceBuilder<T>, IResourceBuilder<IResource>, int) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WaitForCompletion<T>( this IResourceBuilder<T> builder, IResourceBuilder<IResource> dependency, int exitCode = 0) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder for the resource that will be waiting. dependency IResourceBuilder<IResource> The resource builder for the dependency resource. exitCode int optional The exit code which is interpreted as successful. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
This method is useful when a resource should wait until another has completed. A common usage pattern would be to include a console application that initializes the database schema or performs other one off initialization tasks.
Note that this method has no impact at deployment time and only works for local development.
Wait for database initialization app to complete running.var builder = DistributedApplication.CreateBuilder(args);var pgsql = builder.AddPostgres("postgres");var dbprep = builder.AddProject<Projects.DbPrepApp>("dbprep") .WithReference(pgsql);builder.AddProject<Projects.DatabasePrepTool>("dbpreptool") .WithReference(pgsql) .WaitForCompletion(dbprep);WaitForStart(IResourceBuilder<T>, IResourceBuilder<IResource>) Section titled WaitForStart(IResourceBuilder<T>, IResourceBuilder<IResource>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WaitForStart<T>( this IResourceBuilder<T> builder, IResourceBuilder<IResource> dependency) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder for the resource that will be waiting. dependency IResourceBuilder<IResource> The resource builder for the dependency resource. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
This method is useful when a resource should wait until another has started running but doesn't need to wait for health checks to pass. This can help enable initialization scenarios where services need to start before health checks can pass.
Unlike ResourceBuilderExtensions.WaitFor, this method only waits for the dependency resource to enter the Running state and ignores any health check annotations associated with the dependency resource.
var builder = DistributedApplication.CreateBuilder(args);var messaging = builder.AddRabbitMQ("messaging");builder.AddProject<Projects.MyApp>("myapp") .WithReference(messaging) .WaitForStart(messaging);WaitForStart(IResourceBuilder<T>, IResourceBuilder<IResource>, WaitBehavior) Section titled WaitForStart(IResourceBuilder<T>, IResourceBuilder<IResource>, WaitBehavior) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WaitForStart<T>( this IResourceBuilder<T> builder, IResourceBuilder<IResource> dependency, WaitBehavior waitBehavior) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder for the resource that will be waiting. dependency IResourceBuilder<IResource> The resource builder for the dependency resource. waitBehavior WaitBehavior The wait behavior to use. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
This method is useful when a resource should wait until another has started running but doesn't need to wait for health checks to pass. This can help enable initialization scenarios where services need to start before health checks can pass.
Unlike ResourceBuilderExtensions.WaitFor, this method only waits for the dependency resource to enter the Running state and ignores any health check annotations associated with the dependency resource.
The waitBehavior parameter can be used to control the behavior of the wait operation. When WaitBehavior.WaitOnResourceUnavailable is specified, the wait operation will continue to wait until the resource enters the Running state. This is the default behavior with the ResourceBuilderExtensions.WaitForStart overload.
When WaitBehavior.StopOnResourceUnavailable is specified, the wait operation will throw a DistributedApplicationException if the resource enters an unavailable state.
var builder = DistributedApplication.CreateBuilder(args);var messaging = builder.AddRabbitMQ("messaging");builder.AddProject<Projects.MyApp>("myapp") .WithReference(messaging) .WaitForStart(messaging, WaitBehavior.StopOnResourceUnavailable);WithArgs(IResourceBuilder<T>, string[]) Section titled WithArgs(IResourceBuilder<T>, string[]) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithArgs<T>( this IResourceBuilder<T> builder, params string[] args) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder for a resource implementing IResourceWithArgs. args string[] The arguments to be passed to the resource when it is started. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithArgs(IResourceBuilder<T>, object[]) Section titled WithArgs(IResourceBuilder<T>, object[]) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithArgs<T>( this IResourceBuilder<T> builder, params object[] args) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder for a resource implementing IResourceWithArgs. args object[] The arguments to be passed to the resource when it is started. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithArgs(IResourceBuilder<T>, Action<CommandLineArgsCallbackContext>) Section titled WithArgs(IResourceBuilder<T>, Action<CommandLineArgsCallbackContext>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithArgs<T>( this IResourceBuilder<T> builder, Action<CommandLineArgsCallbackContext> callback) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder for a resource implementing IResourceWithArgs. callback Action<CommandLineArgsCallbackContext> A callback that allows for deferred execution for computing arguments. This runs after resources have been allocated by the orchestrator and allows access to other resources to resolve computed data, e.g. connection strings, ports. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithArgs(IResourceBuilder<T>, Func<CommandLineArgsCallbackContext, Task>) Section titled WithArgs(IResourceBuilder<T>, Func<CommandLineArgsCallbackContext, Task>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithArgs<T>( this IResourceBuilder<T> builder, Func<CommandLineArgsCallbackContext, Task> callback) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder for a resource implementing IResourceWithArgs. callback Func<CommandLineArgsCallbackContext, Task> An asynchronous callback that allows for deferred execution for computing arguments. This runs after resources have been allocated by the orchestrator and allows access to other resources to resolve computed data, e.g. connection strings, ports. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithCertificateAuthorityCollection(IResourceBuilder<TResource>, IResourceBuilder<CertificateAuthorityCollection>) Section titled WithCertificateAuthorityCollection(IResourceBuilder<TResource>, IResourceBuilder<CertificateAuthorityCollection>) extension IResourceBuilder<TResource> CertificateAuthorityCollectionAnnotation to the resource annotations to associate a certificate authority collection with the resource. This is used to configure additional trusted certificate authorities for the resource. Custom certificate trust is only applied in run mode; in publish mode resources will use their default certificate trust behavior. public static class ResourceBuilderExtensions{ public static IResourceBuilder<TResource> WithCertificateAuthorityCollection<TResource>( this IResourceBuilder<TResource> builder, IResourceBuilder<CertificateAuthorityCollection> certificateAuthorityCollection) { // ... }}Parameters
builder IResourceBuilder<TResource> The resource builder. certificateAuthorityCollection IResourceBuilder<CertificateAuthorityCollection> Additional certificates in a CertificateAuthorityCollection to treat as trusted certificate authorities for the resource. Returns
IResourceBuilder<TResource> The ApplicationModel.IResourceBuilder`1. Remarks
var caCollection = builder.AddCertificateAuthorityCollection("my-cas") .WithCertificatesFromFile("../my-ca.pem");
var container = builder.AddContainer("my-service", "my-service:latest") .WithCertificateAuthorityCollection(caCollection);WithCertificateTrustConfiguration(IResourceBuilder<TResource>, Func<CertificateTrustConfigurationCallbackAnnotationContext, Task>) Section titled WithCertificateTrustConfiguration(IResourceBuilder<TResource>, Func<CertificateTrustConfigurationCallbackAnnotationContext, Task>) extension IResourceBuilder<TResource> CertificateTrustConfigurationCallbackAnnotation to the resource annotations to associate a callback that is invoked when a resource needs to configure itself for custom certificate trust. May be called multiple times to register additional callbacks to append additional configuration. Custom certificate trust is only applied in run mode; in publish mode resources will use their default certificate trust behavior. public static class ResourceBuilderExtensions{ public static IResourceBuilder<TResource> WithCertificateTrustConfiguration<TResource>( this IResourceBuilder<TResource> builder, Func<CertificateTrustConfigurationCallbackAnnotationContext, Task> callback) { // ... }}Parameters
builder IResourceBuilder<TResource> The resource builder. callback Func<CertificateTrustConfigurationCallbackAnnotationContext, Task> The callback to invoke when a resource needs to configure itself for custom certificate trust. Returns
IResourceBuilder<TResource> The updated resource builder. Remarks
var container = builder.AddContainer("my-service", "my-service:latest") .WithCertificateTrustConfigurationCallback(ctx => { if (ctx.Scope != CertificateTrustScope.Append) { ctx .EnvironmentVariables["CUSTOM_CERTS_BUNDLE_ENV"] = ctx .CertificateBundlePath; } ctx .EnvironmentVariables["ADDITIONAL_CERTS_DIR_ENV"] = ctx .CertificateDirectoriesPath; });WithCertificateTrustScope(IResourceBuilder<TResource>, CertificateTrustScope) Section titled WithCertificateTrustScope(IResourceBuilder<TResource>, CertificateTrustScope) extension IResourceBuilder<TResource> CertificateTrustScope for custom certificate authorities associated with the resource. The scope specifies how custom certificate authorities should be applied to a resource at run time in local development scenarios. Custom certificate trust is only applied in run mode; in publish mode resources will use their default certificate trust behavior. public static class ResourceBuilderExtensions{ public static IResourceBuilder<TResource> WithCertificateTrustScope<TResource>( this IResourceBuilder<TResource> builder, CertificateTrustScope scope) { // ... }}Parameters
builder IResourceBuilder<TResource> The resource builder. scope CertificateTrustScope The scope to apply to custom certificate authorities associated with the resource. Returns
IResourceBuilder<TResource> The ApplicationModel.IResourceBuilder`1. Remarks
CertificateTrustScope.Append which means that custom certificate authorities should be appended to the default trusted certificate authorities for the resource. Setting the scope to CertificateTrustScope.Override indicates the set of certificates in referenced CertificateAuthorityCollection (and optionally Aspire developer certificiates) should be used as the exclusive source of trust for a resource. In all cases, this is a best effort implementation as not all resources support full customization of certificate trust. Set the scope for custom certificate authorities to override the default trusted certificate authorities for a container resource. var caCollection = builder.AddCertificateAuthorityCollection("my-cas") .WithCertificate(new X509Certificate2("my-ca.pem"));
var container = builder.AddContainer("my-service", "my-service:latest") .WithCertificateAuthorityCollection(caCollection) .WithCertificateTrustScope(CertificateTrustScope.Override);WithChildRelationship(IResourceBuilder<T>, IResourceBuilder<IResource>) Section titled WithChildRelationship(IResourceBuilder<T>, IResourceBuilder<IResource>) extension IResourceBuilder<T> ResourceRelationshipAnnotation to the resource annotations to add a parent-child relationship. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithChildRelationship<T>( this IResourceBuilder<T> builder, IResourceBuilder<IResource> child) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. child IResourceBuilder<IResource> The child of builder. Returns
IResourceBuilder<T> A resource builder. Remarks
The WithChildRelationship method is used to add child relationships to the resource. Relationships are used to link resources together in UI.
var builder = DistributedApplication.CreateBuilder(args);
var parameter = builder.AddParameter("parameter");
var backend = builder.AddProject<Projects.Backend>("backend"); .WithChildRelationship(parameter);WithChildRelationship(IResourceBuilder<T>, IResource) Section titled WithChildRelationship(IResourceBuilder<T>, IResource) extension IResourceBuilder<T> ResourceRelationshipAnnotation to the resource annotations to add a parent-child relationship. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithChildRelationship<T>( this IResourceBuilder<T> builder, IResource child) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. child IResource The child of builder. Returns
IResourceBuilder<T> A resource builder. Remarks
The WithChildRelationship method is used to add child relationships to the resource. Relationships are used to link resources together in UI.
var builder = DistributedApplication.CreateBuilder(args);
var parameter = builder.AddParameter("parameter");
var backend = builder.AddProject<Projects.Backend>("backend"); .WithChildRelationship(parameter.Resource);WithCommand(IResourceBuilder<T>, string, string, Func<ExecuteCommandContext, Task<ExecuteCommandResult>>, CommandOptions?) Section titled WithCommand(IResourceBuilder<T>, string, string, Func<ExecuteCommandContext, Task<ExecuteCommandResult>>, CommandOptions?) extension IResourceBuilder<T> ResourceCommandAnnotation to the resource annotations to add a resource command. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithCommand<T>( this IResourceBuilder<T> builder, string name, string displayName, Func<ExecuteCommandContext, Task<ExecuteCommandResult>> executeCommand, CommandOptions? commandOptions = null) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. name string The name of the command. The name uniquely identifies the command. displayName string The display name visible in UI. executeCommand Func<ExecuteCommandContext, Task<ExecuteCommandResult>> A callback that is executed when the command is executed. The callback is run inside the .NET Aspire host. The callback result is used to indicate success or failure in the UI. commandOptions CommandOptions? optional Optional configuration for the command. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
The WithCommand method is used to add commands to the resource. Commands are displayed in the dashboard and can be executed by a user using the dashboard UI.
When a command is executed, the executeCommand callback is called and is run inside the .NET Aspire host.
WithCommand(IResourceBuilder<T>, string, string, Func<ExecuteCommandContext, Task<ExecuteCommandResult>>, Func<UpdateCommandStateContext, ResourceCommandState>, string?, object?, string?, string?, IconVariant?, bool) Section titled WithCommand(IResourceBuilder<T>, string, string, Func<ExecuteCommandContext, Task<ExecuteCommandResult>>, Func<UpdateCommandStateContext, ResourceCommandState>, string?, object?, string?, string?, IconVariant?, bool) extension IResourceBuilder<T> ResourceCommandAnnotation to the resource annotations to add a resource command. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithCommand<T>( this IResourceBuilder<T> builder, string name, string displayName, Func<ExecuteCommandContext, Task<ExecuteCommandResult>> executeCommand, Func<UpdateCommandStateContext, ResourceCommandState>? updateState = null, string? displayDescription = null, object? parameter = null, string? confirmationMessage = null, string? iconName = null, IconVariant? iconVariant = null, bool isHighlighted = false) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. name string The name of the command. The name uniquely identifies the command. displayName string The display name visible in UI. executeCommand Func<ExecuteCommandContext, Task<ExecuteCommandResult>> A callback that is executed when the command is executed. The callback is run inside the .NET Aspire host. The callback result is used to indicate success or failure in the UI. updateState Func<UpdateCommandStateContext, ResourceCommandState> optional A callback that is used to update the command state. The callback is executed when the command's resource snapshot is updated.
If a callback isn't specified, the command is always enabled.
displayDescription string? optional Optional description of the command, to be shown in the UI. Could be used as a tooltip. May be localized. parameter object? optional Optional parameter that configures the command in some way. Clients must return any value provided by the server when invoking the command. confirmationMessage string? optional When a confirmation message is specified, the UI will prompt with an OK/Cancel dialog and the confirmation message before starting the command. iconName string? optional The icon name for the command. The name should be a valid FluentUI icon name from https://aka.ms/fluentui-system-icons iconVariant IconVariant? optional The icon variant. isHighlighted bool optional A flag indicating whether the command is highlighted in the UI. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
The WithCommand method is used to add commands to the resource. Commands are displayed in the dashboard and can be executed by a user using the dashboard UI.
When a command is executed, the executeCommand callback is called and is run inside the .NET Aspire host.
WithComputeEnvironment(IResourceBuilder<T>, IResourceBuilder<IComputeEnvironmentResource>) Section titled WithComputeEnvironment(IResourceBuilder<T>, IResourceBuilder<IComputeEnvironmentResource>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithComputeEnvironment<T>( this IResourceBuilder<T> builder, IResourceBuilder<IComputeEnvironmentResource> computeEnvironmentResource) { // ... }}Parameters
builder IResourceBuilder<T> The compute resource builder. computeEnvironmentResource IResourceBuilder<IComputeEnvironmentResource> The compute environment resource to associate with the compute resource. Returns
IResourceBuilder<T> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
WithConnectionProperty(IResourceBuilder<T>, string, ReferenceExpression) Section titled WithConnectionProperty(IResourceBuilder<T>, string, ReferenceExpression) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithConnectionProperty<T>( this IResourceBuilder<T> builder, string name, ReferenceExpression value) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder to which the connection property annotation will be added. Cannot be null. name string The name of the connection property to annotate. Cannot be null. value ReferenceExpression The value of the connection property, specified as a reference expression. Returns
IResourceBuilder<T> The same resource builder instance with the connection property annotation applied. Remarks
WithConnectionProperty(IResourceBuilder<T>, string, string) Section titled WithConnectionProperty(IResourceBuilder<T>, string, string) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithConnectionProperty<T>( this IResourceBuilder<T> builder, string name, string value) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder to which the connection property will be added. Cannot be null. name string The name of the connection property to add. Cannot be null. value string The value to assign to the connection property. Returns
IResourceBuilder<T> The same resource builder instance with the specified connection property annotation applied. WithConnectionStringRedirection(IResourceBuilder<T>, IResourceWithConnectionString) Section titled WithConnectionStringRedirection(IResourceBuilder<T>, IResourceWithConnectionString) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithConnectionStringRedirection<T>( this IResourceBuilder<T> builder, IResourceWithConnectionString resource) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. resource IResourceWithConnectionString Resource to which connection string generation is redirected. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithContainerFilesSource(IResourceBuilder<T>, string) Section titled WithContainerFilesSource(IResourceBuilder<T>, string) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithContainerFilesSource<T>( this IResourceBuilder<T> builder, string sourcePath) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder to which the container files source annotation will be added. Cannot be null. sourcePath string The path to the container files source to associate with the resource. Cannot be null. Returns
IResourceBuilder<T> The resource builder instance with the container files source annotation applied. WithDebugSupport(IResourceBuilder<T>, Func<string, TLaunchConfiguration>, string, Action<CommandLineArgsCallbackContext>) Section titled WithDebugSupport(IResourceBuilder<T>, Func<string, TLaunchConfiguration>, string, Action<CommandLineArgsCallbackContext>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithDebugSupport<T, TLaunchConfiguration>( this IResourceBuilder<T> builder, Func<string, TLaunchConfiguration> launchConfigurationProducer, string launchConfigurationType, Action<CommandLineArgsCallbackContext>? argsCallback = null) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. launchConfigurationProducer Func<string, TLaunchConfiguration> Launch configuration producer for the resource. launchConfigurationType string The type of the resource. argsCallback Action<CommandLineArgsCallbackContext> optional Optional callback to add or modify command line arguments when running in an extension host. Useful if the entrypoint is usually provided as an argument to the resource executable. WithDeveloperCertificateTrust(IResourceBuilder<TResource>, bool) Section titled WithDeveloperCertificateTrust(IResourceBuilder<TResource>, bool) extension IResourceBuilder<TResource> public static class ResourceBuilderExtensions{ public static IResourceBuilder<TResource> WithDeveloperCertificateTrust<TResource>( this IResourceBuilder<TResource> builder, bool trust) { // ... }}Parameters
builder IResourceBuilder<TResource> The resource builder. trust bool Indicates whether the developer certificate should be treated as trusted. Returns
IResourceBuilder<TResource> The ApplicationModel.IResourceBuilder`1. Remarks
var container = builder.AddContainer("my-service", "my-service:latest") .WithDeveloperCertificateTrust(false);var builder = DistributedApplication.CreateBuilder( new DistributedApplicationOptions(){ Args = args, TrustDeveloperCertificate = false,});var project = builder.AddProject<MyService>("my-service") .WithDeveloperCertificateTrust(true);WithEndpoint(IResourceBuilder<T>, string, Action<EndpointAnnotation>, bool) Section titled WithEndpoint(IResourceBuilder<T>, string, Action<EndpointAnnotation>, bool) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEndpoint<T>( this IResourceBuilder<T> builder, string endpointName, Action<EndpointAnnotation> callback, bool createIfNotExists = true) { // ... }}Parameters
builder IResourceBuilder<T> Resource builder for resource with endpoints. endpointName string Name of endpoint to change. callback Action<EndpointAnnotation> Callback that modifies the endpoint. createIfNotExists bool optional Create endpoint if it does not exist. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
The ResourceBuilderExtensions.WithEndpoint method allows developers to mutate any aspect of an endpoint annotation. Note that changing one value does not automatically change other values to compatible/consistent values. For example setting the EndpointAnnotation.Protocol property of the endpoint annotation in the callback will not automatically change the EndpointAnnotation.UriScheme. All values should be set in the callback if the defaults are not acceptable.
var builder = DistributedApplication.Create(args);var container = builder.AddContainer("mycontainer", "myimage") .WithEndpoint("myendpoint", e => { e.Port = 9998; e.TargetPort = 9999; e.Protocol = ProtocolType.Udp; e.UriScheme = "udp"; });WithEndpoint(IResourceBuilder<T>, int?, int?, string?, string?, string?, bool, bool?, ProtocolType?) Section titled WithEndpoint(IResourceBuilder<T>, int?, int?, string?, string?, string?, bool, bool?, ProtocolType?) extension IResourceBuilder<T> ResourceBuilderExtensions.GetEndpoint. The endpoint name will be the scheme name if not specified. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEndpoint<T>( this IResourceBuilder<T> builder, int? port = null, int? targetPort = null, string? scheme = null, string? name = null, string? env = null, bool isProxied = true, bool? isExternal = null, ProtocolType? protocol = null) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. port int? optional An optional port. This is the port that will be given to other resource to communicate with this resource. targetPort int? optional This is the port the resource is listening on. If the endpoint is used for the container, it is the container port. scheme string? optional An optional scheme e.g. (http/https). Defaults to the protocol argument if it is defined or "tcp" otherwise. name string? optional An optional name of the endpoint. Defaults to the scheme name if not specified. env string? optional An optional name of the environment variable that will be used to inject the targetPort. If the target port is null one will be dynamically generated and assigned to the environment variable. isProxied bool optional Specifies if the endpoint will be proxied by DCP. Defaults to true. isExternal bool? optional Indicates that this endpoint should be exposed externally at publish time. protocol ProtocolType? optional Network protocol: TCP or UDP are supported today, others possibly in future. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Exceptions
DistributedApplicationException Throws an exception if an endpoint with the same name already exists on the specified resource. WithEndpoint(IResourceBuilder<T>, int?, int?, string?, string?, string?, bool, bool?) Section titled WithEndpoint(IResourceBuilder<T>, int?, int?, string?, string?, string?, bool, bool?) extension IResourceBuilder<T> ResourceBuilderExtensions.GetEndpoint. The endpoint name will be the scheme name if not specified. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEndpoint<T>( this IResourceBuilder<T> builder, int? port, int? targetPort, string? scheme, string? name, string? env, bool isProxied, bool? isExternal) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. port int? An optional port. This is the port that will be given to other resource to communicate with this resource. targetPort int? This is the port the resource is listening on. If the endpoint is used for the container, it is the container port. scheme string? An optional scheme e.g. (http/https). Defaults to "tcp" if not specified. name string? An optional name of the endpoint. Defaults to the scheme name if not specified. env string? An optional name of the environment variable that will be used to inject the targetPort. If the target port is null one will be dynamically generated and assigned to the environment variable. isProxied bool Specifies if the endpoint will be proxied by DCP. Defaults to true. isExternal bool? Indicates that this endpoint should be exposed externally at publish time. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Exceptions
DistributedApplicationException Throws an exception if an endpoint with the same name already exists on the specified resource. WithEnvironment(IResourceBuilder<T>, string, string?) Section titled WithEnvironment(IResourceBuilder<T>, string, string?) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEnvironment<T>( this IResourceBuilder<T> builder, string name, string? value) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. name string The name of the environment variable. value string? The value of the environment variable. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithEnvironment(IResourceBuilder<T>, string, ExpressionInterpolatedStringHandler) Section titled WithEnvironment(IResourceBuilder<T>, string, ExpressionInterpolatedStringHandler) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEnvironment<T>( this IResourceBuilder<T> builder, string name, in ExpressionInterpolatedStringHandler value) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. name string The name of the environment variable. value ExpressionInterpolatedStringHandler The value of the environment variable. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithEnvironment(IResourceBuilder<T>, string, ReferenceExpression) Section titled WithEnvironment(IResourceBuilder<T>, string, ReferenceExpression) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEnvironment<T>( this IResourceBuilder<T> builder, string name, ReferenceExpression value) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. name string The name of the environment variable. value ReferenceExpression The value of the environment variable. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithEnvironment(IResourceBuilder<T>, string, Func<string>) Section titled WithEnvironment(IResourceBuilder<T>, string, Func<string>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEnvironment<T>( this IResourceBuilder<T> builder, string name, Func<string> callback) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. name string The name of the environment variable. callback Func<string> A callback that allows for deferred execution of a specific environment variable. This runs after resources have been allocated by the orchestrator and allows access to other resources to resolve computed data, e.g. connection strings, ports. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithEnvironment(IResourceBuilder<T>, Action<EnvironmentCallbackContext>) Section titled WithEnvironment(IResourceBuilder<T>, Action<EnvironmentCallbackContext>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEnvironment<T>( this IResourceBuilder<T> builder, Action<EnvironmentCallbackContext> callback) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. callback Action<EnvironmentCallbackContext> A callback that allows for deferred execution for computing many environment variables. This runs after resources have been allocated by the orchestrator and allows access to other resources to resolve computed data, e.g. connection strings, ports. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithEnvironment(IResourceBuilder<T>, Func<EnvironmentCallbackContext, Task>) Section titled WithEnvironment(IResourceBuilder<T>, Func<EnvironmentCallbackContext, Task>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEnvironment<T>( this IResourceBuilder<T> builder, Func<EnvironmentCallbackContext, Task> callback) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. callback Func<EnvironmentCallbackContext, Task> A callback that allows for deferred execution for computing many environment variables. This runs after resources have been allocated by the orchestrator and allows access to other resources to resolve computed data, e.g. connection strings, ports. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithEnvironment(IResourceBuilder<T>, string, EndpointReference) Section titled WithEnvironment(IResourceBuilder<T>, string, EndpointReference) extension IResourceBuilder<T> endpointReference. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEnvironment<T>( this IResourceBuilder<T> builder, string name, EndpointReference endpointReference) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. name string The name of the environment variable. endpointReference EndpointReference The endpoint from which to extract the url. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithEnvironment(IResourceBuilder<T>, string, IResourceBuilder<ExternalServiceResource>) Section titled WithEnvironment(IResourceBuilder<T>, string, IResourceBuilder<ExternalServiceResource>) extension IResourceBuilder<T> ExternalServiceResource. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEnvironment<T>( this IResourceBuilder<T> builder, string name, IResourceBuilder<ExternalServiceResource> externalService) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. name string The name of the environment variable. externalService IResourceBuilder<ExternalServiceResource> The external service. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithEnvironment(IResourceBuilder<T>, string, IResourceBuilder<ParameterResource>) Section titled WithEnvironment(IResourceBuilder<T>, string, IResourceBuilder<ParameterResource>) extension IResourceBuilder<T> parameter. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEnvironment<T>( this IResourceBuilder<T> builder, string name, IResourceBuilder<ParameterResource> parameter) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. name string Name of environment variable. parameter IResourceBuilder<ParameterResource> Resource builder for the parameter resource. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithEnvironment(IResourceBuilder<T>, string, IResourceBuilder<IResourceWithConnectionString>) Section titled WithEnvironment(IResourceBuilder<T>, string, IResourceBuilder<IResourceWithConnectionString>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEnvironment<T>( this IResourceBuilder<T> builder, string envVarName, IResourceBuilder<IResourceWithConnectionString> resource) { // ... }}Parameters
builder IResourceBuilder<T> The destination resource builder to which the environment variable will be added. envVarName string The name of the environment variable under which the connection string will be set. resource IResourceBuilder<IResourceWithConnectionString> The resource builder of the referenced service from which to pull the connection string. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithEnvironment(IResourceBuilder<T>, string, TValue) Section titled WithEnvironment(IResourceBuilder<T>, string, TValue) extension IResourceBuilder<T> IValueProvider and IManifestExpressionProvider. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithEnvironment<T, TValue>( this IResourceBuilder<T> builder, string name, TValue value) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. name string The name of the environment variable. value TValue The value that provides both runtime values and manifest expressions. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithExplicitStart(IResourceBuilder<T>) Section titled WithExplicitStart(IResourceBuilder<T>) extension IResourceBuilder<T> ExplicitStartupAnnotation annotation to the resource so it doesn't automatically start with the app host startup. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithExplicitStart<T>( this IResourceBuilder<T> builder) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
This method is useful when a resource shouldn't automatically start when the app host starts.
The database clean up tool project isn't started with the app host. The resource start command can be used to run it ondemand later.var builder = DistributedApplication.CreateBuilder(args);var pgsql = builder.AddPostgres("postgres");builder.AddProject<Projects.CleanUpDatabase>("dbcleanuptool") .WithReference(pgsql) .WithExplicitStart();WithExternalHttpEndpoints(IResourceBuilder<T>) Section titled WithExternalHttpEndpoints(IResourceBuilder<T>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithExternalHttpEndpoints<T>( this IResourceBuilder<T> builder) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithHealthCheck(IResourceBuilder<T>, string) Section titled WithHealthCheck(IResourceBuilder<T>, string) extension IResourceBuilder<T> HealthCheckAnnotation to the resource annotations to associate a resource with a named health check managed by the health check service. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithHealthCheck<T>( this IResourceBuilder<T> builder, string key) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. key string The key for the health check. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
The ResourceBuilderExtensions.WithHealthCheck method is used in conjunction with the ResourceBuilderExtensions.WaitFor to associate a resource registered in the application hosts dependency injection container. The ResourceBuilderExtensions.WithHealthCheck method does not inject the health check itself it is purely an association mechanism.
var builder = DistributedApplication.CreateBuilder(args);
var startAfter = DateTime.Now.AddSeconds(30);
builder.Services.AddHealthChecks().AddCheck(mycheck", () =>{ return DateTime.Now > startAfter ? HealthCheckResult.Healthy( ) : HealthCheckResult.Unhealthy();});
var pg = builder.AddPostgres("pg") .WithHealthCheck("mycheck");
builder.AddProject<Projects.MyApp>("myapp") .WithReference(pg) .WaitFor( pg); // This will result in waiting for the building check, and the // custom check defined in the code.WithHttpCommand(IResourceBuilder<TResource>, string, string, string?, string?, HttpCommandOptions?) Section titled WithHttpCommand(IResourceBuilder<TResource>, string, string, string?, string?, HttpCommandOptions?) extension IResourceBuilder<TResource> public static class ResourceBuilderExtensions{ public static IResourceBuilder<TResource> WithHttpCommand<TResource>( this IResourceBuilder<TResource> builder, string path, string displayName, string? endpointName = null, string? commandName = null, HttpCommandOptions? commandOptions = null) { // ... }}Parameters
builder IResourceBuilder<TResource> The resource builder. path string The path to send the request to when the command is invoked. displayName string The display name visible in UI. endpointName string? optional The name of the HTTP endpoint on this resource to send the request to when the command is invoked. commandName string? optional Optional name of the command. The name uniquely identifies the command. If a name isn't specified then it's inferred using the command's endpoint and HTTP method. commandOptions HttpCommandOptions? optional Optional configuration for the command. Returns
IResourceBuilder<TResource> The ApplicationModel.IResourceBuilder`1. Remarks
The command will be added to the resource represented by builder.
If endpointName is specified, the request will be sent to the endpoint with that name on the resource represented by builder. If an endpoint with that name is not found, or the endpoint with that name is not an HTTP endpoint, an exception will be thrown.
If no endpointName is specified, the first HTTP endpoint found on the resource will be used. HTTP endpoints with an https scheme are preferred over those with an http scheme. If no HTTP endpoint is found on the resource, an exception will be thrown.
The command will not be enabled until the endpoint is allocated and the resource the endpoint is associated with is healthy.
If HttpCommandOptions.Method is not specified, POST will be used.
Specifying HttpCommandOptions.HttpClientName will use that named Http.HttpClient when sending the request. This allows you to configure the Http.HttpClient instance with a specific handler or other options using HttpClientFactoryServiceCollectionExtensions.AddHttpClient. If HttpCommandOptions.HttpClientName is not specified, the default Http.HttpClient will be used.
The HttpCommandOptions.PrepareRequest callback will be invoked to configure the request before it is sent. This can be used to add headers or a request payload before the request is sent.
The HttpCommandOptions.GetCommandResult callback will be invoked after the response is received to determine the result of the command invocation. If this callback is not specified, the command will be considered succesful if the response status code is in the 2xx range.
/clear-cache. var apiService = builder.AddProject>MyApiService>("api") .WithHttpCommand("/clear-cache", "Clear cache");/reset-db on endpoint named admin. The request's headers are configured to include an X-Admin-Key header for verification. var adminKey = builder.AddParameter("admin-key");var apiService = builder.AddProject>MyApiService>("api") .WithHttpsEndpoint("admin") .WithEnvironment("ADMIN_KEY", adminKey) .WithHttpCommand("/reset-db", "Reset database", endpointName: "admin", commandOptions: new () { Method = HttpMethod.Get, ConfirmationMessage = "Are you sure you want to reset the database?", PrepareRequest: request => { request.Headers.Add("X-Admin-Key", adminKey); return Task.CompletedTask; } });WithHttpCommand(IResourceBuilder<TResource>, string, string, Func<EndpointReference>, string?, HttpCommandOptions?) Section titled WithHttpCommand(IResourceBuilder<TResource>, string, string, Func<EndpointReference>, string?, HttpCommandOptions?) extension IResourceBuilder<TResource> public static class ResourceBuilderExtensions{ public static IResourceBuilder<TResource> WithHttpCommand<TResource>( this IResourceBuilder<TResource> builder, string path, string displayName, Func<EndpointReference>? endpointSelector, string? commandName = null, HttpCommandOptions? commandOptions = null) { // ... }}Parameters
builder IResourceBuilder<TResource> The resource builder. path string The path to send the request to when the command is invoked. displayName string The display name visible in UI. endpointSelector Func<EndpointReference> A callback that selects the HTTP endpoint to send the request to when the command is invoked. commandName string? optional The name of command. The name uniquely identifies the command. commandOptions HttpCommandOptions? optional Optional configuration for the command. Returns
IResourceBuilder<TResource> The ApplicationModel.IResourceBuilder`1. Exceptions
Remarks
The command will be added to the resource represented by builder.
If no HttpCommandOptions.EndpointSelector is specified, the first HTTP endpoint found on the resource will be used. HTTP endpoints with an https scheme are preferred over those with an http scheme. If no HTTP endpoint is found on the resource, an exception will be thrown.
The supplied HttpCommandOptions.EndpointSelector may return an endpoint from a different resource to that which the command is being added to.
The command will not be enabled until the endpoint is allocated and the resource the endpoint is associated with is healthy.
If HttpCommandOptions.Method is not specified, POST will be used.
Specifying a HttpCommandOptions.HttpClientName will use that named Http.HttpClient when sending the request. This allows you to configure the Http.HttpClient instance with a specific handler or other options using HttpClientFactoryServiceCollectionExtensions.AddHttpClient. If no HttpCommandOptions.HttpClientName is specified, the default Http.HttpClient will be used.
The HttpCommandOptions.PrepareRequest callback will be invoked to configure the request before it is sent. This can be used to add headers or a request payload before the request is sent.
The HttpCommandOptions.GetCommandResult callback will be invoked after the response is received to determine the result of the command invocation. If this callback is not specified, the command will be considered succesful if the response status code is in the 2xx range.
var loadGenerator = builder.AddProject>LoadGenerator>("load");var loadGeneratorEndpoint = loadGenerator.GetEndpoint("https");var customerService = builder.AddProject>CustomerService>("customer-service") .WithHttpCommand( "/stress?resource=customer-service&requests=1000", "Apply load (1000)", endpointSelector: () => loadGeneratorEndpoint) .WithHttpCommand( "/stress?resource=customer-service&requests=5000", "Apply load (5000)", endpointSelector: () => loadGeneratorEndpoint);loadGenerator.WithReference(customerService);WithHttpEndpoint(IResourceBuilder<T>, int?, int?, string?, string?, bool) Section titled WithHttpEndpoint(IResourceBuilder<T>, int?, int?, string?, string?, bool) extension IResourceBuilder<T> ResourceBuilderExtensions.GetEndpoint. The endpoint name will be "http" if not specified. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithHttpEndpoint<T>( this IResourceBuilder<T> builder, int? port = null, int? targetPort = null, string? name = null, string? env = null, bool isProxied = true) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. port int? optional An optional port. This is the port that will be given to other resource to communicate with this resource. targetPort int? optional This is the port the resource is listening on. If the endpoint is used for the container, it is the container port. name string? optional An optional name of the endpoint. Defaults to "http" if not specified. env string? optional An optional name of the environment variable to inject. isProxied bool optional Specifies if the endpoint will be proxied by DCP. Defaults to true. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Exceptions
DistributedApplicationException Throws an exception if an endpoint with the same name already exists on the specified resource. WithHttpHealthCheck(IResourceBuilder<T>, string?, int?, string?) Section titled WithHttpHealthCheck(IResourceBuilder<T>, string?, int?, string?) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithHttpHealthCheck<T>( this IResourceBuilder<T> builder, string? path = null, int? statusCode = null, string? endpointName = null) { // ... }}Parameters
builder IResourceBuilder<T> A resource builder. path string? optional The relative path to test. statusCode int? optional The result code to interpret as healthy. endpointName string? optional The name of the endpoint to derive the base address from. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
This method adds a health check to the health check service which polls the specified endpoint on the resource on a periodic basis. The base address is dynamically determined based on the endpoint that was selected. By default the path is set to "/" and the status code is set to 200.
This example shows adding an HTTP health check to a backend project. The health check makes sure that the front end does not start until the backend is reporting a healthy status based on the return code returned from the "/health" path on the backend server.var builder = DistributedApplication.CreateBuilder(args);var backend = builder.AddProject<Projects.Backend>("backend") .WithHttpHealthCheck("/health");builder.AddProject<Projects.Frontend>("frontend") .WithReference(backend).WaitFor(backend);WithHttpHealthCheck(IResourceBuilder<T>, Func<EndpointReference>, string?, int?) Section titled WithHttpHealthCheck(IResourceBuilder<T>, Func<EndpointReference>, string?, int?) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithHttpHealthCheck<T>( this IResourceBuilder<T> builder, Func<EndpointReference>? endpointSelector, string? path = null, int? statusCode = null) { // ... }}Parameters
builder IResourceBuilder<T> A resource builder. endpointSelector Func<EndpointReference> path string? optional The relative path to test. statusCode int? optional The result code to interpret as healthy. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
This method adds a health check to the health check service which polls the specified endpoint on a periodic basis. The base address is dynamically determined based on the endpoint that was selected. By default the path is set to "/" and the status code is set to 200.
This example shows adding an HTTP health check to a backend project. The health check makes sure that the front end does not start until the backend is reporting a healthy status based on the return code returned from the "/health" path on the backend server.var builder = DistributedApplication.CreateBuilder(args);var backend = builder.AddProject<Projects.Backend>("backend");backend.WithHttpHealthCheck(() => backend.GetEndpoint("https"), path: "/health")builder.AddProject<Projects.Frontend>("frontend") .WithReference(backend).WaitFor(backend);WithHttpProbe(IResourceBuilder<T>, ProbeType, string?, int?, int?, int?, int?, int?, string?) Section titled WithHttpProbe(IResourceBuilder<T>, ProbeType, string?, int?, int?, int?, int?, int?, string?) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithHttpProbe<T>( this IResourceBuilder<T> builder, ProbeType type, string? path = null, int? initialDelaySeconds = null, int? periodSeconds = null, int? timeoutSeconds = null, int? failureThreshold = null, int? successThreshold = null, string? endpointName = null) { // ... }}Parameters
builder IResourceBuilder<T> Resource builder. type ProbeType Type of the probe. path string? optional The path to be used. initialDelaySeconds int? optional The initial delay before calling the probe endpoint for the first time. periodSeconds int? optional The period between each probe. timeoutSeconds int? optional Number of seconds after which the probe times out. failureThreshold int? optional Number of failures in a row before considers that the overall check has failed. successThreshold int? optional Minimum consecutive successes for the probe to be considered successful after having failed. endpointName string? optional The name of the endpoint to be used for the probe. Returns
IResourceBuilder<T> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
This method allows you to specify a probe and implicit adds an http health check to the resource based on probe parameters.
For example add a probe to a resource in this way:var service = builder.AddProject<Projects.MyService>("service") .WithHttpProbe(ProbeType.Liveness, "/health");var service = builder.AddProject<Projects.MyService>("service") .WithHttpProbe(ProbeType.Liveness, "/health") .WithHttpHealthCheck("/health");WithHttpProbe(IResourceBuilder<T>, ProbeType, Func<EndpointReference>, string?, int?, int?, int?, int?, int?) Section titled WithHttpProbe(IResourceBuilder<T>, ProbeType, Func<EndpointReference>, string?, int?, int?, int?, int?, int?) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithHttpProbe<T>( this IResourceBuilder<T> builder, ProbeType type, Func<EndpointReference>? endpointSelector, string? path = null, int? initialDelaySeconds = null, int? periodSeconds = null, int? timeoutSeconds = null, int? failureThreshold = null, int? successThreshold = null) { // ... }}Parameters
builder IResourceBuilder<T> Resource builder. type ProbeType Type of the probe. endpointSelector Func<EndpointReference> The selector used to get endpoint reference. path string? optional The path to be used. initialDelaySeconds int? optional The initial delay before calling the probe endpoint for the first time. periodSeconds int? optional The period between each probe. timeoutSeconds int? optional Number of seconds after which the probe times out. failureThreshold int? optional Number of failures in a row before considers that the overall check has failed. successThreshold int? optional Minimum consecutive successes for the probe to be considered successful after having failed. Returns
IResourceBuilder<T> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
This method allows you to specify a probe and implicit adds an http health check to the resource based on probe parameters.
For example add a probe to a resource in this way:var service = builder.AddProject<Projects.MyService>("service") .WithHttpProbe(ProbeType.Liveness, "/health");var service = builder.AddProject<Projects.MyService>("service") .WithHttpProbe(ProbeType.Liveness, "/health") .WithHttpHealthCheck("/health");WithHttpsCertificate(IResourceBuilder<TResource>, X509Certificate2, IResourceBuilder<ParameterResource>) Section titled WithHttpsCertificate(IResourceBuilder<TResource>, X509Certificate2, IResourceBuilder<ParameterResource>) extension IResourceBuilder<TResource> HttpsCertificateAnnotation to the resource annotations to associate an X.509 certificate key pair with the resource. This is used to configure the certificate presented by the resource for HTTPS/TLS endpoints. public static class ResourceBuilderExtensions{ public static IResourceBuilder<TResource> WithHttpsCertificate<TResource>( this IResourceBuilder<TResource> builder, X509Certificate2 certificate, IResourceBuilder<ParameterResource>? password = null) { // ... }}Parameters
builder IResourceBuilder<TResource> The resource builder. certificate X509Certificate2 An X509Certificates.X509Certificate2 key pair to use for HTTPS/TLS endpoints on the resource. password IResourceBuilder<ParameterResource> optional A parameter specifying the password used to encrypt the certificate private key. Returns
IResourceBuilder<TResource> The ApplicationModel.IResourceBuilder`1. Remarks
var certificate = new X509Certificate2("path/to/certificate.pfx", "password");builder.AddContainer("my-service", "my-image") .WithHttpsCertificate(certificate);WithHttpsCertificateConfiguration(IResourceBuilder<TResource>, Func<HttpsCertificateConfigurationCallbackAnnotationContext, Task>) Section titled WithHttpsCertificateConfiguration(IResourceBuilder<TResource>, Func<HttpsCertificateConfigurationCallbackAnnotationContext, Task>) extension IResourceBuilder<TResource> public static class ResourceBuilderExtensions{ public static IResourceBuilder<TResource> WithHttpsCertificateConfiguration<TResource>( this IResourceBuilder<TResource> builder, Func<HttpsCertificateConfigurationCallbackAnnotationContext, Task> callback) { // ... }}Parameters
builder IResourceBuilder<TResource> The resource builder. callback Func<HttpsCertificateConfigurationCallbackAnnotationContext, Task> The callback to configure the resource to use a certificate key pair. Returns
IResourceBuilder<TResource> The updated resource builder. Remarks
builder.AddContainer("my-service", "my-image") .WithHttpsCertificateConfiguration(ctx => { ctx.Arguments.Add("--https-certificate-path"); ctx.Arguments.Add(ctx.PfxPath); return Task.CompletedTask; });WithHttpsDeveloperCertificate(IResourceBuilder<TResource>, IResourceBuilder<ParameterResource>) Section titled WithHttpsDeveloperCertificate(IResourceBuilder<TResource>, IResourceBuilder<ParameterResource>) extension IResourceBuilder<TResource> public static class ResourceBuilderExtensions{ public static IResourceBuilder<TResource> WithHttpsDeveloperCertificate<TResource>( this IResourceBuilder<TResource> builder, IResourceBuilder<ParameterResource>? password = null) { // ... }}Parameters
builder IResourceBuilder<TResource> The resource builder. password IResourceBuilder<ParameterResource> optional A parameter specifying the password used to encrypt the certificate private key. Returns
IResourceBuilder<TResource> The ApplicationModel.IResourceBuilder`1. Remarks
builder.AddContainer("my-service", "my-image") .WithHttpsDeveloperCertificate()WithHttpsEndpoint(IResourceBuilder<T>, int?, int?, string?, string?, bool) Section titled WithHttpsEndpoint(IResourceBuilder<T>, int?, int?, string?, string?, bool) extension IResourceBuilder<T> ResourceBuilderExtensions.GetEndpoint. The endpoint name will be "https" if not specified. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithHttpsEndpoint<T>( this IResourceBuilder<T> builder, int? port = null, int? targetPort = null, string? name = null, string? env = null, bool isProxied = true) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. port int? optional An optional host port. targetPort int? optional This is the port the resource is listening on. If the endpoint is used for the container, it is the container port. name string? optional An optional name of the endpoint. Defaults to "https" if not specified. env string? optional An optional name of the environment variable to inject. isProxied bool optional Specifies if the endpoint will be proxied by DCP. Defaults to true. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Exceptions
DistributedApplicationException Throws an exception if an endpoint with the same name already exists on the specified resource. WithHttpsHealthCheck(IResourceBuilder<T>, string?, int?, string?) Section titled WithHttpsHealthCheck(IResourceBuilder<T>, string?, int?, string?) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithHttpsHealthCheck<T>( this IResourceBuilder<T> builder, string? path = null, int? statusCode = null, string? endpointName = null) { // ... }}Parameters
builder IResourceBuilder<T> A resource builder. path string? optional The relative path to test. statusCode int? optional The result code to interpret as healthy. endpointName string? optional The name of the endpoint to derive the base address from. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
This method adds a health check to the health check service which polls the specified endpoint on the resource on a periodic basis. The base address is dynamically determined based on the endpoint that was selected. By default the path is set to "/" and the status code is set to 200.
This example shows adding an HTTPS health check to a backend project. The health check makes sure that the front end does not start until the backend is reporting a healthy status based on the return code returned from the "/health" path on the backend server.var builder = DistributedApplication.CreateBuilder(args);var backend = builder.AddProject<Projects.Backend>("backend") .WithHttpsHealthCheck("/health");builder.AddProject<Projects.Frontend>("frontend") .WithReference(backend).WaitFor(backend);WithIconName(IResourceBuilder<T>, string, IconVariant) Section titled WithIconName(IResourceBuilder<T>, string, IconVariant) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithIconName<T>( this IResourceBuilder<T> builder, string iconName, IconVariant iconVariant = IconVariant.Filled) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. iconName string The name of the FluentUI icon to use. See https://aka.ms/fluentui-system-icons for available icons. iconVariant IconVariant optional The variant of the icon (Regular or Filled). Defaults to Filled. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
This method allows you to specify a custom FluentUI icon that will be displayed for the resource in the dashboard. If no custom icon is specified, the dashboard will use default icons based on the resource type.
Set a Redis resource to use the Database icon:var redis = builder.AddContainer("redis", "redis:latest") .WithIconName("Database");var service = builder.AddProject<Projects.MyService>("service") .WithIconName("CloudArrowUp", IconVariant.Regular);WithImagePushOptions(IResourceBuilder<T>, Action<ContainerImagePushOptionsCallbackContext>) Section titled WithImagePushOptions(IResourceBuilder<T>, Action<ContainerImagePushOptionsCallbackContext>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithImagePushOptions<T>( this IResourceBuilder<T> builder, Action<ContainerImagePushOptionsCallbackContext> callback) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. callback Action<ContainerImagePushOptionsCallbackContext> The callback to configure push options. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Exceptions
ArgumentNullException Thrown when builder or callback is null. Remarks
ContainerImagePushOptionsCallbackContext that provides access to the resource and the ContainerImagePushOptions that can be modified. Multiple callbacks can be registered on the same resource, and they will be invoked in the order they were added. Examples
Configure a custom image name and tag for a container resource:
var container = builder.AddContainer("myapp", "myapp:latest") .WithImagePushOptions(context => { context.Options.RemoteImageName = "myorg/myapp"; context.Options.RemoteImageTag = "v1.0.0"; });WithImagePushOptions(IResourceBuilder<T>, Func<ContainerImagePushOptionsCallbackContext, Task>) Section titled WithImagePushOptions(IResourceBuilder<T>, Func<ContainerImagePushOptionsCallbackContext, Task>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithImagePushOptions<T>( this IResourceBuilder<T> builder, Func<ContainerImagePushOptionsCallbackContext, Task> callback) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. callback Func<ContainerImagePushOptionsCallbackContext, Task> The asynchronous callback to configure push options. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Exceptions
ArgumentNullException Thrown when builder or callback is null. Remarks
ContainerImagePushOptionsCallbackContext that provides access to the resource and the ContainerImagePushOptions that can be modified. Multiple callbacks can be registered on the same resource, and they will be invoked in the order they were added. Examples
Configure image options asynchronously by retrieving values from configuration:
var container = builder.AddContainer("myapp", "myapp:latest") .WithImagePushOptions(async context => { var config = await GetConfigurationAsync(); context.Options.RemoteImageName = config.ImageName; context.Options.RemoteImageTag = config.ImageTag; });WithManifestPublishingCallback(IResourceBuilder<T>, Action<ManifestPublishingContext>) Section titled WithManifestPublishingCallback(IResourceBuilder<T>, Action<ManifestPublishingContext>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithManifestPublishingCallback<T>( this IResourceBuilder<T> builder, Action<ManifestPublishingContext> callback) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. callback Action<ManifestPublishingContext> Callback method which takes a ManifestPublishingContext which can be used to inject JSON into the manifest. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithManifestPublishingCallback(IResourceBuilder<T>, Func<ManifestPublishingContext, Task>) Section titled WithManifestPublishingCallback(IResourceBuilder<T>, Func<ManifestPublishingContext, Task>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithManifestPublishingCallback<T>( this IResourceBuilder<T> builder, Func<ManifestPublishingContext, Task> callback) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. callback Func<ManifestPublishingContext, Task> Callback method which takes a ManifestPublishingContext which can be used to inject JSON into the manifest. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. WithoutHttpsCertificate(IResourceBuilder<TResource>) Section titled WithoutHttpsCertificate(IResourceBuilder<TResource>) extension IResourceBuilder<TResource> public static class ResourceBuilderExtensions{ public static IResourceBuilder<TResource> WithoutHttpsCertificate<TResource>( this IResourceBuilder<TResource> builder) { // ... }}Parameters
builder IResourceBuilder<TResource> The resource builder. Returns
IResourceBuilder<TResource> The ApplicationModel.IResourceBuilder`1. Remarks
var redis = builder.AddRedis("cache") .WithoutHttpsCertificate();WithParentRelationship(IResourceBuilder<T>, IResourceBuilder<IResource>) Section titled WithParentRelationship(IResourceBuilder<T>, IResourceBuilder<IResource>) extension IResourceBuilder<T> ResourceRelationshipAnnotation to the resource annotations to add a parent-child relationship. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithParentRelationship<T>( this IResourceBuilder<T> builder, IResourceBuilder<IResource> parent) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. parent IResourceBuilder<IResource> The parent of builder. Returns
IResourceBuilder<T> A resource builder. Remarks
The WithParentRelationship method is used to add parent relationships to the resource. Relationships are used to link resources together in UI.
var builder = DistributedApplication.CreateBuilder(args);var backend = builder.AddProject<Projects.Backend>("backend");
var frontend = builder.AddProject<Projects.Manager>("frontend") .WithParentRelationship(backend);WithParentRelationship(IResourceBuilder<T>, IResource) Section titled WithParentRelationship(IResourceBuilder<T>, IResource) extension IResourceBuilder<T> ResourceRelationshipAnnotation to the resource annotations to add a parent-child relationship. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithParentRelationship<T>( this IResourceBuilder<T> builder, IResource parent) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. parent IResource The parent of builder. Returns
IResourceBuilder<T> A resource builder. Remarks
The WithParentRelationship method is used to add parent relationships to the resource. Relationships are used to link resources together in UI.
var builder = DistributedApplication.CreateBuilder(args);var backend = builder.AddProject<Projects.Backend>("backend");
var frontend = builder.AddProject<Projects.Manager>("frontend") .WithParentRelationship(backend.Resource);WithReference(IResourceBuilder<TDestination>, IResourceBuilder<IResourceWithConnectionString>, string?, bool) Section titled WithReference(IResourceBuilder<TDestination>, IResourceBuilder<IResourceWithConnectionString>, string?, bool) extension IResourceBuilder<TDestination> Each resource defines the format of the connection string value. The underlying connection string value can be retrieved using IResourceWithConnectionString.GetConnectionStringAsync.
Connection strings are also resolved by the configuration system (appSettings.json in the AppHost project, or environment variables). If a connection string is not found on the resource, the configuration system will be queried for a connection string using the resource's name.
public static class ResourceBuilderExtensions{ public static IResourceBuilder<TDestination> WithReference<TDestination>( this IResourceBuilder<TDestination> builder, IResourceBuilder<IResourceWithConnectionString> source, string? connectionName = null, bool optional = false) { // ... }}Parameters
builder IResourceBuilder<TDestination> The resource where connection string will be injected. source IResourceBuilder<IResourceWithConnectionString> The resource from which to extract the connection string. connectionName string? optional An override of the source resource's name for the connection string. The resulting connection string will be "ConnectionStrings__connectionName" if this is not null. optional bool optional true to allow a missing connection string; false to throw an exception if the connection string is not found. Returns
IResourceBuilder<TDestination> The ApplicationModel.IResourceBuilder`1. Exceptions
DistributedApplicationException Throws an exception if the connection string resolves to null. It can be null if the resource has no connection string, and if the configuration has no connection string for the source resource. WithReference(IResourceBuilder<TDestination>, IResourceBuilder<IResourceWithServiceDiscovery>) Section titled WithReference(IResourceBuilder<TDestination>, IResourceBuilder<IResourceWithServiceDiscovery>) extension IResourceBuilder<TDestination> ReferenceEnvironmentInjectionAnnotation on the destination resource, i.e. either "services__{sourceResourceName}__{endpointName}__{endpointIndex}={uriString}" for .NET service discovery, or "{RESOURCE_ENDPOINT}={uri}" for endpoint injection. public static class ResourceBuilderExtensions{ public static IResourceBuilder<TDestination> WithReference<TDestination>( this IResourceBuilder<TDestination> builder, IResourceBuilder<IResourceWithServiceDiscovery> source) { // ... }}Parameters
builder IResourceBuilder<TDestination> The resource where the service discovery information will be injected. source IResourceBuilder<IResourceWithServiceDiscovery> The resource from which to extract service discovery information. Returns
IResourceBuilder<TDestination> The ApplicationModel.IResourceBuilder`1. WithReference(IResourceBuilder<TDestination>, IResourceBuilder<IResourceWithServiceDiscovery>, string) Section titled WithReference(IResourceBuilder<TDestination>, IResourceBuilder<IResourceWithServiceDiscovery>, string) extension IResourceBuilder<TDestination> ReferenceEnvironmentInjectionAnnotation on the destination resource, i.e. either "services__{name}__{endpointName}__{endpointIndex}={uriString}" for .NET service discovery, or "{name}_{ENDPOINT}={uri}" for endpoint injection. public static class ResourceBuilderExtensions{ public static IResourceBuilder<TDestination> WithReference<TDestination>( this IResourceBuilder<TDestination> builder, IResourceBuilder<IResourceWithServiceDiscovery> source, string name) { // ... }}Parameters
builder IResourceBuilder<TDestination> The resource where the service discovery information will be injected. source IResourceBuilder<IResourceWithServiceDiscovery> The resource from which to extract service discovery information. name string The name of the resource for the environment variable. Returns
IResourceBuilder<TDestination> The ApplicationModel.IResourceBuilder`1. WithReference(IResourceBuilder<TDestination>, string, Uri) Section titled WithReference(IResourceBuilder<TDestination>, string, Uri) extension IResourceBuilder<TDestination> ReferenceEnvironmentInjectionAnnotation on the destination resource, i.e. either "services__{name}__default__0={uri}" for .NET service discovery, or "{name}={uri}" for endpoint injection. public static class ResourceBuilderExtensions{ public static IResourceBuilder<TDestination> WithReference<TDestination>( this IResourceBuilder<TDestination> builder, string name, Uri uri) { // ... }}Parameters
builder IResourceBuilder<TDestination> The resource where the service discovery information will be injected. name string The name of the service. uri Uri The uri of the service. Returns
IResourceBuilder<TDestination> The ApplicationModel.IResourceBuilder`1. WithReference(IResourceBuilder<TDestination>, IResourceBuilder<ExternalServiceResource>) Section titled WithReference(IResourceBuilder<TDestination>, IResourceBuilder<ExternalServiceResource>) extension IResourceBuilder<TDestination> ExternalServiceResource into the destination resource, using the name as the service name. The uri will be injected using the format "services__{name}__default__0={uri}." public static class ResourceBuilderExtensions{ public static IResourceBuilder<TDestination> WithReference<TDestination>( this IResourceBuilder<TDestination> builder, IResourceBuilder<ExternalServiceResource> externalService) { // ... }}Parameters
builder IResourceBuilder<TDestination> The resource where the service discovery information will be injected. externalService IResourceBuilder<ExternalServiceResource> The external service. Returns
IResourceBuilder<TDestination> The ApplicationModel.IResourceBuilder`1. WithReference(IResourceBuilder<TDestination>, EndpointReference) Section titled WithReference(IResourceBuilder<TDestination>, EndpointReference) extension IResourceBuilder<TDestination> ReferenceEnvironmentInjectionAnnotation on the destination resource, i.e. either "services__{name}__{endpointName}__{endpointIndex}={uriString}" for .NET service discovery, or "{NAME}_{ENDPOINT}={uri}" for endpoint injection. public static class ResourceBuilderExtensions{ public static IResourceBuilder<TDestination> WithReference<TDestination>( this IResourceBuilder<TDestination> builder, EndpointReference endpointReference) { // ... }}Parameters
builder IResourceBuilder<TDestination> The resource where the service discovery information will be injected. endpointReference EndpointReference The endpoint from which to extract the url. Returns
IResourceBuilder<TDestination> The ApplicationModel.IResourceBuilder`1. WithReferenceEnvironment(IResourceBuilder<TDestination>, ReferenceEnvironmentInjectionFlags) Section titled WithReferenceEnvironment(IResourceBuilder<TDestination>, ReferenceEnvironmentInjectionFlags) extension IResourceBuilder<TDestination> public static class ResourceBuilderExtensions{ public static IResourceBuilder<TDestination> WithReferenceEnvironment<TDestination>( this IResourceBuilder<TDestination> builder, ReferenceEnvironmentInjectionFlags flags) { // ... }}Parameters
builder IResourceBuilder<TDestination> The resource to configure. flags ReferenceEnvironmentInjectionFlags The injection flags determining which reference information is emitted. Returns
IResourceBuilder<TDestination> The ApplicationModel.IResourceBuilder`1. WithReferenceRelationship(IResourceBuilder<T>, IResource) Section titled WithReferenceRelationship(IResourceBuilder<T>, IResource) extension IResourceBuilder<T> ResourceRelationshipAnnotation to the resource annotations to add a reference to another resource. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithReferenceRelationship<T>( this IResourceBuilder<T> builder, IResource resource) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. resource IResource The resource that the relationship is to. Returns
IResourceBuilder<T> A resource builder. WithReferenceRelationship(IResourceBuilder<T>, ReferenceExpression) Section titled WithReferenceRelationship(IResourceBuilder<T>, ReferenceExpression) extension IResourceBuilder<T> ResourceRelationshipAnnotation s for all resources found in the expression. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithReferenceRelationship<T>( this IResourceBuilder<T> builder, ReferenceExpression expression) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. expression ReferenceExpression The reference expression. Returns
IResourceBuilder<T> A resource builder. WithReferenceRelationship(IResourceBuilder<T>, IResourceBuilder<IResource>) Section titled WithReferenceRelationship(IResourceBuilder<T>, IResourceBuilder<IResource>) extension IResourceBuilder<T> ResourceRelationshipAnnotation to the resource annotations to add a reference to another resource. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithReferenceRelationship<T>( this IResourceBuilder<T> builder, IResourceBuilder<IResource> resourceBuilder) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. resourceBuilder IResourceBuilder<IResource> The resource builder that the relationship is to. Returns
IResourceBuilder<T> A resource builder. WithRelationship(IResourceBuilder<T>, IResource, string) Section titled WithRelationship(IResourceBuilder<T>, IResource, string) extension IResourceBuilder<T> ResourceRelationshipAnnotation to the resource annotations to add a relationship. public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithRelationship<T>( this IResourceBuilder<T> builder, IResource resource, string type) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. resource IResource The resource that the relationship is to. type string The relationship type. Returns
IResourceBuilder<T> A resource builder. Remarks
The WithRelationship method is used to add relationships to the resource. Relationships are used to link resources together in UI. The type indicates information about the relationship type.
var builder = DistributedApplication.CreateBuilder(args);var backend = builder.AddProject<Projects.Backend>("backend");var manager = builder.AddProject<Projects.Manager>("manager") .WithRelationship(backend.Resource, "Manager");WithRemoteImageName(IResourceBuilder<T>, string) Section titled WithRemoteImageName(IResourceBuilder<T>, string) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithRemoteImageName<T>( this IResourceBuilder<T> builder, string remoteImageName) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. remoteImageName string The remote image name (e.g., "myapp" or "myorg/myapp"). Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Exceptions
ArgumentNullException Thrown when builder or remoteImageName is null. Remarks
ContainerImagePushOptions.RemoteImageName property. The remote image name should not include the registry endpoint or tag. Those are managed separately. This method can be combined with ResourceBuilderExtensions.WithRemoteImageTag to fully customize the image reference. Examples
Set a custom remote image name for a container:
var container = builder.AddContainer("myapp", "myapp:latest") .WithRemoteImageName("myorg/myapp");WithRemoteImageTag(IResourceBuilder<T>, string) Section titled WithRemoteImageTag(IResourceBuilder<T>, string) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithRemoteImageTag<T>( this IResourceBuilder<T> builder, string remoteImageTag) { // ... }}Parameters
builder IResourceBuilder<T> The resource builder. remoteImageTag string The remote image tag (e.g., "latest", "v1.0.0"). Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Exceptions
ArgumentNullException Thrown when builder or remoteImageTag is null. Remarks
ContainerImagePushOptions.RemoteImageTag property. The tag can be any valid container image tag such as version numbers, environment names, or deployment identifiers. This method can be combined with ResourceBuilderExtensions.WithRemoteImageName to fully customize the image reference. Examples
Set a specific version tag for a container:
var container = builder.AddContainer("myapp", "myapp:latest") .WithRemoteImageTag("v1.0.0");WithUrl(IResourceBuilder<T>, string, string?) Section titled WithUrl(IResourceBuilder<T>, string, string?) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithUrl<T>( this IResourceBuilder<T> builder, string url, string? displayText = null) { // ... }}Parameters
builder IResourceBuilder<T> The builder for the resource. url string A URL to show for the resource. displayText string? optional The display text to show when the link is displayed. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
ResourceBuilderExtensions.WithUrlForEndpoint. Examples
Add a static URL to be displayed for the resource:
var frontend = builder.AddProject<Projects.Frontend>("frontend") .WithUrl("https://example.com/", "Home");Update all displayed URLs to use the specified path and (optional) display text:
var frontend = builder.AddProject<Projects.Frontend>("frontend") .WithUrl("/home", "Home");WithUrl(IResourceBuilder<T>, ExpressionInterpolatedStringHandler, string?) Section titled WithUrl(IResourceBuilder<T>, ExpressionInterpolatedStringHandler, string?) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithUrl<T>( this IResourceBuilder<T> builder, in ExpressionInterpolatedStringHandler url, string? displayText = null) { // ... }}Parameters
builder IResourceBuilder<T> The builder for the resource. url ExpressionInterpolatedStringHandler The interpolated string that produces the URL. displayText string? optional The display text to show when the link is displayed. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
WithUrl(IResourceBuilder<T>, ReferenceExpression, string?) Section titled WithUrl(IResourceBuilder<T>, ReferenceExpression, string?) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithUrl<T>( this IResourceBuilder<T> builder, ReferenceExpression url, string? displayText = null) { // ... }}Parameters
builder IResourceBuilder<T> The builder for the resource. url ReferenceExpression A ReferenceExpression that will produce the URL. displayText string? optional The display text to show when the link is displayed. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
WithUrlForEndpoint(IResourceBuilder<T>, string, Action<ResourceUrlAnnotation>) Section titled WithUrlForEndpoint(IResourceBuilder<T>, string, Action<ResourceUrlAnnotation>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithUrlForEndpoint<T>( this IResourceBuilder<T> builder, string endpointName, Action<ResourceUrlAnnotation> callback) { // ... }}Parameters
builder IResourceBuilder<T> The builder for the resource. endpointName string The name of the endpoint to customize the URL for. callback Action<ResourceUrlAnnotation> The callback that will customize the URL. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
Use this method to customize the URL that is automatically added for an endpoint on the resource. To add another URL for an endpoint, use ResourceBuilderExtensions.WithUrlForEndpoint.
The callback will be executed after endpoints have been allocated and the URL has been generated. This allows you to modify the URL or its display text.
If the URL returned by callback is relative, it will be combined with the endpoint URL to create an absolute URL.
If the endpoint with the specified name does not exist, the callback will not be executed and a warning will be logged.
Customize the URL for the "https" endpoint to use the link text "Home":var frontend = builder.AddProject<Projects.Frontend>("frontend") .WithUrlForEndpoint( "https", url => url.DisplayText = "Home");var frontend = builder.AddProject<Projects.Frontend>("frontend") .WithUrlForEndpoint("https", url => url.Url = "/home");WithUrlForEndpoint(IResourceBuilder<T>, string, Func<EndpointReference, ResourceUrlAnnotation>) Section titled WithUrlForEndpoint(IResourceBuilder<T>, string, Func<EndpointReference, ResourceUrlAnnotation>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithUrlForEndpoint<T>( this IResourceBuilder<T> builder, string endpointName, Func<EndpointReference, ResourceUrlAnnotation> callback) { // ... }}Parameters
builder IResourceBuilder<T> The builder for the resource. endpointName string The name of the endpoint to add the URL for. callback Func<EndpointReference, ResourceUrlAnnotation> The callback that will create the URL. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
Use this method to add another URL for an endpoint on the resource. To customize the URL that is automatically added for an endpoint, use ResourceBuilderExtensions.WithUrlForEndpoint.
The callback will be executed after endpoints have been allocated and the resource is about to start.
If the endpoint with the specified name does not exist, the callback will not be executed and a warning will be logged.
Add a URL for the "https" endpoint that deep-links to an admin page with the text "Admin":var frontend = builder.AddProject<Projects.Frontend>("frontend") .WithUrlForEndpoint( "https", ep => new() { Url = "/admin", DisplayText = "Admin" });WithUrls(IResourceBuilder<T>, Action<ResourceUrlsCallbackContext>) Section titled WithUrls(IResourceBuilder<T>, Action<ResourceUrlsCallbackContext>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithUrls<T>( this IResourceBuilder<T> builder, Action<ResourceUrlsCallbackContext> callback) { // ... }}Parameters
builder IResourceBuilder<T> The builder for the resource. callback Action<ResourceUrlsCallbackContext> The callback that will customize URLs for the resource. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
The callback will be executed after endpoints have been allocated for this resource. This allows you to modify any URLs for the resource, including adding, modifying, or even deletion. Note that any endpoints on the resource will automatically get a corresponding URL added for them.
Update all displayed URLs to have display text:var frontend = builder.AddProject<Projects.Frontend>("frontend") .WithUrls(c => { foreach (var url in c.Urls) { if (string.IsNullOrEmpty(url.DisplayText)) { url.DisplayText = "frontend"; } } });var frontend = builder.AddProject<Projects.Frontend>("frontend") .WithUrls(c => { foreach (var url in c.Urls) { if (url.Endpoint is not null) { var uri = new UriBuilder( url.Url) { Host = $"{c.Resource.Name}.localhost" }; url.Url = uri.ToString(); } } });WithUrls(IResourceBuilder<T>, Func<ResourceUrlsCallbackContext, Task>) Section titled WithUrls(IResourceBuilder<T>, Func<ResourceUrlsCallbackContext, Task>) extension IResourceBuilder<T> public static class ResourceBuilderExtensions{ public static IResourceBuilder<T> WithUrls<T>( this IResourceBuilder<T> builder, Func<ResourceUrlsCallbackContext, Task> callback) { // ... }}Parameters
builder IResourceBuilder<T> The builder for the resource. callback Func<ResourceUrlsCallbackContext, Task> The async callback that will customize URLs for the resource. Returns
IResourceBuilder<T> The ApplicationModel.IResourceBuilder`1. Remarks
The callback will be executed after endpoints have been allocated for this resource. This allows you to modify any URLs for the resource, including adding, modifying, or even deletion. Note that any endpoints on the resource will automatically get a corresponding URL added for them.