ResourceExtensions Methods
IResource interface. GetArgumentValuesAsync(IResourceWithArgs, DistributedApplicationOperation) Section titled GetArgumentValuesAsync(IResourceWithArgs, DistributedApplicationOperation)extensionValueTask<string[]> public static class ResourceExtensions{ public static ValueTask<string[]> GetArgumentValuesAsync( this IResourceWithArgs resource, DistributedApplicationOperation applicationOperation = DistributedApplicationOperation.Run) { // ... }}Parameters
resourceIapplicationOperation DistributedReturns
ValueTask<string[]> The arguments retrieved from the resource.Remarks
applicationOperation is DistributedApplicationOperation.Run and arguments were provided from IValueProvider otherwise it will be synchronous. Using ResourceExtensions.GetArgumentValuesAsync inside a unit test to validate argument values. var builder = DistributedApplication.CreateBuilder();var container = builder.AddContainer( "elasticsearch", "library/elasticsearch", "8.14.0") .WithArgs("--discovery.type", "single-node") .WithArgs("--xpack.security.enabled", "true");
var args = await container.Resource.GetArgumentsAsync();
Assert.Collection(args, arg => { Assert.Equal("--discovery.type", arg); }, arg => { Assert.Equal("--xpack.security.enabled", arg); });GetComputeEnvironment(IResource) Section titled GetComputeEnvironment(IResource)extensionnullableIpublic static class ResourceExtensions{ public static IComputeEnvironmentResource? GetComputeEnvironment( this IResource resource) { // ... }}Parameters
resourceIReturns
IComputeEnvironmentResource? The compute environment the resource is bound to, or null if the resource is not bound to any specific compute environment.GetDeploymentTargetAnnotation(IResource, IComputeEnvironmentResource?) Section titled GetDeploymentTargetAnnotation(IResource, IComputeEnvironmentResource?)extensionnullableDeploymentpublic static class ResourceExtensions{ public static DeploymentTargetAnnotation? GetDeploymentTargetAnnotation( this IResource resource, IComputeEnvironmentResource? targetComputeEnvironment = null) { // ... }}Parameters
resourceItargetComputeEnvironment IGetEndpoint(IResourceWithEndpoints, string) Section titled GetEndpoint(IResourceWithEndpoints, string)extensionEndpointpublic static class ResourceExtensions{ public static EndpointReference GetEndpoint( this IResourceWithEndpoints resource, string endpointName) { // ... }}Parameters
resourceIIResourceWithEndpoints which contains EndpointAnnotation annotations.endpointName stringThe name of the endpoint.Returns
EndpointReference An EndpointReference object providing resolvable reference for the specified endpoint.GetEndpoint(IResourceWithEndpoints, string, NetworkIdentifier) Section titled GetEndpoint(IResourceWithEndpoints, string, NetworkIdentifier)extensionEndpointpublic static class ResourceExtensions{ public static EndpointReference GetEndpoint( this IResourceWithEndpoints resource, string endpointName, NetworkIdentifier contextNetworkId) { // ... }}Parameters
resourceIIResourceWithEndpoints which contains EndpointAnnotation annotations.endpointName stringThe name of the endpoint.contextNetworkId NetworkEndpointReference Returns
EndpointReference An EndpointReference object providing resolvable reference for the specified endpoint.GetEndpoints(IResourceWithEndpoints) Section titled GetEndpoints(IResourceWithEndpoints)extensionIpublic static class ResourceExtensions{ public static IEnumerable<EndpointReference> GetEndpoints( this IResourceWithEndpoints resource) { // ... }}Parameters
resourceIIResourceWithEndpoints which contains EndpointAnnotation annotations.Returns
IEnumerable<EndpointReference> An enumeration of EndpointReference based on the EndpointAnnotation annotations from the resources' IResource.Annotations collection.GetEndpoints(IResourceWithEndpoints, NetworkIdentifier) Section titled GetEndpoints(IResourceWithEndpoints, NetworkIdentifier)extensionIpublic static class ResourceExtensions{ public static IEnumerable<EndpointReference> GetEndpoints( this IResourceWithEndpoints resource, NetworkIdentifier contextNetworkId) { // ... }}Parameters
resourceIIResourceWithEndpoints which contains EndpointAnnotation annotations.contextNetworkId NetworkReturns
IEnumerable<EndpointReference> An enumeration of EndpointReference based on the EndpointAnnotation annotations from the resources' IResource.Annotations collection.GetEnvironmentVariableValuesAsync(IResourceWithEnvironment, DistributedApplicationOperation) Section titled GetEnvironmentVariableValuesAsync(IResourceWithEnvironment, DistributedApplicationOperation)extensionValueTask<Dictionary<string, string>> public static class ResourceExtensions{ public static ValueTask<Dictionary<string, string>> GetEnvironmentVariableValuesAsync( this IResourceWithEnvironment resource, DistributedApplicationOperation applicationOperation = DistributedApplicationOperation.Run) { // ... }}Parameters
resourceIapplicationOperation DistributedReturns
ValueTask<Dictionary<string, string>> The environment variables retrieved from the resource.Remarks
applicationOperation is DistributedApplicationOperation.Run and environment variables were provided from IValueProvider otherwise it will be synchronous. Using ResourceExtensions.GetEnvironmentVariableValuesAsync inside a unit test to validate environment variable values. var builder = DistributedApplication.CreateBuilder();var container = builder.AddContainer( "elasticsearch", "library/elasticsearch", "8.14.0") .WithEnvironment("discovery.type", "single-node") .WithEnvironment("xpack.security.enabled", "true");
var env = await container.Resource.GetEnvironmentVariableValuesAsync();
Assert.Collection(env, env => { Assert.Equal("discovery.type", env.Key); Assert.Equal("single-node", env.Value); }, env => { Assert.Equal("xpack.security.enabled", env.Key); Assert.Equal("true", env.Value); });1 if no ReplicaAnnotation is found. public static class ResourceExtensions{ public static int GetReplicaCount( this IResource resource) { // ... }}Parameters
resourceIReturns
intThe number of replicas for the specified resource.GetResourceDependenciesAsync(IResource, DistributedApplicationExecutionContext, ResourceDependencyDiscoveryMode, CancellationToken) Section titled GetResourceDependenciesAsync(IResource, DistributedApplicationExecutionContext, ResourceDependencyDiscoveryMode, CancellationToken)extensionTaskresource depends on. public static class ResourceExtensions{ public static Task<IReadOnlySet<IResource>> GetResourceDependenciesAsync( this IResource resource, DistributedApplicationExecutionContext executionContext, ResourceDependencyDiscoveryMode mode = ResourceDependencyDiscoveryMode.Recursive, CancellationToken cancellationToken = default(CancellationToken)) { // ... }}Parameters
resourceIexecutionContext DistributedmodeResourcecancellationToken CancellationToken optionalA cancellation token to observe while computing dependencies.Returns
Task<IReadOnlySet<IResource>> A set of all resources that the specified resource depends on.Remarks
Dependencies are computed from multiple sources: IWaitConnectionI)
When mode is Resource, only the immediate dependencies are returned. When mode is Resource, all transitive dependencies are included.
This method invokes environment variable and command-line argument callbacks to discover all references. The context resource ( resource) is not considered a dependency (even if it is transitively referenced).
GetResourceDependenciesAsync(IResource, DistributedApplicationExecutionContext, ResourceDependencyDiscoveryOptions, CancellationToken) Section titled GetResourceDependenciesAsync(IResource, DistributedApplicationExecutionContext, ResourceDependencyDiscoveryOptions, CancellationToken)extensionTaskresource depends on. public static class ResourceExtensions{ public static Task<IReadOnlySet<IResource>> GetResourceDependenciesAsync( this IResource resource, DistributedApplicationExecutionContext executionContext, ResourceDependencyDiscoveryOptions options, CancellationToken cancellationToken = default(CancellationToken)) { // ... }}Parameters
resourceIexecutionContext DistributedoptionsResourceResourceDependencyDiscoveryOptions enumeration for more information.cancellationToken CancellationToken optionalA cancellation token to observe while computing dependencies.Returns
Task<IReadOnlySet<IResource>> A set of all resources that the specified resource depends on.Remarks
Dependencies are computed from multiple sources: IWaitConnectionI)
This method invokes environment variable and command-line argument callbacks to discover all references. The context resource ( resource) is not considered a dependency (even if it is transitively referenced).
HasAnnotationIncludingAncestorsOfType(IResource) Section titled HasAnnotationIncludingAncestorsOfType(IResource)extensionboolresource or its ancestors have an annotation of type Tpublic static class ResourceExtensions{ public static bool HasAnnotationIncludingAncestorsOfType<T>( this IResource resource) { // ... }}Parameters
resourceIReturns
booltrue if an annotation of the specified type was found; otherwise, false.resource has an annotation of type Tpublic static class ResourceExtensions{ public static bool HasAnnotationOfType<T>( this IResource resource) { // ... }}Parameters
resourceIReturns
booltrue if an annotation of the specified type was found; otherwise, false.public static class ResourceExtensions{ public static bool IsExcludedFromPublish( this IResource resource) { // ... }}Parameters
resourceIProcessArgumentValuesAsync(IResource, DistributedApplicationExecutionContext, Action<object?, string?, Exception?, bool>, ILogger, CancellationToken) Section titled ProcessArgumentValuesAsync(IResource, DistributedApplicationExecutionContext, Action<object?, string?, Exception?, bool>, ILogger, CancellationToken)extensionValueTask public static class ResourceExtensions{ public static ValueTask ProcessArgumentValuesAsync( this IResource resource, DistributedApplicationExecutionContext executionContext, Action<object?, string?, Exception?, bool> processValue, ILogger logger, CancellationToken cancellationToken = default(CancellationToken)) { // ... }}Parameters
resourceIexecutionContext DistributedprocessValue Action<object?, string?, Exception?, bool> A callback invoked for each argument value. This action provides the unprocessed value, processed string representation, an exception if one occurs, and a boolean indicating the success of processing. loggerILogger The logger used for logging information or errors during the argument processing.cancellationToken CancellationToken optionalA token for cancelling the operation, if needed.Returns
ValueTask A task representing the asynchronous operation.ProcessEnvironmentVariableValuesAsync(IResource, DistributedApplicationExecutionContext, Action<string, object?, string?, Exception?>, ILogger, CancellationToken) Section titled ProcessEnvironmentVariableValuesAsync(IResource, DistributedApplicationExecutionContext, Action<string, object?, string?, Exception?>, ILogger, CancellationToken)extensionValueTask public static class ResourceExtensions{ public static ValueTask ProcessEnvironmentVariableValuesAsync( this IResource resource, DistributedApplicationExecutionContext executionContext, Action<string, object?, string?, Exception?> processValue, ILogger logger, CancellationToken cancellationToken = default(CancellationToken)) { // ... }}Parameters
resourceIexecutionContext DistributedprocessValue Action<string, object?, string?, Exception?> An action delegate invoked for each environment variable, providing the key, the unprocessed value, the processed value (if available), and any exception encountered during processing.loggerILogger The logger used to log any information or errors during the environment variables processing.cancellationToken CancellationToken optionalA cancellation token to observe during the asynchronous operation.Returns
ValueTask A task that represents the asynchronous operation.public static class ResourceExtensions{ public static bool RequiresImageBuild( this IResource resource) { // ... }}Parameters
resourceIReturns
boolTrue if the resource requires image building; otherwise, false.Remarks
RequiresImageBuildAndPush(IResource) Section titled RequiresImageBuildAndPush(IResource)extensionboolpublic static class ResourceExtensions{ public static bool RequiresImageBuildAndPush( this IResource resource) { // ... }}Parameters
resourceIReturns
boolTrue if the resource requires image building and pushing; otherwise, false.Remarks
ResolveEndpoints(IResource, IPortAllocator?) Section titled ResolveEndpoints(IResource, IPortAllocator?)extensionIpublic static class ResourceExtensions{ public static IReadOnlyList<ResolvedEndpoint> ResolveEndpoints( this IResource resource, IPortAllocator? portAllocator = null) { // ... }}Parameters
resourceIportAllocator IReturns
IReadOnlyList<ResolvedEndpoint> A read-only list of resolved endpoints with computed port values.TryGetAnnotationsIncludingAncestorsOfType(IResource, IEnumerable<T>) Section titled TryGetAnnotationsIncludingAncestorsOfType(IResource, IEnumerable<T>)extensionboolpublic static class ResourceExtensions{ public static bool TryGetAnnotationsIncludingAncestorsOfType<T>( this IResource resource, out IEnumerable<T>? result) { // ... }}Parameters
resourceIresultIEnumerable<T> When this method returns, contains the annotations of the specified type, if found; otherwise, null.Returns
booltrue if annotations of the specified type were found; otherwise, false.TryGetAnnotationsOfType(IResource, IEnumerable<T>) Section titled TryGetAnnotationsOfType(IResource, IEnumerable<T>)extensionboolpublic static class ResourceExtensions{ public static bool TryGetAnnotationsOfType<T>( this IResource resource, out IEnumerable<T>? result) { // ... }}Parameters
resourceIresultIEnumerable<T> When this method returns, contains the annotations of the specified type, if found; otherwise, null.Returns
booltrue if annotations of the specified type were found; otherwise, false.TryGetContainerImageName(IResource, string?) Section titled TryGetContainerImageName(IResource, string?)extensionboolpublic static class ResourceExtensions{ public static bool TryGetContainerImageName( this IResource resource, out string? imageName) { // ... }}Parameters
resourceIimageName string?The container image name if found, otherwise null.Returns
boolTrue if the container image name was found, otherwise false.TryGetContainerImageName(IResource, bool, string?) Section titled TryGetContainerImageName(IResource, bool, string?)extensionboolpublic static class ResourceExtensions{ public static bool TryGetContainerImageName( this IResource resource, bool useBuiltImage, out string? imageName) { // ... }}Parameters
resourceIuseBuiltImage boolWhen true, uses the image name from DockerfileBuildAnnotation if present. When false, uses only ContainerImageAnnotation.imageName string?The container image name if found, otherwise null.Returns
boolTrue if the container image name was found, otherwise false.TryGetContainerMounts(IResource, IEnumerable<ContainerMountAnnotation>) Section titled TryGetContainerMounts(IResource, IEnumerable<ContainerMountAnnotation>)extensionboolpublic static class ResourceExtensions{ public static bool TryGetContainerMounts( this IResource resource, out IEnumerable<ContainerMountAnnotation>? volumeMounts) { // ... }}Parameters
resourceIvolumeMounts Inull.Returns
booltrue if the volume mounts were successfully retrieved; otherwise, false.TryGetEndpoints(IResource, IEnumerable<EndpointAnnotation>) Section titled TryGetEndpoints(IResource, IEnumerable<EndpointAnnotation>)extensionboolpublic static class ResourceExtensions{ public static bool TryGetEndpoints( this IResource resource, out IEnumerable<EndpointAnnotation>? endpoints) { // ... }}Parameters
resourceIendpointsIReturns
boolTrue if the endpoints were found, false otherwise.TryGetEnvironmentVariables(IResource, IEnumerable<EnvironmentCallbackAnnotation>) Section titled TryGetEnvironmentVariables(IResource, IEnumerable<EnvironmentCallbackAnnotation>)extensionboolpublic static class ResourceExtensions{ public static bool TryGetEnvironmentVariables( this IResource resource, out IEnumerable<EnvironmentCallbackAnnotation>? environmentVariables) { // ... }}Parameters
resourceIenvironmentVariables IReturns
boolTrue if the environment variables were successfully retrieved, false otherwise.public static class ResourceExtensions{ public static bool TryGetLastAnnotation<T>( this IResource resource, out T? annotation) { // ... }}Parameters
resourceIannotationT?When this method returns, contains the last annotation of the specified type from the resource, if found; otherwise, the default value for T.Returns
booltrue if the last annotation of the specified type was found in the resource; otherwise, false.TryGetUrls(IResource, IEnumerable<ResourceUrlAnnotation>) Section titled TryGetUrls(IResource, IEnumerable<ResourceUrlAnnotation>)extensionboolpublic static class ResourceExtensions{ public static bool TryGetUrls( this IResource resource, out IEnumerable<ResourceUrlAnnotation>? urls) { // ... }}Parameters
resourceIurlsIReturns
boolTrue if the URLs were found, false otherwise.WithContainerBuildOptions(IResourceBuilder<T>, Action<ContainerBuildOptionsCallbackContext>) Section titled WithContainerBuildOptions(IResourceBuilder<T>, Action<ContainerBuildOptionsCallbackContext>)extensionIpublic static class ResourceExtensions{ public static IResourceBuilder<T> WithContainerBuildOptions<T>( this IResourceBuilder<T> builder, Action<ContainerBuildOptionsCallbackContext> callback) { // ... }}Parameters
builderIcallbackAction<ContainerBuildOptionsCallbackContext> A callback to configure container build options.Returns
IResourceBuilder<T> A reference to the ApplicationModel.IResourceBuilder`1 .WithContainerBuildOptions(IResourceBuilder<T>, Func<ContainerBuildOptionsCallbackContext, Task>) Section titled WithContainerBuildOptions(IResourceBuilder<T>, Func<ContainerBuildOptionsCallbackContext, Task>)extensionIpublic static class ResourceExtensions{ public static IResourceBuilder<T> WithContainerBuildOptions<T>( this IResourceBuilder<T> builder, Func<ContainerBuildOptionsCallbackContext, Task> callback) { // ... }}Parameters
builderIcallbackFunc<ContainerBuildOptionsCallbackContext, Task> An async callback to configure container build options.Returns
IResourceBuilder<T> A reference to the ApplicationModel.IResourceBuilder`1 .