microsoft/aspire · error · InvalidOperationException

AzureEnvironmentResource must be present in the application…

Error message

AzureEnvironmentResource must be present in the application model.

What it means

Provisioning any Bicep resource requires an AzureEnvironmentResource in the application model; it carries the subscription/resource group/tenant provisioning context. When the model contains no such resource, this InvalidOperationException is thrown before deployment.

Solutions

  1. Add an Azure environment to the AppHost: builder.AddAzureEnvironment(...) (or ensure RunWithAzureDeveloperCli/publish-mode wiring creates it).
  2. Verify no transformation removes the AzureEnvironmentResource before provisioning.
  3. In tests, include an AzureEnvironmentResource in the model passed to the provisioning pipeline.

Example fix

// before
builder.AddAzureStorage("storage"); // publish without azure env
// after
builder.AddAzureEnvironment();
builder.AddAzureStorage("storage");
Defensive patterns

Strategy: validation

Validate before calling

if (!appModel.Resources.OfType<AzureEnvironmentResource>().Any())
    throw new InvalidOperationException("Add builder.AddAzureEnvironment() to the AppHost before provisioning Azure resources.");

Try / catch

try { await provisionAsync(...); } catch (InvalidOperationException ex) when (ex.Message.Contains("AzureEnvironmentResource")) { /* add azure environment to the model */ }

Prevention

When it happens

Trigger: Running an AppHost that adds Azure resources (e.g. AddAzureStorage) without calling any AddAzureEnvironment-equivalent (RunWithAzureEnvironment or AddAzureEnvironment), or having it removed by a model transformation, then triggering provisionStep.

Common situations: Publishing/deploying an AppHost created before Azure environment support or with azure environment wiring stripped; tests constructing a DistributedApplicationModel with only Bicep resources; conditional environment registration that didn't run.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/c917d58517b69901. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Azure/AzureBicepResource.cs:340

            context.Logger.LogDebug("Resource {ResourceName} is excluded from publish. Skipping provisioning.", resource.Name);
            return;
        }

        // Skip if already provisioned
        if (resource.ProvisioningTaskCompletionSource != null &&
            resource.ProvisioningTaskCompletionSource.Task.IsCompleted)
        {
            context.Logger.LogDebug("Resource {ResourceName} is already provisioned. Skipping provisioning.", resource.Name);
            return;
        }

        var bicepProvisioner = context.Services.GetRequiredService<IBicepProvisioner>();

        // Find the AzureEnvironmentResource from the application model
        var azureEnvironment = context.Model.Resources.OfType<AzureEnvironmentResource>().FirstOrDefault();
        if (azureEnvironment == null)
        {
            throw new InvalidOperationException("AzureEnvironmentResource must be present in the application model.");
        }

        var provisioningContext = await azureEnvironment.ProvisioningContextTask.Task.ConfigureAwait(false);

        var resourceTask = await context.ReportingStep
            .CreateTaskAsync(new MarkdownString($"Deploying **{resource.Name}**"), context.CancellationToken)
            .ConfigureAwait(false);

        await using (resourceTask.ConfigureAwait(false))
        {
            try
            {
                if (await bicepProvisioner.ConfigureResourceAsync(
                    resource, context.CancellationToken).ConfigureAwait(false))
                {
                    resource.ProvisioningTaskCompletionSource?.TrySetResult();
                    await resourceTask.CompleteAsync(
                        new MarkdownString($"Using existing deployment for **{resource.Name}**"),

View on GitHub (pinned to 25830f84bd)