{"record":{"id":"64fccc39c67cf146","repo":"microsoft/aspire","slug":"unknown-container-lifetime-enum-getname-typeof","errorCode":null,"errorMessage":"Unknown container lifetime '{Enum.GetName(typeof(ContainerLifetime), containerLifetimeAnnotation.Lifetime)}'.","messagePattern":"Unknown container lifetime '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs","lineNumber":1140,"sourceCode":"            return persistenceAnnotation.Mode switch\n            {\n                PersistenceMode.Session => Lifetime.Session,\n                PersistenceMode.Persistent => Lifetime.Persistent,\n                PersistenceMode.Resource => persistenceAnnotation.SourceResource is { } sourceResource\n                    ? GetLifetimeType(sourceResource, visitedResources)\n                    : throw new InvalidOperationException($\"Resource '{resource.Name}' has a resource persistence mode but no source resource.\"),\n                PersistenceMode.ParentProcess => Lifetime.Persistent,\n                _ => throw new InvalidOperationException($\"Unknown persistence mode '{Enum.GetName(typeof(PersistenceMode), persistenceAnnotation.Mode)}'.\")\n            };\n        }\n\n        if (resource.TryGetLastAnnotation<ContainerLifetimeAnnotation>(out var containerLifetimeAnnotation))\n        {\n            return containerLifetimeAnnotation.Lifetime switch\n            {\n                ContainerLifetime.Session => Lifetime.Session,\n                ContainerLifetime.Persistent => Lifetime.Persistent,\n                _ => throw new InvalidOperationException($\"Unknown container lifetime '{Enum.GetName(typeof(ContainerLifetime), containerLifetimeAnnotation.Lifetime)}'.\")\n            };\n        }\n\n        return Lifetime.Session;\n    }\n\n    /// <summary>\n    /// Determines whether the specified resource has a persistent lifetime.\n    /// </summary>\n    /// <param name=\"resource\">The resource to get persistent lifetime behavior for.</param>\n    /// <returns><see langword=\"true\"/> if the resource has a persistent container or executable lifetime, otherwise <see langword=\"false\"/>.</returns>\n    internal static bool HasPersistentLifetime(this IResource resource)\n    {\n        return resource.GetLifetimeType() == Lifetime.Persistent;\n    }\n\n    internal static string GetOtelServiceInstanceId(this IResource resource, DcpInstance instance)\n    {","sourceCodeStart":1122,"sourceCodeEnd":1158,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs#L1122-L1158","documentation":"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.","triggerScenarios":"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.","commonSituations":"Version skew between packages (one writes a new ContainerLifetime value the resolving code doesn't know); custom tooling casting ints to ContainerLifetime.","solutions":["Align all Aspire packages to the same version.","Set the annotation's Lifetime to ContainerLifetime.Session or ContainerLifetime.Persistent only.","Remove or fix hand-built ContainerLifetimeAnnotation instances with invalid values."],"exampleFix":"// before\nnew ContainerLifetimeAnnotation { Lifetime = (ContainerLifetime)7 }; // invalid\n// after\nnew ContainerLifetimeAnnotation { Lifetime = ContainerLifetime.Persistent }; // valid","handlingStrategy":"try-catch","validationCode":"if (!Enum.IsDefined(typeof(ContainerLifetime), ann.Lifetime)) throw new InvalidOperationException($\"Unknown ContainerLifetime: {ann.Lifetime}\");","typeGuard":"bool IsKnown(ContainerLifetime l) => Enum.IsDefined(typeof(ContainerLifetime), l);","tryCatchPattern":"try { var lifetime = resource.GetLifetimeType(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Unknown container lifetime\")) { /* check package version alignment */ throw; }","preventionTips":["Align Aspire package versions across the app.","Only assign ContainerLifetime.Session or ContainerLifetime.Persistent.","Avoid manual enum casts in custom tooling."],"tags":["aspire","enum","container-lifetime","version-mismatch"],"backgroundTag":"invalid-enum-value","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"}