Skip to content

Fix: Skip resources targeted to different compute environments#14177

Merged
eerhardt merged 5 commits intomainfrom
copilot/fix-aca-resource-restriction
Jan 28, 2026
Merged

Fix: Skip resources targeted to different compute environments#14177
eerhardt merged 5 commits intomainfrom
copilot/fix-aca-resource-restriction

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 28, 2026

Description

When using multiple compute environments (e.g., ACA + AppService), all environments processed all resources regardless of WithComputeEnvironment() targeting. This caused ACA to reject resources intended for AppService due to port validation mismatches, and similar cross-environment validation failures.

Example scenario that now works:

var aca = builder.AddAzureContainerAppEnvironment("aca");
var appService = builder.AddAzureAppServiceEnvironment("appservice");

builder.AddProject<Projects.WebApp>("webapp")
    .WithComputeEnvironment(aca);

// Container with custom port targeted to AppService
// Previously: ACA would error requiring port 80
// Now: ACA skips it entirely
builder.AddContainer("redis", "redis")
    .WithHttpEndpoint(port: 8123, targetPort: 6379)
    .WithComputeEnvironment(appService);

Changes:

  • Added filtering in AzureContainerAppsInfrastructure, AzureAppServiceInfrastructure, DockerComposeInfrastructure, and KubernetesInfrastructure to skip resources with ComputeEnvironmentAnnotation targeting a different environment
  • Granted internal visibility of ComputeEnvironmentAnnotation to infrastructure projects via InternalsVisibleTo
  • Removed conflicting shared file includes (ResourceNameComparer, DashboardConfigNames, etc.) from infrastructure projects now using internal types directly
  • Added test validating multi-environment resource targeting

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
    • No
  • Does the change require an update in our Aspire docs?
    • Yes
    • No
Original prompt

This section details on the original issue you should resolve

<issue_title>ACA restricts other resources when using multiple compute environments</issue_title>
<issue_description>I have the following apphost:

var builder = DistributedApplication.CreateBuilder(args);

var aca = builder.AddAzureContainerAppEnvironment("aca");
var appService = builder.AddAzureAppServiceEnvironment("appservice");

builder.AddProject<Projects.WebAppACA>("webappaca")
    .WithExternalHttpEndpoints()
    .WithComputeEnvironment(aca);

builder.AddRedis("redis")
    .WithHttpEndpoint(port: 8123, targetPort: 8123, name: "http")
    .WithExternalHttpEndpoints()
    .WithComputeEnvironment(appService);

builder.Build().Run();

When I run aspire deploy I get the following error:

❌ An error occurred while connecting to the app host. The app host possibly crashed before it was available: AppHost process has exited        
unexpectedly. Use --debug to see more details..
Unhandled exception. System.AggregateException: One or more errors occurred. (The endpoint 'http' is an http endpoint and must use port 80)     
 ---> System.NotSupportedException: The endpoint 'http' is an http endpoint and must use port 80
   at Aspire.Hosting.Azure.ContainerAppContext.ProcessEndpoints() in /_/src/Aspire.Hosting.Azure.AppContainers/ContainerAppContext.cs:line 232
   at Aspire.Hosting.Azure.BaseContainerAppContext.ProcessResourceAsync(CancellationToken cancellationToken) in 
/_/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs:line 104
   at Aspire.Hosting.Azure.ContainerAppEnvironmentContext.CreateContainerAppAsync(IResource resource, AzureProvisioningOptions 
provisioningOptions, CancellationToken cancellationToken) in /_/src/Aspire.Hosting.Azure.AppContainers/ContainerAppEnvironmentContext.cs:line 43
   at Aspire.Hosting.Azure.AzureContainerAppsInfrastructure.OnBeforeStartAsync(BeforeStartEvent event, CancellationToken cancellationToken) in 
