{"record":{"id":"c917d58517b69901","repo":"microsoft/aspire","slug":"azureenvironmentresource-must-be-present-in-the-application-c917d5","errorCode":null,"errorMessage":"AzureEnvironmentResource must be present in the application model.","messagePattern":"AzureEnvironmentResource must be present in the application model\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure/AzureBicepResource.cs","lineNumber":340,"sourceCode":"            context.Logger.LogDebug(\"Resource {ResourceName} is excluded from publish. Skipping provisioning.\", resource.Name);\n            return;\n        }\n\n        // Skip if already provisioned\n        if (resource.ProvisioningTaskCompletionSource != null &&\n            resource.ProvisioningTaskCompletionSource.Task.IsCompleted)\n        {\n            context.Logger.LogDebug(\"Resource {ResourceName} is already provisioned. Skipping provisioning.\", resource.Name);\n            return;\n        }\n\n        var bicepProvisioner = context.Services.GetRequiredService<IBicepProvisioner>();\n\n        // Find the AzureEnvironmentResource from the application model\n        var azureEnvironment = context.Model.Resources.OfType<AzureEnvironmentResource>().FirstOrDefault();\n        if (azureEnvironment == null)\n        {\n            throw new InvalidOperationException(\"AzureEnvironmentResource must be present in the application model.\");\n        }\n\n        var provisioningContext = await azureEnvironment.ProvisioningContextTask.Task.ConfigureAwait(false);\n\n        var resourceTask = await context.ReportingStep\n            .CreateTaskAsync(new MarkdownString($\"Deploying **{resource.Name}**\"), context.CancellationToken)\n            .ConfigureAwait(false);\n\n        await using (resourceTask.ConfigureAwait(false))\n        {\n            try\n            {\n                if (await bicepProvisioner.ConfigureResourceAsync(\n                    resource, context.CancellationToken).ConfigureAwait(false))\n                {\n                    resource.ProvisioningTaskCompletionSource?.TrySetResult();\n                    await resourceTask.CompleteAsync(\n                        new MarkdownString($\"Using existing deployment for **{resource.Name}**\"),","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure/AzureBicepResource.cs#L322-L358","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add an Azure environment to the AppHost: builder.AddAzureEnvironment(...) (or ensure RunWithAzureDeveloperCli/publish-mode wiring creates it).","Verify no transformation removes the AzureEnvironmentResource before provisioning.","In tests, include an AzureEnvironmentResource in the model passed to the provisioning pipeline."],"exampleFix":"// before\nbuilder.AddAzureStorage(\"storage\"); // publish without azure env\n// after\nbuilder.AddAzureEnvironment();\nbuilder.AddAzureStorage(\"storage\");","handlingStrategy":"validation","validationCode":"if (!appModel.Resources.OfType<AzureEnvironmentResource>().Any())\n    throw new InvalidOperationException(\"Add builder.AddAzureEnvironment() to the AppHost before provisioning Azure resources.\");","typeGuard":null,"tryCatchPattern":"try { await provisionAsync(...); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"AzureEnvironmentResource\")) { /* add azure environment to the model */ }","preventionTips":["Always call builder.AddAzureEnvironment() in AppHosts that provision Azure resources.","Keep azure environment registration outside conditional blocks that may not run.","Include AzureEnvironmentResource in models built by tests."],"tags":["azure","bicep","provisioning","app-model"],"backgroundTag":"missing-dependency","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}