- 
                Notifications
    You must be signed in to change notification settings 
- Fork 715
Description
Description
When using AddAzureContainerAppEnvironment together with AddPythonApp in Aspire 9.5.0, running azd infra gen (manifest generation) fails with an IServiceProvider is not available error. This is a regression in 9.5.0 - the same code works correctly in 9.4.2.
Environment
- .NET Aspire version: 9.5.0
- .NET SDK version: 9.0
- OS: macOS (also reproduces on Windows/Linux)
- Tool: Azure Developer CLI (azd)
Steps to Reproduce
- 
Create an AppHost project with Aspire 9.5.0 
- 
Add the following packages: <PackageReference Include="Aspire.Hosting.AppHost" Version="9.5.0" /> <PackageReference Include="Aspire.Hosting.Python" Version="9.5.0" /> <PackageReference Include="Aspire.Hosting.Azure.AppContainers" Version="9.5.0" /> 
- 
Configure AppHost.cs with both Azure Container App Environment and Python app: var builder = DistributedApplication.CreateBuilder(args); var containerAppEnvironment = builder.AddAzureContainerAppEnvironment("containerAppEnvironment"); var pythonapp = builder.AddPythonApp("instrumented-python-app", "../InstrumentedPythonProject", "app.py") .WithHttpEndpoint(env: "PORT") .WithExternalHttpEndpoints(); builder.Build().Run(); 
- 
Run azd infra gen
Expected Behavior
Manifest generation should succeed and produce infrastructure files.
Actual Behavior
The command fails with the following error:
ERROR: generating apphost manifest: generating app host manifest: dotnet run --publisher manifest on project
'AspireWithPython.AppHost.csproj' failed: exit code: 134
Unhandled exception. System.AggregateException: One or more errors occurred.
(IServiceProvider is not available because execution context was not constructed with DistributedApplicationExecutionContextOptions.)
 ---> System.InvalidOperationException: IServiceProvider is not available because execution context was not
 constructed with DistributedApplicationExecutionContextOptions.
   at Aspire.Hosting.DistributedApplicationExecutionContext.get_ServiceProvider()
      in /_/src/Aspire.Hosting/DistributedApplicationExecutionContext.cs:line 67
   at Aspire.Hosting.ResourceBuilderExtensions.<>c__DisplayClass71_0`1.<WithVSCodeDebugSupport>b__0(CommandLineArgsCallbackContext ctx)
      in /_/src/Aspire.Hosting/ResourceBuilderExtensions.cs:line 2322
   at Aspire.Hosting.ResourceBuilderExtensions.<>c__DisplayClass14_0`1.<WithArgs>b__0(CommandLineArgsCallbackContext context)
      in /_/src/Aspire.Hosting/ResourceBuilderExtensions.cs:line 323
   at Aspire.Hosting.Azure.AzureResourcePreparer.GetAzureReferences(IResource resource, CancellationToken cancellationToken)
      in /_/src/Aspire.Hosting.Azure/AzureResourcePreparer.cs:line 387
   at Aspire.Hosting.Azure.AzureResourcePreparer.BuildRoleAssignmentAnnotations(DistributedApplicationModel appModel, List`1 azureResources, AzureProvisioningOptions options, CancellationToken cancellationToken)
      in /_/src/Aspire.Hosting.Azure/AzureResourcePreparer.cs:line 142
   at Aspire.Hosting.Azure.AzureResourcePreparer.BeforeStartAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken)
      in /_/src/Aspire.Hosting.Azure/AzureResourcePreparer.cs:line 39
   at Aspire.Hosting.DistributedApplication.ExecuteBeforeStartHooksAsync(CancellationToken cancellationToken)
      in /_/src/Aspire.Hosting/DistributedApplication.cs:line 490
   at Aspire.Hosting.DistributedApplication.RunAsync(CancellationToken cancellationToken)
      in /_/src/Aspire.Hosting/DistributedApplication.cs:line 440
Root Cause Analysis
- 
New Feature in 9.5.0: WithVSCodeDebugSupportwas added toAddPythonAppin Aspire 9.5.0 to enable VS Code debugging support (see diff)
- 
The Conflict: When AddAzureContainerAppEnvironmentis present, theAzureResourcePreparer.BeforeStartAsynclifecycle hook runs during manifest generation and callsGetAzureReferences(), which evaluates all args callbacks to discover Azure resource references
- 
The Bug: The WithVSCodeDebugSupportcallback attempts to accessctx.ExecutionContext.ServiceProvider, but during manifest generation, the execution context wasn't constructed withDistributedApplicationExecutionContextOptions, causing theServiceProviderproperty getter to throw an exception before theIsRunModecheck can execute
- 
Why 9.4.2 Works: The Python package in 9.4.2 does not include WithVSCodeDebugSupport, so this callback never exists
Impact
This regression affects any Aspire 9.5.0 project that:
- Uses Azure Container Apps resources (AddAzureContainerAppEnvironment)
- Uses Python apps (AddPythonApp)
- Attempts to generate deployment manifests (azd infra genor similar)
This likely affects other resource types with WithVSCodeDebugSupport when combined with Azure resources.
Workaround
Use Aspire 9.5.0 for all packages except the Python package, which should remain at 9.4.2:
<Sdk Name="Aspire.AppHost.Sdk" Version="9.5.0" />
<ItemGroup>
  <PackageReference Include="Aspire.Hosting.AppHost" Version="9.5.0" />
  <PackageReference Include="Aspire.Hosting.Python" Version="9.4.2" />
  <PackageReference Include="Aspire.Hosting.Azure.AppContainers" Version="9.5.0" />
</ItemGroup>This allows you to use Aspire 9.5.0 features while avoiding the WithVSCodeDebugSupport bug. The only functionality lost is VS Code debugging support for Python apps.
Reproduction Repository
Sample code available at: https://github.com/dotnet/aspire-samples/tree/main/samples/AspireWithPython
Just add var containerAppEnvironment = builder.AddAzureContainerAppEnvironment("containerAppEnvironment");