Skip to content

WithVSCodeDebugSupport in Python 9.5.0 causes IServiceProvider error with AddAzureContainerAppEnvironment during manifest generation (azd infra gen) #11781

@lest54

Description

@lest54

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

  1. Create an AppHost project with Aspire 9.5.0

  2. 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" />
  3. 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();
  4. 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

  1. New Feature in 9.5.0: WithVSCodeDebugSupport was added to AddPythonApp in Aspire 9.5.0 to enable VS Code debugging support (see diff)

  2. The Conflict: When AddAzureContainerAppEnvironment is present, the AzureResourcePreparer.BeforeStartAsync lifecycle hook runs during manifest generation and calls GetAzureReferences(), which evaluates all args callbacks to discover Azure resource references

  3. The Bug: The WithVSCodeDebugSupport callback attempts to access ctx.ExecutionContext.ServiceProvider, but during manifest generation, the execution context wasn't constructed with DistributedApplicationExecutionContextOptions, causing the ServiceProvider property getter to throw an exception before the IsRunMode check can execute

  4. 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 gen or 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");

Metadata

Metadata

Assignees

Labels

area-deploymentarea-integrationsIssues pertaining to Aspire Integrations packagesazureIssues associated specifically with scenarios tied to using Azure

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions