DistributedApplicationBuilderExtensions Methods
IDistributedApplicationBuilder. CreateResourceBuilder(IDistributedApplicationBuilder, string) Section titled CreateResourceBuilder(IDistributedApplicationBuilder, string) extension IResourceBuilder<T> public static class DistributedApplicationBuilderExtensions{ public static IResourceBuilder<T> CreateResourceBuilder<T>( this IDistributedApplicationBuilder builder, string name) { // ... }}Parameters
builder IDistributedApplicationBuilder The distributed application builder. name string The name of an existing resource. Returns
IResourceBuilder<T> A resource builder. Remarks
This method is not available in polyglot app hosts.
The DistributedApplicationBuilderExtensions.CreateResourceBuilder method is used to create an ApplicationModel.IResourceBuilder`1 for a specific resource where the original resource builder cannot be referenced. This does not create a new resource, but instead returns a resource builder for an existing resource based on its name.
This method is typically used when testing Aspire applications where the original resource builder cannot be referenced directly. Using the DistributedApplicationBuilderExtensions.CreateResourceBuilder method allows for easier mutation of resources within the test scenario.
[Fact]public async Task GetWebResourceHealthReturnsUnhealthyWhenRedisUnavailable(){ // Arrange var appHost = await DistributedApplicationTestingBuilder .CreateAsync<Projects.MyAspireApp_AppHost>( );
// Get the "cache" resource and modify it to sleep for 1 day instead of starting Redis. var redis = appHost.CreateResourceBuilder<ContainerResource>("cache")); redis.WithEntrypoint("sleep 1d");
await using var app = await appHost.BuildAsync(); await app.StartAsync();
// Act var httpClient = new HttpClient { BaseAddress = app.GetEndpoint( "webfrontend") }; var response = await httpClient.GetAsync("/health");
// Assert Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode); Assert.Equal("Unhealthy", await response.Content.ReadAsStringAsync());}TryCreateResourceBuilder(IDistributedApplicationBuilder, string, IResourceBuilder<T>) Section titled TryCreateResourceBuilder(IDistributedApplicationBuilder, string, IResourceBuilder<T>) extension bool public static class DistributedApplicationBuilderExtensions{ public static bool TryCreateResourceBuilder<T>( this IDistributedApplicationBuilder builder, string name, out IResourceBuilder<T>? resourceBuilder) { // ... }}Parameters
builder IDistributedApplicationBuilder The distributed application builder. name string The name of an existing resource. resourceBuilder IResourceBuilder<T> When this method returns, contains the resource builder if the resource was found and is of the correct type; otherwise, null. Returns
bool true if the resource was found and is of the correct type; otherwise, false. Remarks
This method is not available in polyglot app hosts.
This method is similar toDistributedApplicationBuilderExtensions.CreateResourceBuilder but returns false instead of throwing an exception when the resource is not found or is not of the correct type.