microsoft/aspire · error · InvalidOperationException
AzureEnvironmentResource must be present in the application…
Error message
AzureEnvironmentResource must be present in the application model.
What it means
LoginToRegistryAsync needs the application model to contain an AzureEnvironmentResource, which supplies the provisioning context (tenant, subscription) used for ACR token authentication. If no such resource exists in the model, the lookup returns null and the library throws InvalidOperationException.
Solutions
- Call builder.AddAzureEnvironment(...) in your AppHost before adding the container registry resource.
- Verify with context.Model.Resources that exactly one AzureEnvironmentResource exists when debugging the app model.
- Upgrade or align all Azure hosting packages so the environment resource is registered by the standard Azure provisioning path.
- If login is invoked from custom tooling, ensure the same IDistributedApplicationBuilder instance that contains the registry also contains the environment resource.
Example fix
// before
var registry = builder.AddAzureContainerRegistry("acr");
// after
builder.AddAzureEnvironment("azure-env");
var registry = builder.AddAzureContainerRegistry("acr"); Defensive patterns
Strategy: validation
Validate before calling
var azureEnv = appBuilder.Resources.OfType<AzureEnvironmentResource>().FirstOrDefault();
if (azureEnv is null)
{
throw new InvalidOperationException("Call builder.AddAzureEnvironment before registry login/publish.");
} Type guard
var azureEnv = appBuilder.Resources.OfType<AzureEnvironmentResource>().FirstOrDefault(); bool hasAzureEnvironment = azureEnv is not null;
Try / catch
try
{
await LoginToRegistryAsync(registry, context);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("AzureEnvironmentResource"))
{
logger.LogError("App model is missing AzureEnvironmentResource; add builder.AddAzureEnvironment().");
throw;
} Prevention
- Always call AddAzureEnvironment in AppHosts that add Azure infrastructure resources.
- Review AppHost templates after upgrading packages that introduced AzureEnvironmentResource.
- Add a startup assertion in custom tooling that the environment resource exists before login operations.
When it happens
Trigger: Running a publish/deploy workflow that references an AzureContainerRegistryResource (or performs registry login) without ever calling AddAzureEnvironment on the distributed application builder, so the model contains no AzureEnvironmentResource.
Common situations: Copy-pasting an AppHost that adds a container registry without the Azure environment setup; adding an ACR to an existing app that was created before AzureEnvironmentResource was introduced; removing the Azure environment resource during refactoring while keeping registry infrastructure.
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
- A purge task with the name
- Ago must be at least 1 minute to be compatible with acr…
- Azure environment resource required by AKS environment
- Failed to retrieve container registry endpoint.
- Failed to retrieve container registry information.
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/a8040d3034b72000.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Azure.ContainerRegistry/AzureContainerRegistryHelpers.cs:27
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Pipelines;
using Microsoft.Extensions.DependencyInjection;
namespace Aspire.Hosting.Azure;
/// <summary>
/// Helper methods for Azure Container Registry operations.
/// </summary>
internal static class AzureContainerRegistryHelpers
{
public static async Task LoginToRegistryAsync(IContainerRegistry registry, PipelineStepContext context)
{
var acrLoginService = context.Services.GetRequiredService<IAcrLoginService>();
var tokenCredentialProvider = context.Services.GetRequiredService<ITokenCredentialProvider>();
// Find the AzureEnvironmentResource from the application model
var azureEnvironment = context.Model.Resources.OfType<AzureEnvironmentResource>().FirstOrDefault() ??
throw new InvalidOperationException("AzureEnvironmentResource must be present in the application model.");
var registryName = await registry.Name.GetValueAsync(context.CancellationToken).ConfigureAwait(false) ??
throw new InvalidOperationException("Failed to retrieve container registry information.");
var registryEndpoint = await registry.Endpoint.GetValueAsync(context.CancellationToken).ConfigureAwait(false) ??
throw new InvalidOperationException("Failed to retrieve container registry endpoint.");
var loginTask = await context.ReportingStep.CreateTaskAsync(
new MarkdownString($"Logging in to **{registryName}**"),
context.CancellationToken).ConfigureAwait(false);
await using (loginTask.ConfigureAwait(false))
{
try
{
// Get tenant ID from the provisioning context (always available from subscription)
var provisioningContext = await azureEnvironment.ProvisioningContextTask.Task.ConfigureAwait(false);
var tenantId = provisioningContext.Tenant.TenantId?.ToString()
?? throw new InvalidOperationException("Tenant ID is required for ACR authentication but was not available in provisioning context.");
View on GitHub (pinned to 25830f84bd)