/_/src/Aspire.Hosting.Azure.AppContainers/AzureContainerAppsInfrastructure.cs:line 48
   at Aspire.Hosting.Eventing.DistributedApplicationEventing.<>c__DisplayClass4_0`1.<<Subscribe>b__0>d.MoveNext() in 
/_/src/Aspire.Hosting/Eventing/DistributedApplicationEventing.cs:line 82
--- End of stack trace from previous location ---
   at Aspire.Hosting.Eventing.DistributedApplicationEventing.PublishAsync[[T]](T event, EventDispatchBehavior dispatchBehavior, 
CancellationToken cancellationToken) in /_/src/Aspire.Hosting/Eventing/DistributedApplicationEventing.cs:line 69
   at Aspire.Hosting.DistributedApplication.ExecuteBeforeStartHooksAsync(CancellationToken cancellationToken) in 
/_/src/Aspire.Hosting/DistributedApplication.cs:line 538
   at Aspire.Hosting.DistributedApplication.RunAsync(CancellationToken cancellationToken) in 
/_/src/Aspire.Hosting/DistributedApplication.cs:line 488
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at Aspire.Hosting.DistributedApplication.Run() in /_/src/Aspire.Hosting/DistributedApplication.cs:line 519
   at Program.<Main>$(String[[]] args) in D:\DotNetTest\MultiComputeEnvTest\MultiComputeEnvTest\MultiComputeEnvTest.AppHost\AppHost.cs:line 15

ACA is putting restrictions on redis - but it isn't being deployed to ACA.

To fix this, we should change the following code

https://github.com/dotnet/aspire/blob/9e35d404c4590078753a26592f36dca396a7cbe1/src/Aspire.Hosting.Azure.AppContainers/AzureContainerAppsInfrastructure.cs#L46-L48

We should check if this resource's compute environment is targeting the ACA environment. If it isn't, it should be skipped and we don't add a DeploymentTargetAnnotation.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits January 28, 2026 20:24
Co-authored-by: eerhardt <8291187+eerhardt@users.noreply.github.com>
Co-authored-by: eerhardt <8291187+eerhardt@users.noreply.github.com>
Co-authored-by: eerhardt <8291187+eerhardt@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ACA restricting other resources in multiple compute environments Fix: Skip resources targeted to different compute environments Jan 28, 2026
Copilot AI requested a review from eerhardt January 28, 2026 20:39
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 28, 2026

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 14177

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 14177"

@eerhardt eerhardt marked this pull request as ready for review January 28, 2026 21:08
@eerhardt eerhardt requested review from Copilot and davidfowl January 28, 2026 21:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an issue where multiple compute environments (e.g., Azure Container Apps + AppService) would incorrectly process all resources regardless of WithComputeEnvironment() targeting. Previously, ACA would validate resources intended for AppService, causing errors due to port validation mismatches. The fix adds filtering logic to skip resources explicitly targeted to different compute environments.

Changes:

  • Added a public GetComputeEnvironment() helper method in ResourceExtensions.cs to retrieve the compute environment a resource is bound to
  • Added filtering logic in all four infrastructure implementations (ACA, AppService, DockerCompose, Kubernetes) to skip resources with ComputeEnvironmentAnnotation targeting different environments
  • Added comprehensive test coverage across three test projects validating the multi-environment resource targeting behavior

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs Added public GetComputeEnvironment() extension method to retrieve the compute environment bound to a resource
src/Aspire.Hosting.Azure.AppContainers/AzureContainerAppsInfrastructure.cs Added filtering to skip resources targeted to different compute environments before processing
src/Aspire.Hosting.Azure.AppService/AzureAppServiceInfrastructure.cs Added filtering to skip resources targeted to different compute environments before processing
src/Aspire.Hosting.Docker/DockerComposeInfrastructure.cs Added filtering to skip resources targeted to different compute environments before processing
src/Aspire.Hosting.Kubernetes/KubernetesInfrastructure.cs Added filtering to skip resources targeted to different compute environments before processing
tests/Aspire.Hosting.Azure.Tests/AzureContainerAppsTests.cs Added test validating ACA and AppService multi-environment targeting with port validation scenario
tests/Aspire.Hosting.Docker.Tests/DockerComposeTests.cs Added test validating DockerCompose and Kubernetes multi-environment targeting
tests/Aspire.Hosting.Docker.Tests/Aspire.Hosting.Docker.Tests.csproj Added project reference to Kubernetes for test scenarios
tests/Aspire.Hosting.Kubernetes.Tests/KubernetesEnvironmentResourceTests.cs Added test validating Kubernetes and DockerCompose multi-environment targeting
tests/Aspire.Hosting.Kubernetes.Tests/Aspire.Hosting.Kubernetes.Tests.csproj Added project references to Docker and TestUtilities for test scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs
@davidfowl
Copy link
Copy Markdown
Contributor

PR Testing Report

CLI Version Verification

  • Expected Commit: fc72aac
  • Installed Version: 13.2.0-pr.14177.gfc72aacd
  • Status: ✅ Verified

Test Results

Scenario Status Notes
Basic App Run ✅ Passed �spire new + �spire run worked correctly
Multi-Compute Env Publish ✅ Passed Resources correctly routed to targeted environments

Multi-Compute Environment Publish Test

Created an AppHost with two compute environments (ACA + App Service) and resources targeted to each:

var aca = builder.AddAzureContainerAppEnvironment("aca");
var appService = builder.AddAzureAppServiceEnvironment("appservice");

var apiService = builder.AddProject<Projects.MultiEnv_ApiService>("apiservice")
    .WithComputeEnvironment(aca);

builder.AddProject<Projects.MultiEnv_Web>("webfrontend")
    .WithComputeEnvironment(appService);

Result: �spire publish succeeded with all 6 steps passing:

  • �piservice → Bicep generated for ACA
  • webfrontend → Bicep generated for App Service
  • Each compute environment correctly skipped resources not targeted to it ✅

Note on Cross-Environment References

When testing with .WithReference(apiservice) on a resource in a different compute environment, the publish failed with:

App Service context not found for resource apiservice

This appears to be expected behavior / a separate issue - cross-environment resource references aren't supported. The PR's fix for filtering resources by compute environment is working correctly.


Tested with CLI from PR artifacts on Windows

@davidfowl
Copy link
Copy Markdown
Contributor

Updated PR Testing Report

CLI Version Verification

  • Expected Commit: fc72aac
  • Installed Version: 13.2.0-pr.14177.gfc72aacd
  • Status: ✅ Verified

Exact Scenario from PR Description

Tested the exact scenario that was failing before this fix:

var aca = builder.AddAzureContainerAppEnvironment("aca");
var appService = builder.AddAzureAppServiceEnvironment("appservice");

builder.AddProject<Projects.ExactTest_Web>("webappaca")
    .WithExternalHttpEndpoints()
    .WithComputeEnvironment(aca);

// Container with custom port targeted to AppService
// Previously: ACA would error "endpoint 'http' is an http endpoint and must use port 80"
// Now: ACA should skip it entirely
builder.AddRedis("redis")
    .WithHttpEndpoint(port: 8123, targetPort: 8123, name: "http")
    .WithExternalHttpEndpoints()
    .WithComputeEnvironment(appService);

Result: ✅ PASSED

✓ 6/6 steps succeeded • Total time: 0.18s

Steps Summary:
       0.16s  ✓ pipeline-execution
     28.38ms  ✓ publish-azure93ffa
     17.17ms  ✓ process-parameters
      1.38ms  ✓ validate-appservice-config-appservice
      0.71ms  ✓ publish-prereq
      0.36ms  ✓ publish

✓ PIPELINE SUCCEEDED

Key finding: The Redis container with custom port 8123 (targeted to AppService) was correctly skipped by ACA infrastructure - no port 80 validation error occurred. The fix is working as intended.


Tested with CLI from PR artifacts on Windows

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 28, 2026

🎬 CLI E2E Test Recordings

The following terminal recordings are available for commit fc72aac:

Test Recording
CreateAndDeployToDockerCompose ▶️ View Recording
CreateAndDeployToDockerComposeInteractive ▶️ View Recording
CreateAndRunAspireStarterProject ▶️ View Recording
CreateAndRunJsReactProject ▶️ View Recording
CreateAndRunPythonReactProject ▶️ View Recording
CreateEmptyAppHostProject ▶️ View Recording
CreateStartAndStopAspireProject ▶️ View Recording
CreateTypeScriptAppHostWithViteApp ▶️ View Recording
DoctorCommand_WithSslCertDir_ShowsTrusted ▶️ View Recording
DoctorCommand_WithoutSslCertDir_ShowsPartiallyTrusted ▶️ View Recording
PsCommandListsRunningAppHost ▶️ View Recording

📹 Recordings uploaded automatically from CI run #21455482372

@eerhardt eerhardt merged commit 7bdf487 into main Jan 28, 2026
661 of 665 checks passed
@eerhardt eerhardt deleted the copilot/fix-aca-resource-restriction branch January 28, 2026 22:29
@dotnet-policy-service dotnet-policy-service Bot added this to the 13.2 milestone Jan 28, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 28, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ACA restricts other resources when using multiple compute environments

4 participants