microsoft/aspire · error · InvalidOperationException

Unknown container lifetime

Error message

Unknown container lifetime '{Enum.GetName(typeof(ContainerLifetime), containerLifetimeAnnotation.Lifetime)}'.

What it means

GetLifetimeType maps ContainerLifetime.Session and ContainerLifetime.Persistent to Lifetime values. Any other ContainerLifetime value hits the default arm and throws InvalidOperationException naming the unknown lifetime.

Solutions

  1. Align all Aspire packages to the same version.
  2. Set the annotation's Lifetime to ContainerLifetime.Session or ContainerLifetime.Persistent only.
  3. Remove or fix hand-built ContainerLifetimeAnnotation instances with invalid values.

Example fix

// before
new ContainerLifetimeAnnotation { Lifetime = (ContainerLifetime)7 }; // invalid
// after
new ContainerLifetimeAnnotation { Lifetime = ContainerLifetime.Persistent }; // valid
Defensive patterns

Strategy: try-catch

Validate before calling

if (!Enum.IsDefined(typeof(ContainerLifetime), ann.Lifetime)) throw new InvalidOperationException($"Unknown ContainerLifetime: {ann.Lifetime}");

Type guard

bool IsKnown(ContainerLifetime l) => Enum.IsDefined(typeof(ContainerLifetime), l);

Try / catch

try { var lifetime = resource.GetLifetimeType(); } catch (InvalidOperationException ex) when (ex.Message.Contains("Unknown container lifetime")) { /* check package version alignment */ throw; }

Prevention

When it happens

Trigger: A ContainerLifetimeAnnotation whose Lifetime is not a recognized enum member — usually from mixed Aspire package versions where an enum value was added in a newer version, or a bad cast when constructing the annotation manually.

Common situations: Version skew between packages (one writes a new ContainerLifetime value the resolving code doesn't know); custom tooling casting ints to ContainerLifetime.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs:1140

            return persistenceAnnotation.Mode switch
            {
                PersistenceMode.Session => Lifetime.Session,
                PersistenceMode.Persistent => Lifetime.Persistent,
                PersistenceMode.Resource => persistenceAnnotation.SourceResource is { } sourceResource
                    ? GetLifetimeType(sourceResource, visitedResources)
                    : throw new InvalidOperationException($"Resource '{resource.Name}' has a resource persistence mode but no source resource."),
                PersistenceMode.ParentProcess => Lifetime.Persistent,
                _ => throw new InvalidOperationException($"Unknown persistence mode '{Enum.GetName(typeof(PersistenceMode), persistenceAnnotation.Mode)}'.")
            };
        }

        if (resource.TryGetLastAnnotation<ContainerLifetimeAnnotation>(out var containerLifetimeAnnotation))
        {
            return containerLifetimeAnnotation.Lifetime switch
            {
                ContainerLifetime.Session => Lifetime.Session,
                ContainerLifetime.Persistent => Lifetime.Persistent,
                _ => throw new InvalidOperationException($"Unknown container lifetime '{Enum.GetName(typeof(ContainerLifetime), containerLifetimeAnnotation.Lifetime)}'.")
            };
        }

        return Lifetime.Session;
    }

    /// <summary>
    /// Determines whether the specified resource has a persistent lifetime.
    /// </summary>
    /// <param name="resource">The resource to get persistent lifetime behavior for.</param>
    /// <returns><see langword="true"/> if the resource has a persistent container or executable lifetime, otherwise <see langword="false"/>.</returns>
    internal static bool HasPersistentLifetime(this IResource resource)
    {
        return resource.GetLifetimeType() == Lifetime.Persistent;
    }

    internal static string GetOtelServiceInstanceId(this IResource resource, DcpInstance instance)
    {

View on GitHub (pinned to 25830f84bd)